Android Art--bitmap efficient Loading and caching (1)

Source: Internet
Author: User

through Bitmap We can design a imageloaderthat implements the functions that should be:

Synchronous loading of images, asynchronous loading of images, compression of images, memory caches, disk caches, network access;

1. Loading

First mention loading: the Bitmapfactory class provides four classes of methods:decodefile,decoderesource,decodestream and decidebytearray. The file system, resource, input stream, and number of bytes are loaded Bitmap objects, respectively .

2. Compression

How do I compress a picture? First of all, why do we compress pictures? Because many times the ImageView size is smaller than the original size of the picture, no need to load the original image into memory, may cause oom, and ImageView There's no way to show the original picture all, sample compression will shrink the picture in ImageView while reducing the memory footprint of the image to avoid Oom , Improve Bitmap load performance. picture memory is too general to affect the user experience, and the storage of the device is also a waste of resources. How to compress? The image is scaled by bitmapfactory.options, mainly the insamplesize parameter, called sample rate. insamplesize=1just like the original . ,insamplesize=4, after loading the picture length and width is the original picture length and width of the Quarter, the number ofpixels is the original picture of the 1/16, The memory consumed after loading is also the 1/16 of the original image .

These steps are:

( 1 Span style= "font-family:arial" >bitmapfactory.options injustdecodebound true

(2) from bitmapfactory.options outwidth and outheight

(3) combining target view (ImageView) the desired image size, calculate the sampling rate;

(4) Set the injustdecodebounds of bitmapfactory.options to falseand reload.

3. Caching

where the memory cache and disk cache is the most core, the cache is for the mobile service, for example: There are a large number of network pictures, need to download to display in the user interface, if the PC can be downloaded directly to the local display, but on the mobile side we all need a lot of traffic to download, in order to solve the problem of traffic consumption, the cache is our only choice, with the cache we from the network after the first network load, the picture is cached in our device, When we look at the picture again, we don't have to get it from the network again, but first get it from the device's cache, and if not, consider getting it from the network, which is the caching policy.

at present, our common cache algorithm is The LRU algorithm, which is the least recently used algorithm, is the key to eliminate the least recently used cache objects if the cache is full. Speaking of the two caches of LRU:lrucahe and Disklrucahe, the former is used to implement the memory cache, The latter acts as a cache of storage devices.

this time, I introduced Bitmap loading and caching, next time we further analyze the specific code.

Android Art--bitmap efficient Loading and caching (1)

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.