现在的位置: 首页 > 综合 > 正文

Loading Large Bitmaps Efficiently [有效率的加载大尺寸的位图]

2013年10月06日 ⁄ 综合 ⁄ 共 3189字 ⁄ 字号 评论关闭

Loading Large Bitmaps Efficiently[有效率的加载大尺寸的位图]

  • 图片有不同的形状与大小。在大多数情况下它们的实际大小都比需要呈现出来的要大很多。例如,系统的Gallery程序会显示那些你使用设备camera拍摄的图片,但是那些图片的分辨率通常都比你的设备屏幕分辨率要高很多。
  • 考虑到程序是在有限的内存下工作,理想情况是你只需要在内存中加载一个低分辨率的版本即可。这个低分辨率的版本应该是与你的UI大小所匹配的,这样才便于显示。一个高分辨率的图片不会提供任何可见的好处,却会占用宝贵的(precious)的内存资源,并且在缩放时产生额外的性能开销。
  • 这一课会介绍如何通过解码大的bitmap,然后加载一个较小的图片到内存中去,从而避免超出程序的内存限制。

Read Bitmap Dimensions and Type 读取位图的尺寸与类型


BitmapFactory类提供了一些解码的方法
(
decodeByteArray(),decodeFile(),decodeResource(),
etc.) 用来从不同的资源中创建一个
Bitmap。根据你的图片数据来源选择合适的解码方法。那些方法在构造位图的时候会尝试分配内存,因此会容易导致OutOfMemory的异常。每一种decode方法都提供了通过BitmapFactory.Options 来设置一些附加的标记来指定decode的选项。设置 inJustDecodeBounds 属性为true可以在解码的时候避免内存的分配,它会返回一个null的bitmap,但是outWidth,outHeightoutMimeType 还是可以获取的。这个技术可以允许你在构造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;

为了避免OutOfMemory 的异常,我们在真正decode图片之前检查它的尺寸,除非你确定这个数据源提供了准确无误的图片且不会导致占用过多的内存。

Load a Scaled Down Version into Memory 加载一个按比例缩小的版本到内存中

  • 通过上面的步骤我们已经知道了图片的尺寸,那些数据可以用来决定是应该加载整个图片到内存中还是加载一个缩略的版本。下面有一些因素需要考虑:

    • 估计加载完整图片所需要耗费的内存。
    • 程序在加载这张图片时会涉及到其他内存需求。
    • 呈现这张图片的组件的尺寸大小。
    • 屏幕大小与当前设备的屏幕密度。
  • 例如,如果把一个原图是1024*768 pixel的图片显示到ImageView为128*96 pixel的缩略图就没有必要把整张图片都加载到内存中。
  • 为了告诉解码器去加载一个缩略的图片到内存,需要在你的BitmapFactory.Options 中设置 inSampleSizetrue
    。例如
    , 一个分辨率为2048x1536 的图片,如果设置inSampleSize 为4,那么会加载一个大概为512x384的图片。加载这张小的图片到内存仅仅使用大概0.75MB,如果是加载全图那么大概要耗费12MB(假设bitmap的配置是ARGB_8888).
    下面有一根据目标图片大小来计算Sample图片大小的方法:
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;
}
  • Note: 设置 inSampleSize 为2的幂
    对于decoder会更加的有效率,然而,如果你打算把调整过大小的图片缓存到磁盘上,设置为2的幂也能很有效的节省缓存的空间.
  • 使用这个方法,首先需要设置inJustDecodeBounds 为true,
    把options的值传递过来,然后使用inSampleSize 的值并设置inJustDecodeBounds 为false 重新decode一遍。
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);
}

使用这个方法可以简单的加载一个任意大小的图片并显示为100*100 pixel的缩略图形式。像下面演示的一样:

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));


你可以通过替换合适的BitmapFactory.decode* 方法来写一个类似的方法从其他的数据源进行解码图片

官方文档:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap

抱歉!评论已关闭.