Effectively load Large-size Bitmaps (Loading Large Bitmaps Efficiently)

Source: Internet
Author: User

Effectively load Large-size Bitmaps (Loading Large Bitmaps Efficiently)

Effectively load Large-size Bitmaps (Loading Large Bitmaps Efficiently)

The image has different shapes and sizes. In most cases, their actual size is much larger than what needs to be presented. For example, the system's Gallery program will display the images you have taken using the camera device, but the resolutions of those images are usually much higher than those of your device's screen.

Considering that the program is working in limited memory, it is ideal that you only need to load a low-resolution version in the memory. This low-resolution version should match your UI size to facilitate display. A high-resolution image will not provide any visible benefits, but will occupy valuable (precious) memory resources, and cause (incurs) additional efficiency problems when moving images quickly.

This section describes how to load a scaled-down image to decoding large bitmaps in the memory to avoid exceeding the program memory limit.

Read Bitmap Dimensions and Type)

The BitmapFactory class provides some decode methods (decodeByteArray (), decodeFile (), decodeResource (), etc .) creates a Bitmap from different resources. select an appropriate decode method based on your image data source. when constructing bitmap, these methods will try to allocate memory, which may easily lead to an OutOfMemory exception. Each decode method provides BitmapFactory. Options to set some additional tags to specify the decode Options. Setting the inJustDecodeBounds attribute to true can avoid memory allocation during decoding. It returns a null bitmap, but outWidth, outHeight, and outMimeType can still be obtained. This technology allows you to read the image size and type before constructing a bitmap.

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

Options. inJustDecodeBounds = true;

BitmapFactory. decodeResource (getResources (), R. id. myimage, options );

Int imageWidth = options. outWidth;

String imageType = options. outMimeType;

To avoid java. lang. outOfMemory exception, we need to check its size before the real decode image, unless you are sure that this data source provides an accurate picture and does not cause excessive memory usage.

Load a Scaled-Down Version to the Memory (Load a Scaled Down Version into Memory)

We have learned the image size through the above steps. The data can be used to determine whether to load the entire image to the memory or to load a reduced version. Consider the following factors:

Evaluate the memory used to load the complete image.

When the program loads this image, it will involve other memory requirements.

The size of the widget that displays the image.

The screen size and the screen density of the current device.

For example, if you display an image with an image of 1024x768 pixel to a thumbnail with an image of 128x96 pixel in ImageView, you do not need to load the entire image to the memory.

To tell decoder to load a lower version of the image to the memory, you need to set inSampleSize to true in your BitmapFactory. Options. For example, an image with a resolution of 2048x1536. If inSampleSize is set to 4, a bitmap with a resolution of x is generated. The size of the image to be loaded is only about 0.75 MB. If it is to load the full graph, it will take about 12 MB (the premise is that the bitmap configuration is ARGB_8888 ). the following Sample Code calculates the size of the Sample image based on the size of the target image:

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: The power of inSampleSize is set to 2 because decoder will eventually process down the number of non-2 power and obtain the number closest to the power of 2. For more information, see the inSampleSize document.

To use this method, you must first set inJustDecodeBounds to true, pass the options value, and then use the inSampleSize value and set inJustDecodeBounds to false to re-Decode the operation.

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

}

Using the above method, you can simply load an image of any size and display it as a thumbnail of 100*100 pixel. As shown below:

MImageView. setImageBitmap (

DecodeSampledBitmapFromResource (getResources (), R. id. myimage, 100,100 ));

You can replace the appropriate BitmapFactory. decode * method to write a similar method for decode bitmap from other data sources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.