Android advanced exercise-efficient display of Bitmap (efficient loading of large bitmaps)

Source: Internet
Author: User

Efficiently load large bitmaps
Images have various shapes and sizes. In many cases, the actual image size is much larger than the image size displayed in the application. For example
Gallery
The actual resolution of the photo displayed by the application is usually higher than that of the mobile phone device. Considering that we are developing an application with limited memory usage, ideally, we just want to load a low-resolution bitmap into the memory. Generally, the bitmap of this low-resolution version must be consistent with the actual display size of the UI component. A high-resolution image will not bring us any obvious benefits, but it will occupy valuable memory resources and generate additional performance overhead to obtain the size and type of Bitmap.

The bitmapfactory class provides several decoding methods (
decodeByteArray()
,
decodeFile()
,
decodeResource()
, Etc) from different sources.
Bitmap: how to select the most appropriate decode method depends on the source of your image data. These methods will apply for memory to build bitmap objects.
OutofmemoryException. Each type of decode method has an additional signature for you to pass

BitmapFactory.OptionsClass to specify the decoding option.
Set the injustdecodebounds attribute
TrueYou can avoid applying for memory, although
NullBitmap object, but
BitmapFactory.OptionsObject settings

outWidth
,
outHeight
And
outMimeTypeThis technology allows you to know the size and type of a bitmap object before building it.

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

Exception,
Before decoding bitmap, you need to check the bitmap size and type, unless you are very clear about the size of the bitmap you want to decoding, And the size should be suitable for the current application memory environment.
Load bitmap of 'contract version' to memory
Now we know the size of the bitmap, which will help us decide whether to load the entire bitmap or the bitmap of the 'downgrade'. There are some factors to consider here,
How much memory is expected to be used to load the entire image? 2,
Considering other memory requirements, How much memory do you Want to allocate to bitmap? 3,
Used to display bitmap

ImageView
Widget or other UI component size 4,
The screen size and density of the current device, for example, are not worth loading at all.
1024x768

Pixel images to the memory, and ultimately only in
128x96
Pixel size

ImageView
The display on the control should tell decoder that the image needs to be sampled, load a smaller bitmap to the memory, and set

BitmapFactory.OptionsObject

inSampleSizeThe property is
True. For example, a resolution is
2048x1536 pixels.
inSampleSizeSet it to 4. The final image size is approximately
512x384, the memory usage for loading is 0.75m instead of the 12 m for loading the entire image (assuming that the bitmap configuration is

ARGB_8888), The following is a calculation based on the target height and width.
Insamplesize Method

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) {        if (width > height) {            inSampleSize = Math.round((float)height / (float)reqHeight);        } else {            inSampleSize = Math.round((float)width / (float)reqWidth);        }    }    return inSampleSize;}
Note: inSampleSizeIf the value is the power of 2, it will be faster and more efficient for decoder. However, if you want to cache the size-adjusted bitmap to the memory or hard disk, it still makes sense to decoding the most suitable bitmap size, which helps to save memory or hard disk space.
The following is a method for obtaining bitmap
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 can easily load a bitmap in the UI component of any display size.

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

In other
BitmapFactory.decode*

In the series of decode methods, the above technologies for obtaining bitmap are also required.






 

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.