Android Performance Optimization--Image optimization

Source: Internet
Author: User

Android Performance Optimization--Image optimization

In Android Performance optimization, we will find that the largest memory and performance impact is often the picture resource, followed by the control resources. In contrast, the impact of other resources will be smaller. Here I first on the optimization of the picture resources to explain, if there is anything wrong, I hope the great God corrected.

1, first we can take two samples of the picture, essentially reduce the memory footprint of the image. is to reduce the size of the large picture into memory, in order to achieve the purpose of reducing memory. The code is as follows:

1 //Create a thumbnail image2     PrivateBitmap Oncreatethumbnail (String filePath2,inti) {3         4         //Get Options5Bitmapfactory.options options=Newbitmapfactory.options ();6         //set to unread content, value read boundary value7options.injustdecodebounds=true;8         //by editing, the boundary value is obtained and deposited into option9 Bitmapfactory.decodefile (filepath2,options);Ten         //Get zoom ratio One         intRatiowidth= (options.outwidth)/i; A         //Assignment Zoom ratio -Options.insamplesize=Ratiowidth; -         //set the picture format to display theoptions.inpreferredconfig=config.rgb_565; -         //set to read content, -options.injustdecodebounds=false; -         //Get thumbnail image +         returnbitmapfactory.decodefile (filePath2, options); -}

Of course, there are a lot of third-party framework can quickly implement this function, such as Picasso , and so on, its use just call the method on it, quite easy (no friends, you can search on the Internet, very simple and practical framework).

2, followed by the use of three-tier cache architecture, improve the speed of image access. The three-tier cache schema is memory-file-network.

Memory is the fastest part of access but the allotted space is limited, so it is not possible to occupy too much. The memory cache can use the LRU algorithm (the least recently used algorithm) to determine which images to delete in memory and save those pictures.

Files are saved locally, either in the SD card or in the phone's internal storage.

Network is to access the network to download pictures, to load pictures.

3, the common png,JPG,webp and other format of the picture in the set to the UI before the need to pass the decoding process, But the picture uses the different code rate, also causes the memory to occupy the different. Here are a few common decoding rates:

1) The decoding rate of the alpha_8 format, at this time the image only ALPHA value, no RGB value, a pixel occupies a byte, memory footprint is the smallest, but also the most unclear one, very easy to picture distortion, not recommended to use.

2) argb_4444 format of the decoding rate, this format of the picture, looks poor quality, has not been recommended to use. It is highly recommended to use argb_8888 instead. A pixel occupies 2 bytes, an alpha (A) value, a Red (R) value, a Green (G) value, and a Blue (B) value that accounts for 4 bites. A total of 16bites, or 2 bytes

3) The decoding rate of the argb_8888 format, a pixel occupies 4 bytes, an alpha (A) value, a Red (R) value, a Green (G) value, and a Blue (B) value each account for 8 bites, a total of 32bites, or 4 bytes. This is a high-quality picture format, commonly used on the computer format. It is also the default format for a bitmap on Android phones.

4) The decoding rate of the rgb_565 format, a pixel occupies 2 bytes, no alpha (a) value, that is, transparent and translucent, Red (R) value of 5 bites, Green (G) value accounted for 6 bites, Blue (B) value accounted for 5 bites, total 16bites, That is, 2 bytes. For the translucent color of the picture, the format of the picture can achieve a better rendering effect, relative to the argb_8888 can also reduce the memory cost of half, so it is a good choice.

In general, it is recommended to use the last one, the performance is good, and there is no alpha value, the system does not have two times of rendering, can greatly improve efficiency and performance.

4, the last point, is also the most important point of picture optimization. Reuse Bitmap.

We all know that bitmap will occupy a lot of memory space, such as:

So you can reuse an existing bitmap memory area. The Inbitmap property allows you to increase the Bitmap cycle efficiency. Memory usage such as:

Using this property, you can tell the Bitmap Decoder to try to use an existing memory area, and the newly decoded Bitmap will attempt to use the previous Bitmap the pixel data memory area that is occupied in the heap , instead of asking for memory to reapply an area to store the bitmap. With this feature, even thousands of images will only need to occupy the memory size of the number of images that the screen can display.

Code:

// Get Options bitmapfactory.options Options=new  bitmapfactory.options (); Options.inbitmap=  Mcurrentbitmap;mcurrentbitmap=

There are a few limitations to using this property:

1, in the SDK and 18, the reuse of the bitmap size must be consistent, for example, to Inbitmap assignment of the image size of 100-100, then the new application bitmap must also be 100-100 to be able to be reused. Starting with SDK 19, the bitmap size of the new application must be less than or equal to the bitmap size already assigned.

2, the new application bitmap and the old bitmap must have the same decoding format, for example, everyone is 8888, if the previous bitmap is 8888, then can not support 4444 and 565 format bitmap.

Android Performance Optimization--Image optimization

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.