Many people complain that a large number of images are loaded in the listview on android, and other pages are loaded with larger images than those in the listview. A large number of different types of images are combined, and the user experience must be taken into account, at this time, applications often use OOM, which is hard to worry about. Below are two methods that can be tested with strong strength for your reference. Correct me:
Solution 1: simple linked list + soft Reference Management
1. Define a single-instance class (e.g. EstoreCacheManager) and initialize it as a hungry man;
2. Define a LinkedHashMap and soft reference to bind the linked list and soft reference:
(
Private SoftReference mImageReference;
Private LinkedHashMap mImageMap;
MImageMap = new LinkedHashMap ();
MImageReference = new SoftReference (mImageMap );
}
3. When performing the insert operation, determine whether the size of LinkedHashMap exceeds a set value (e.g.: 200), delete the oldest one, and then insert it;
4. Capture exceptions when downloading images:
{
Try {
MBitmap = loadImageByUrl (url );
} Catch (OutOfMemoryError e ){
// The VM does not always free-up memory as it shocould,
// So manually invoke the garbage collector
// And try loading the image again.
System. gc ();
MBitmap = loadImageByUrl (url );
}
}
Solution 2: complex LRU Method management (implemented by modifying and optimizing an open-source Library)
For more information, see the demo project and source code I wrote for a new one:
Http://code.google.com/p/androidforyouku
From androidzhaoxiaogang's column