Bitmaps加载之高效加载大图

前文

很多时候我们要加载的图片的尺寸比放置的控件的来的大,而该控件只要求图片的像素和控件本身的尺寸一致即可达到最好的显示效果,大的图片不仅不能带来更多的视觉好处,反而消耗了一定的内存,还产生了额外的缩放的计算

高效加载大图步骤

1. 读取Bitmap的尺寸和类型

利用BitmapFactory.Options的inJustDecodeBounds属性来读取图片尺寸,将inJustDecodeBounds设为true是,调用BitmapFactory的decode相关方法时,Bitmap将不会读到内存,但是相关的信息会读到,如下:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

打印相关参数:

I/Log: width: 2247  height: 1264 config: ARGB_8888  mime: image/png

2. 将缩放后的Bitmap读取到内存

得到图片尺寸信息后,可以通过下列一些因素来考虑加载多大的图片进来:

  • 估计将图片完全加载进来的内存占用
  • 你的应用中打算给图片分配的内存的大小
  • 图片所要用到的控件的尺寸
  • 当前设备屏幕的大小和density

比如图片的尺寸为1024x768 pixel,但是我的ImageView的尺寸只是128x96,如果将图片完全加载进来的话占用的内存大小为:1024*768*4=3145728bytes=3M(假设默认ARGB_8888),而如果将图片缩放到ImageView的尺寸再加载进来,占用的内存大小为:128*96*4=49152bytes=48Kb,减少了97.5%的内存占用,这效果十分明显,而这缩放的实现可以通过BitmapFactory.Options的inSampleSize来实现,inSampleSize的计算,看下面具体代码:

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) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

计算完inSampleSize后,就可以使用了,如下:

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中调用
mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
...

Notice

  • 关于inSampleSize的计算,当inSampleSize<=1时,inSampleSize=1;当inSampleSize>1时,inSampleSize=向下取整2的幂,比如设置inSampleSize=7,但是实际取的值为4,如下面代码演示:
for (int i = 0; i < 16; i++) {
    BitmapFactory.Options scaleOption = new BitmapFactory.Options();
    scaleOption.inSampleSize = i;
    scaleOption.inScaled=false;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, scaleOption);
    double rw = (w * 1.0 / bitmap.getWidth());
    double rh = (h * 1.0 / bitmap.getHeight());
    DecimalFormat format = new DecimalFormat("0.00");
    Log.i("Log", "width: \t" + bitmap.getWidth() +'\t'+ "  height: \t" + bitmap.getHeight() +'\t'+
            "  originW/scaleH: \t" + format.format(rw) +'\t'+ " originH/scaleH: \t" + format.format(rh) +'\t'+ "   inSampleSize: \t" + i);
}

上面代码从0到16设置inSampleSize,输出结果如下:

I/Log: width:   1024      height:   640   originW/scaleH:   1.00     originH/scaleH:    1.00       inSampleSize:    0
I/Log: width:   1024      height:   640   originW/scaleH:   1.00     originH/scaleH:    1.00       inSampleSize:    1
I/Log: width:   512   height:   320   originW/scaleH:   2.00     originH/scaleH:    2.00       inSampleSize:    2
I/Log: width:   512   height:   320   originW/scaleH:   2.00     originH/scaleH:    2.00       inSampleSize:    3
I/Log: width:   256   height:   160   originW/scaleH:   4.00     originH/scaleH:    4.00       inSampleSize:    4
I/Log: width:   256   height:   160   originW/scaleH:   4.00     originH/scaleH:    4.00       inSampleSize:    5
I/Log: width:   256   height:   160   originW/scaleH:   4.00     originH/scaleH:    4.00       inSampleSize:    6
I/Log: width:   256   height:   160   originW/scaleH:   4.00     originH/scaleH:    4.00       inSampleSize:    7
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    8
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    9
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    10
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    11
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    12
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    13
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    14
I/Log: width:   128   height:   80    originW/scaleH:   8.00     originH/scaleH:    8.00       inSampleSize:    15

Reference

  1. Loading Large Bitmaps Efficiently

你可能感兴趣的:(Bitmaps加载之高效加载大图)