Memory Optimization: large image display

Source: Internet
Author: User

Java code
Public Bitmap optimizeBitmap (byte [] source, int maxWidth, int maxHeight ){
Bitmap result = null;
Int length = source. length;
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inJustDecodeBounds = true;
Result = BitmapFactory. decodeByteArray (source, 0, length, options );
Int widthRatio = (int) Math. ceil (options. outWidth/maxWidth );
Int heightRatio = (int) Math. ceil (options. outHeight/maxHeight );
If (widthRatio> 1 | heightRatio> 1 ){
If (widthRatio> heightRatio ){
Options. inSampleSize = widthRatio;
} Else {
Options. inSampleSize = heightRatio;
}
}
Options. inJustDecodeBounds = false;
Result = BitmapFactory. decodeByteArray (source, 0, length, options );
Return result;
}
 
 
The memory of Android Emulator is only 8 Mb. When you need to display more large charts, it is easy to throw the "bitmap size exceeds VM budget" exception.
BitmapFactory. Options public boolean member variable inJustDecodeBounds. If the value is set to true, the decoder returns NULL. We can query the image without loading the memory.
In the sample code, we use the Options object instance options to obtain the width and height of the image.
The BitmapFactory. Options public int member variable inSampleSize is used to set the image scale-down. For example, when inSampleSize is set to 4, the encoder returns the original 1/4 size image.
 
Note: When the encoder needs to return an image, set the value of inJustDecodeBounds to false.

Author "Dyingbleed"
 

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.