android实用的Bitmap处理函数-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

android实用的Bitmap处理函数

在做android 开发的时候,特别是游戏开发,经常会用到不同大小的图片,比如:适配不同屏大小、以及不同的地方需要不同大小的图片等等。

创新互联公司是一家以成都网站建设、网页设计、品牌设计、软件运维、营销推广、小程序App开发等移动开发为一体互联网公司。已累计为成都混凝土泵车等众行业中小客户提供优质的互联网建站和软件开发服务。

这里给出一个将图片放大、缩小的处理函数:

/**
* bgp_w_picpath  需要处理的图片
*  newWidth 、newHeight 这张图片处理后的宽高,这里newWidth 和 newHeight 其有效值是>= 0; 当其中一个值为0时候,表示按等宽* 高比来放大或是缩小。如果两个值都为0,则不进行放大缩小。
*  recy_the_bitmap 其值是一个boolean 值。其作用是:对bgp_w_picpath  这张图片处理完成后是否回收其资源,对于手机这样的移动设备开发,其资源非常有限,这个是必须的。
 **/

 public static Bitmap zoomImage(Bitmap bgp_w_picpath, int newWidth, int newHeight, boolean recy_the_bitmap) { // 记得函数声明为static
           int width;  
           int height;  
           Bitmap bitmap_mage = null;

            if(bgp_w_picpath != null && !(newWidth==0 && newHeight==0))
               {
                   float scaleWidth = 1.0f;
                   float scaleHeight = 1.0f;

                   width = bgp_w_picpath.getWidth();
                   height = bgp_w_picpath.getHeight();
                   Matrix matrix = new Matrix();

                    if(newWidth > 0)
                    {
                         scaleWidth = ((float) newWidth) / width;  
                    }
                    if(newHeight > 0)
                     {
                         scaleHeight = ((float) newHeight) / height;
                      }

                    if(scaleWidth == 1.0f)
                     {
                         scaleWidth = scaleHeight;
                      }

                    if(scaleHeight == 1.0f)
                    {
                     scaleHeight = scaleWidth;
                     }

                    matrix.postScale(scaleWidth, scaleHeight);  
                    bitmap_mage = Bitmap.createBitmap(bgp_w_picpath, 0, 0, width, height,  
                      matrix, true);

                }


          if(bitmap_mage != null)
           {
                if(recy_the_bitmap)
                {// 回收资源
                 bgp_w_picpath.recycle();
                 bgp_w_picpath = null;
                 }
                return bitmap_mage;
            }
       else
           {
                return bgp_w_picpath;
            }

}

有什么不妥的地方请大家指出,


新闻名称:android实用的Bitmap处理函数
本文网址:http://kswsj.cn/article/pcoeho.html

其他资讯