Bitmap 处理 之加载大的Bitmap

流畅的加载大的Bitmap

原文连接

在处理Bitmap的过中经常遇到的问题是OOM,尤其是在加载像素比较大的bitmap时,出现这种问题的概率比较高。在加载图片的时候,就要考虑是否真的需要那么大像素的bitmap,如果要显示的ImageView的大小是120x140,但是要加载的资源的图片是1024X768的,这时候如果直接加载资源很容易出现OOM的问题,如果在加载图片之前能够知道图片的像素的大小,然后在适当的压缩处理,然后在显示出来,这样OOM的问题就比较不容易出现。

读取Bitmap的大小和类型

BitmapFactory提供了很多方法来解析方法(decodeByteArray(), decodeFile(), decodeResource()等等)来创建bitmap,你可以选择一个合适的方法来创建bitmap,但是这样直接的创建bitmap和容易引起OOM,BitmapFactory还提供一组配套的方法来创建bitmap,需要加上BitmapFactory.Options的参数,在显示bitmap之前事先读取bitmap的大小和类型:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

将BitmapFactory.Options的inJustDecodeBounds设置为true,可以在解析bitmap之前获得bitmap的大小和类型

压缩bitmap之后,再加载到内存

在显示bitmap之前,需要获得目标显示的bitmap的大小和要加载的bitmap的大小,然后计算出来一个合适的压缩比,把通过压缩过的bitmap加载到内存中,下面是一个封装的方法,来计算合适的压缩比
 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

       // Raw height and width of image 

       final int height = options.outHeight; 

       final int width = options.outWidth;

       int inSampleSize = 1;

       if (height > reqHeight || width > reqWidth) {

           // Calculate ratios of height and width to requested height and

           // width

           final int heightRatio = Math.round((float) height / (float) reqHeight);

           final int widthRatio = Math.round((float) width / (float) reqWidth);

           // Choose the smallest ratio as inSampleSize value, this will

           // guarantee

           // a final image with both dimensions larger than or equal to the

           // requested height and width.

           inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

       }

       return inSampleSize;

   }
其中 reqWidth,reqHeight是目标的宽的和高度,height,width是资源的高度和宽的,然后计算出一个压缩的比例,使用最小的压缩比
要使用这个方法,首先将inJustDecodeBounds 设置为true,解析获得资源的大小和类型,然后通过上面的方法获得压缩比,然后再将inJustDecodeBounds设置为false,通过计算出来的要所比来解析最终要显示的bitmap
 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,

       int reqWidth, int reqHeight) {

   // First decode with inJustDecodeBounds=true to check dimensions 

   final BitmapFactory.Options options = new BitmapFactory.Options();

   options.inJustDecodeBounds = true;

   BitmapFactory.decodeResource(res, resId, options);

   // Calculate inSampleSize

   options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

   // Decode bitmap with inSampleSize set

   options.inJustDecodeBounds = false;

   return BitmapFactory.decodeResource(res, resId, options);

  }

例子: imageView的显示,ImageView要显示的图像的大小是100X100,实际的图像可能不是100X100,通过上面的方法做一下压缩处理,这样就避免出现OOM的问题
mImageView.setImageBitmap(


   decodeSampledBitmapFromResource(getResources(), R.drawable.myimage, 100, 100));

上面是通过解析decodeSampledBitmapFromResource 资源文件获得bitmap,当然还有其他的方法来获得bitmap
  1. 通过文件获得bitmap
public static Bitmap decodeSampledBitmapFromFile(String filename,

           int reqWidth, int reqHeighte) {

       // First decode with inJustDecodeBounds=true to check dimensions

       final BitmapFactory.Options options = new BitmapFactory.Options();

       options.inJustDecodeBounds = true;

       BitmapFactory.decodeFile(filename, options);

       // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

       // Decode bitmap with inSampleSize set

       options.inJustDecodeBounds = false;

       return BitmapFactory.decodeFile(filename, options);

   
   2.通过FileDescriptor获得bitmap
public static Bitmap decodeSampledBitmapFromDescriptor(

           FileDescriptor fileDescriptor, int reqWidth, int reqHeight, ImageCache cache) {

       // First decode with inJustDecodeBounds=true to check dimensions

       final BitmapFactory.Options options = new BitmapFactory.Options();

       options.inJustDecodeBounds = true;

       BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

       // Calculate inSampleSize

       options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

       // Decode bitmap with inSampleSize set

       options.inJustDecodeBounds = false;

       return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);

   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值