Displaying Bitmaps Efficiently系列
- Loading Large Bitmaps Efficiently
- Processing Bitmaps Off the UI Thread
- Caching Bitmaps
- Managing Bitmap Memory
- Displaying Bitmaps in Your UI
Caching Bitmaps(缓存)
Use a Memory Cache
The LruCache class is particularly well suited to the task of caching bitmaps, keeping recently referenced objects in a strong referenced LinkedHashMap(强引用) and evicting the least recently used member before the cache exceeds its designated size.
如何选择一个合适的LruCache大小
- How memory intensive is the rest of your activity and/or application?
- How many images will be on-screen at once? How many need to be available ready to come on-screen?
- What is the screen size and density of the device? An extra high density screen (xhdpi) device like Galaxy Nexus will need a larger cache to hold the same number of images in memory compared to a device like Nexus S (hdpi).
- What dimensions and configuration are the bitmaps and therefore how much memory will each take up?
- How frequently will the images be accessed? Will some be accessed more frequently than others? If so, perhaps you may want to keep certain items always in memory or even have multiple LruCache objects for different groups of bitmaps.
- Can you balance quality against quantity? Sometimes it can be more useful to store a larger number of lower quality bitmaps, potentially loading a higher quality version in another background task.