This is a class diagram based on Imageloader, which helps us to better understand this open source library.
The advantages of this open Source Library: 1, support multi-threaded download pictures. 2, achieve the picture of the level two cache.
3. Bitmap can be cropped according to the size of the control, reducing the bitmap consuming too much memory
4, to provide on the slower network to load the picture
5, better control the loading process of the picture, for example, the slide process to pause loading pictures, stop sliding when
To load the picture.
The getinstance () method inside the Imageloader uses a singleton design pattern. Improves performance by determining whether a double layer is null.
Imageloaderconfigurationfactory used the factory model.
Limitedagememorycache is a memorycache decorator, which is equivalent to adding a feature to MemoryCache.
Contentlengthinputstream is the decorator of InputStream, can get the length of InputStream corresponding data source through the available () function
Imageloaderconfiguration inside the threadpollsize set is the size of the thread pool, set here is 3
In the project to use the ListView display pictures of the module, the usual load of images are through the httpurlconnection to make network requests, and then download pictures and display on the activity, but this method for
ListView is obviously unrealistic, so in the network found an open source library Imageloader, in addition to the implementation of basic functions, but also implemented a picture cache (level three cache, memory, SDcard,
Network (in fact, really only two level)), read the other people on the Imageloader, the realization of the principle of Imageloader also understand a lot, make a note, you can stay for later review, by the way to consolidate under
The knowledge learned.
The least accessed implementation leverages two generics, a value that stores key and bitmap types, and a long value for the other store key, which iterates when it needs to be deleted and gets the fewest
The key to be used is then removed to the corresponding value. The same is true of Limitedagememorycache.
This library load image is also the use of httpclient, network delay when the exception thrown, the network is slow to call Slownetworkimagedownloader, it implements the Imagedownloader interface,
Not always trying to figure out how to implement caching? How to realize the LRU and FIFO, later learned that the original is the use of linklist and linkhashmap,linklist can be used as a queue,
So it is easy to implement the first to remove the function, and Linkhashmap has the function of the iterative sorting, the default is the insertion sort, there is another kind of access to the order, is the access to the picture
It will store its key at the end of the table and delete it from its original location, and deleting the first one every time it deletes the image that was not accessed for the longest time.
public class Lrumemorycache implements Memorycacheaware<string, bitmap> {private Final linkedhashmap<string, Bitmap> map;//uses Linkhashmap to store the corresponding data private final int maxsize;/** Size of this cache in bytes */private int size;/** @pa Ram MaxSize Maximum sum of the sizes of the Bitmaps in this cache */public lrumemorycache (int maxSize) {if (MaxSize <= 0) {throw new IllegalArgumentException ("maxSize <= 0");} This.maxsize = Maxsize;this.map = new linkedhashmap<string, bitmap> (0, 0.75f, true);} /** * Returns the Bitmap for {@code key} if it exists in the cache. If a BITMAP was returned, it's moved to the head * of the queue. This returns null if a Bitmap are not cached. */@Overridepublic final Bitmap get (String key) {if (key = = null) {throw new NullPointerException ("key = = null");} Synchronized (this) {return map.get (key);}} /** Caches {@code Bitmap} for {@code key}. The Bitmap is moved to the head of the queue. */@Overridepublic Final Boolean put (String key, Bitmap value) {//save key and value in if (key = = NULL | | value = = NULL) {throw new NullPointerException ("key = = NULL | | Value = = null ");} Synchronized (this) {size = SizeOf (key, value); Bitmap previous = Map.put (key, value), if (previous! = null) {size = SizeOf (key, previous);}} TrimToSize (maxSize); return true;} /** * Remove The eldest entries until the total of remaining entries are at or below the requested size. * * @param maxSize The maximum size of the cache before returning. May be-1 to evict even 0-sized elements. */private void trimtosize (int maxSize) {while (true) {String key; Bitmap value;synchronized (This) {if (Size < 0 | | (Map.isempty () && size! = 0)) {throw new IllegalStateException (GetClass (). GetName () + ". SizeOf () is reporting inconsistent results!");} if (size <= maxSize | | map.isempty ()) {break;} map.entry<string, bitmap> toevict = Map.entryset (). iterator (). Next (); if (toevict = = null) {break;} Key = Toevict.getkey (); value = Toevict.getvalue (); Map.Remove (key); Size-= SizeOf (Key,value);}}} /** removes the entry for {@code key} if it exists. *///removes the corresponding key (actually the first element) from @overridepublic final void remove (String key) {if (key = = null) {throw new NullPointerException (" Key = = null ");} Synchronized (this) {Bitmap previous = Map.Remove (key); if (previous! = null) {size = SizeOf (key, previous);}} @Overridepublic collection<string> keys () {synchronized (this) {return new hashset<string> (Map.keyset ())}} @Overridepublic void Clear () {trimtosize ( -1);//-1 would evict 0-sized elements}/** * Returns the size {@code Bitmap} in B Ytes. * <p/> * An entry's size must not change while it's in the cache. */private int sizeOf (String key, Bitmap value) {return value.getrowbytes () * Value.getheight ();} @Overridepublic Synchronized Final String toString () {return String.Format ("lrucache[maxsize=%d]", maxSize);}}
Imageloader Study Notes