Android mobile phone development of picture memory optimization points

Source: Internet
Author: User
Tags config

1, the picture into a thumbnail and then load:

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 read 1.png thumbnail, length, width is only 1/2 of the original picture. The size of the picture is reduced and the memory footprint is naturally smaller. The disadvantage of this is that the picture quality is bad, the larger the value of the insamplesize, the worse the quality of the picture. Because each handset manufacturer scales the picture the algorithm to be different, on the different handset's scaling picture quality may be different. The author has experienced Moto mobile phone image scaling after the quality can be accepted, Samsung mobile phones on the same scaling ratio, quality is a lot worse.

2. Load the picture with arbg_4444 color mode:

There are four kinds of Android, respectively:

Alpha_8: Consumes 1byte of memory per pixel

argb_4444: Consumes 2byte of memory per pixel

argb_8888: Consumes 4byte of memory per pixel

rgb_565: Consumes 2byte of memory per pixel

The Android default color mode is argb_8888, the color mode is the most exquisite color, display the highest quality. But again, the memory footprint is 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 above code reads 1.png in argb_4444 mode. Memory reduction Although not as obvious as the first method, for most pictures, there is no difference between the argb_8888 mode and the model. However, when you read a picture with a gradient effect, a color bar may appear. In addition, will affect the image of the special effects processing.

3, call the picture Recycle () method:

This is actually not a real way to reduce the memory of the picture. The main purpose is to mark the object of the picture to facilitate the collection of local data of the image object. The picture object's local data occupies the largest amount of memory and is calculated separately from the memory in the program's Java section. So often appear Java heap enough to use, and the picture occurs outofmemoryerror situation. Calling this method when the picture is not in use can effectively reduce the peak value of the local data in the picture, thereby reducing the probability of outofmemoryerror. However, a picture object that calls Recycle () is in an "obsolete" state, causing a program error when invoked. Therefore, it is not recommended to use this method when there is no guarantee that the picture object will never be called again. In particular, the picture object that has been assigned to the control with the SETIMAGEBITMAP (Bitmap img) method may be invoked by the System class library, causing a program error.

4, using the matrix object to enlarge the picture how to change the color mode:

Even if you use the matrix object to enlarge the picture, it will consume more memory, but sometimes you have to. The enlarged image uses the argb_8888 color mode, even if the original picture is argb_4444 color mode, and there is no way to directly specify the color mode when zooming in. You can change the picture color mode using the following methods.

The code is as follows
Matrix matrix = new Matrix ();
Float newwidth = 200;//width of picture enlarged
float newheight = 300;//length of image enlarged
Matrix.postscale (Newwidth/img.getwidth (), Newheight/img.getheight ());
Bitmap IMG1 = Bitmap.createbitmap (img, 0, 0, img.getwidth (), Img.getheight (), Matrix, true);/get enlarged picture
Img2 = Img1.copy (Bitmap.Config.ARGB_4444, false)//Picture with argb_4444 color mode
img = NULL;
IMG1 = null;

This creates an additional image object img1 than the original picture. But the system automatically reclaims the 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.