Android Bitmap Caching Bitmaps (slag translation)

Source: Internet
Author: User

Three, cached images

1. Use memory cache
Memory cache provides quick access to images with increased usage of app memory. The
advocates the use of LRUCache to refer to the image (which was already available in the API4 Support library), caches LRUCache with a strong reference linkedhashmap, keeps up to date with the most recent LRUCache, Remove the last used LRUCache to avoid memory overflow.
In the past, the use of SoftReference or weakreference to refer to the image, however, at the beginning of API 9 (Android 2.3), the resource collector has increased the recovery of their efforts, resulting in them quite invalid.

In order to create a lrucache of the right size, there are a number of factors to consider:
A, how much free memory your Activity/app has left behind (not reaching the app's memory limit)
B, how many pictures are loaded on the screen at once , need (in memory) to prepare how many available pictures to load on the screen, so that the screen first display a portion of the loaded pictures, and then continue to load other non-loaded pictures, so that users can do something to dry rather than long wait for
C, screen size and device display density; load the same number of images into the cache, High display density device xhdpi (such as Galaxy Nexus) consumes more memory than a lower display density device hdpi (such as Galaxy Nexus S) to consume more than
D, what size and configuration of images loaded into memory, and how much memory each image occupies
E, The frequency of access to the cached image, which images are accessed more frequently than others, may need to be cached more preferentially in memory for images with high frequency access, or to create multiple LRUCache
D, balance quality and quantity; sometimes, Caching a large number of low-quality images is better than opening a thread to load a high-quality image.

There is basically no specific size or guideline to use for all applications, which can only be determined by your analysis of usage and the idea of a suitable solution. After all, loading a useless cache that is too small can only be wasteful, but too large may cause java.lang.OutOfMemory
Exceptions or Low app memory (not reaching the app's memory limit)

First you need to get the maximum memory value available to the VM (MB). Exceeding this value triggers the outofmemory exception, and it is important to note that the image stored in LRUCache is calculated in kilobytes and remembers the unit conversion

when calculating the storage size.

Final intMaxMemory = (int) (Runtime.getruntime (). Maxmemery ()/1024);//Suppose you go to 1/8 maxmemory to cache the image .Final intCacheSize = MAXMEMORY/8; LruCache<string, bitmap> Mmemorycache =NewLrucache<string, bitmap>(cacheSize) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {//The cache size would be measured in kilobytes rather than//Number of items.        returnBitmap.getbytecount ()/1024; }};//Cached Imagesif(Mmemorycache.get (key) = =NULL) {mmemorycache.put (key, bitmap);}

2. Using local cache
Memory caching is useful for quick access to recently displayed images, however you cannot rely too much on these images that have been loaded in memory. Components such as the GridView can easily fill the memory cache when loading large amounts of data.
Your app will be placed in the background due to an interception of another task, such as an incoming call, and your app may be killed and the cache will be recycled (the system kills and robs the goods). When your app resumes from the Onresume () method,
The app has to load every image again.














Android Bitmap Caching Bitmaps (slag translation)

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.