如何优化加载大位图(BitmapFactory使用系列)

Loading Large Bitmaps Efficiently

如何优化加载大位图

Images come in all shapes and sizes.In many cases they are larger than required for a typical application userinterface (UI). For example, the system Gallery application displays photostaken using your Android devices's camera which are typically much higherresolution than the screen density of your device.

Given that you are working with limited memory, ideally you onlywant to load a lower resolution version in memory. The lower resolution versionshould match the size of the UI component that displays it. An image with ahigher resolution does not provide any visible benefit, but still takes upprecious memory and incurs additional performance overhead due to additional onthe fly scaling.

This lesson walks you through decoding large bitmaps without exceedingthe per application memory limit by loading a smaller subsampled version inmemory.

图片有各种形状和大小。在很多情况下,这些图片的大小超过我们应用程序的需要,例如用手机拍摄的图片。

假如,你的应用程序只有有限的内存容量,最理想的做法是在内存中加载低分辨率的图像版本。低版本的图像需要适应设备屏幕的大小。一张高分辨率的图片不仅不会改善显示质量,而且还会影响应用程序的性能。

这个课程将讲述如何将一些大的位图压缩成少于应用程序使用内存容量的小图片。

ReadBitmap Dimensions and Type


读取位图的尺寸和类型


TheBitmapFactoryclass providesseveral decoding methods (decodeByteArray(),decodeFile(),decodeResource(), etc.)for creating aBitmapfrom various sources.Choose the most appropriate decode method based on your image data source.These methods attempt to allocate memory for the constructed bitmap andtherefore can easily result in anOutOfMemoryexception. Each typeof decode method has additional signatures that let you specify decodingoptions via theBitmapFactory.Optionsclass. Setting theinJustDecodeBoundsproperty totruewhile decoding avoidsmemory allocation, returningnullfor the bitmap objectbut settingoutWidth,outHeightandoutMimeType. Thistechnique allows you to read the dimensions and type of the image data prior toconstruction (and memory allocation) of the bitmap.

BitmapFactory类提供了几个解码方法能够从各种源文件中创建Bitmap对象,例如(decodeByteArray(),decodeFile(),decodeResource()等)。我们需要根据图片的格式选择合适的解码方法。这些方法都尝试去为创建好的Bitmap对象分配内存,但是这样做会很容易导致OutOfMemory的异常。每种类型的解码方法,你都需要通过BitmapFactory.Options类来指定参数值。将inJustDecodeBounds参数值设定为true的时候,解码过程中将不为对象分配内容,但是会将图片的尺寸等信息通过BitmapFactory.Options对象的outWidth,outHeightoutMimeType值反馈。

如下代码所示:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
//解码完成后,outWidth,outHeightoutMimeType值将包含图片的尺寸大小
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

To avoidjava.lang.OutOfMemoryexceptions, check thedimensions of a bitmap before decoding it, unless you absolutely trust thesource to provide you with predictably sized image data that comfortably fitswithin the available memory.

为了避免java.lang.OutOfMemory异常,需要在真正解码之前,通过上面代码描述的方法获取图片的真实尺寸。

Load aScaled Down Version into Memory


将一个压缩版本加载到内存中


Now that the image dimensions are known, they can be used todecide if the full image should be loaded into memory or if a subsampledversion should be loaded instead. Here are some factors to consider:

至此,你可以通过图片的尺寸来觉得是否使用一个一个压缩的版本来加载到内存中。下面有一些因素,你需要考虑到:

·Estimated memory usage of loading the fullimage in memory.

·Amount of memory you are willing to commit toloading this image given any other memory requirements of your application.

·Dimensions of the targetImageViewor UIcomponent that the image is to be loaded into.

·Screen size and density of the currentdevice.

1、预估加载整个图片后,需要占用的内存大小;

2、 应用程序关于加载图片时使用内存大小的需求;

3、 存放图片的ImageView或者UI组件的尺寸;

4、 屏幕大小。

To tell the decoder to subsample theimage, loading a smaller version into memory, setinSampleSizetotruein yourBitmapFactory.Optionsobject. For example,an image with resolution 2048x1536 that is decoded with aninSampleSizeof 4 produces abitmap of approximately 512x384. Loading this into memory uses 0.75MB ratherthan 12MB for the full image (assuming a bitmap configuration ofARGB_8888). Here’sa method to calculate a sample size value that is a power of two based on atarget width and height:

为了能够将图片压缩,你需要将inSampleSize的值设定为true。下面是计算压缩比例的例子:

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;
}

Note:A power of two valueis calculated because the decoder uses a final value by rounding down to thenearest power of two, as per theinSampleSizedocumentation.

To use this method, first decode withinJustDecodeBoundsset totrue, pass theoptions through and then decode again using the newinSampleSizevalue andinJustDecodeBoundsset tofalse:

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);
}

This method makes it easy to load a bitmap of arbitrarily largesize into anImageViewthat displays a100x100 pixel thumbnail, as shown in the following example code:

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

You can follow a similar process to decode bitmaps from othersources, by substituting the appropriateBitmapFactory.decode*method as needed.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值