Image Memory Optimization for Android mobile phone Development

Source: Internet
Author: User

1. Convert the image into a thumbnail and load it again:

The Code is as follows:
1 BitmapFactory. Options options = new BitmapFactory. Options ();
2 options. inSampleSize = 2;
3 Bitmap img = BitmapFactory. decodeFile ("/sdcard/1.png", options );

This code is used to read the thumbnail of 1.png. The length and width are only 1/2 of the original image. The image size is reduced, and the memory occupied is naturally reduced. The disadvantage of this is that the image quality is deteriorated. The larger the value of inSampleSize, the worse the image quality. Because mobile phone manufacturers have different Image Scaling algorithms, the image quality on different mobile phones may be different. I have encountered a situation where the quality is acceptable after the moto mobile phone is scaled. Samsung mobile phones have the same scaling ratio, but the quality is much worse.

2. Use the ARBG_4444 color mode to load the image:

There are four types of Android:

ALPHA_8: each pixel occupies 1 byte of memory

ARGB_4444: each pixel occupies 2 bytes of memory

ARGB_8888: each pixel occupies 4 bytes of memory

RGB_565: each pixel occupies 2 bytes of memory

The default color mode for Android is ARGB_8888. This color mode features the most delicate colors and the highest display quality. However, the memory is also the largest.

The Code is as follows:
1 BitmapFactory. Options options = new BitmapFactory. Options ();
2 options. inPreferredConfig = Bitmap. Config. ARGB_4444;
3 Bitmap img = BitmapFactory. decodeFile ("/sdcard/1.png", options );

The preceding code is used to read 1.png in ARGB_4444 mode. Although the memory reduction is not as obvious as the first method, most images cannot see any difference with the ARGB_8888 mode. However, when reading images with gradient effects, there may be color bars. In addition, it will affect the special effect processing of the image.

3. Call the recycle () method of the image:

This is not actually a way to actually reduce the image memory. The main purpose is to mark the image object to facilitate the collection of local data of the image object. The local data of the image object occupies the largest memory and is calculated separately from the Java memory of the program. Therefore, Java heap is often enough, and the image has an OutOfMemoryError. This method can be called when the image is not in use, effectively reducing the peak value of the local data of the image, thus reducing the probability of OutOfMemoryError. However, the image object that calls recycle () is in the "obsolete" state, which may cause a program error. Therefore, this method is not recommended when it cannot guarantee that the image object will never be called again. Note that image objects that have been allocated to the control using the setImageBitmap (Bitmap img) method may be called by the system class library and cause program errors.

4. How to change the color mode of an image zoomed in with a Matrix object:

Although using a Matrix object to enlarge an image will consume more memory, sometimes you have to do the same. The enlarged image uses the ARGB_8888 color mode, even if the original image is in the ARGB_4444 color mode, and there is no way to directly specify the color mode when you zoom in. You can change the image color mode in the following ways.

The Code is as follows:
Matrix matrix = new Matrix ();
Float newWidth = 200; // The enlarged width of the image.
Float newHeight = 300; // The enlarged image Length
Matrix. postScale (newWidth/img. getWidth (), newHeight/img. getHeight ());
Bitmap img1 = Bitmap. createBitmap (img, 0, 0, img. getWidth (), img. getHeight (), matrix, true); // The enlarged image is obtained.
Img2 = img1.copy (Bitmap. Config. ARGB_4444, false); // obtain the image in the ARGB_4444 color mode.
Img = null;
Img1 = null;

Here an additional image object img1 is generated compared to the original image. However, the system automatically recycles img1, so the actual memory is reduced.

 

Related Article

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.