android 实用的Bitmap处理函数

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

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


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

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

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

                    width = bgimage.getWidth(); 
                    height = bgimage.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(bgimage, 0, 0, width, height,   
                       matrix, true);

                 }


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


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值