Loading Large Bitmaps Efficiently
有效加载大型Bitmap
Images come in all shapes and sizes.
图片的形状和尺寸是各种各样的
In many cases they are larger than required for a typical application user interface (UI).
For example, the system Gallery application displays photos taken using your Android devices's camera which are typically much higher resolution than the screen density of your device.
很多情况中,他们都大于典型应用UI所需要的
例如,系统Gallery应用显示使用你的Android设备的camera拍出的照片,它们分辨率都远大于你的设备的屏幕密度
Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory.
The lower resolution version should match the size of the UI component that displays it.
An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.
由于你在一个有限内存的设备上工作,理想情况下,你只是想在内存中加载一个低分辨率的版本
这个低分辨率的版本应该正好与显示它的UI组件大小匹配
一个高分辨率的图像不会提供任何可见的好处,但是仍然会占据非常大的内存并且会引起额外的性能开销
This lesson walks you through decoding large bitmaps without exceeding the per application memory limit by loading a smaller subsampled version in memory.
这一课将给你展示通过在内存中加载一个小一些的子样版本来解码大bitmap而不会超过每一个应用内存限制
Read Bitmap Dimensions and Type
读取bitmap尺寸与类型
The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources.
Choose the most appropriate decode method based on your image data source.
These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception.
Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.
Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.
BitmapFactory类提供几种解码方法(decodeByteArray(), decodeFile(), decodeResource()等等)从资源中来建立一个bitmap
基于你的图片数据源选字最恰当的解码方式
这些方法试图为构建bitmap分配内存,因而可能很容易的导致一个OutOfMemory异常
每种解码方法都有额外的签名好让你通过BitmapFactory.Options指定解码选项
当解码的时候,设置属性inJustDecodeBounds=true来避免内存分配,返回值为null,但是设置了BitmapFactory.Options的outWidth, outHeight 和 outMimeType属性。
这种技术允许你在建立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;
To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory.
为了避免java.lang.OutOfMemory异常,在解码bitmap之前检查他的尺寸,除非你绝对信任提供给你的图片资源的大小是可知的,正好在可用内存范围内的
Load a Scaled Down Version into Memory
加载一个按比例比缩小版本的到内存中
Now that the image dimensions are known, they can be used to decide if the full image should be loaded into memory or if a subsampled version should be loaded instead.
Here are some factors to consider:
现在图片尺寸知道了,它们可以用于判断全图是否应该被加载到内存,或者应该加载子样版本
下面一些因素需要考虑:
Estimated memory usage of loading the full image in memory.
Amount of memory you are willing to commit to loading this image given any other memory requirements of your application.
Dimensions of the target ImageView or UI component that the image is to be loaded into.
Screen size and density of the current device.
For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.
评估加载全图所使用的内存
基于你的应用中任何其他内存需求,加载这个图片你愿意提供多少内存
图片要加载到的目标ImageView或者其他UI组件的尺寸
当前设备的屏幕尺寸和密度
例如,如果图片最终是要在一个ImageView中显示为一个128x96像素的缩略图,那么加载一张1024x768像素图片到内存中是不值得的
To tell the decoder to subsample the image, loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object.
For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384.
Loading this into memory uses 0.75MB rather than 12MB for the full image (assuming a bitmap configuration of ARGB_8888).
Here’s a method to calculate a the sample size value based on a target width and height:
在你的BitmapFactory.Options对象中设置inSampleSize=true,可以告诉解码器把图片子样话,加载一个小版本到内存中
例如,一个分辨率为2048x1536的图片,使用inSampleSize=4进行编码,产出的bitmap大致为512x384.
一个基于目标宽高的方法来计算样本大小的值
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: Using powers of 2 for inSampleSize values is faster and more efficient for the decoder.
However, if you plan to cache the resized versions in memory or on disk, it’s usually still worth decoding to the most appropriate image dimensions to save space.
注意:解码时使用2的方幂设置inSampleSize,速度和效率都更好一些
然而,如果你打算缓存调整过的图片版本到内存或者到磁盘中,通常还是需要解码到最合适的图片大小来节省空间
To use this method, first decode with inJustDecodeBounds set to true, pass the options through and then decode again using the new inSampleSize value and inJustDecodeBounds set to false:
使用这个方法,首先设置inJustDecodeBounds=true来解码,传递这个选项,然后使用新的inSampleSize值并且设置inJustDecodeBounds=false再解码一次
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 large size into an ImageView that displays a 100x100 pixel thumbnail, as shown in the following example code:
这个方法使得加载一个任意大小的图片到一个ImageView显示一个100x100像素的缩略图变得容易,就像下面示例代码展示的那样
mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
You can follow a similar process to decode bitmaps from other sources, by substituting the appropriate BitmapFactory.decode* method as needed.
根据需要通过替换恰当的BitmapFactory.decode*方法,你可以效仿出一个简单的过程来从其他资源中解码bitmap
原文地址如下,英文水平实在有限,希望拍砖同时能给予指正。
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html