In Android code Development/samples has a tool class: Imagedownloader, which is the role of downloading images from the Web display to a given imageview. The text is as follows:
This helper class download images from the Internet and binds those with the provided ImageView.
Here is an explanation of how this tool class is implemented, one of the following.
(1) First, he used a strong reference (strongreference) and a soft reference (SoftReference) to save the downloaded picture (bitmap), the practice is: strongreference to save a certain amount of pictures (bitmap), When this capacity is exceeded, it is moved into the softreference to be saved.
One of the strong strongreference here to save the picture (bitmap) is actually implemented using Linkedhashmap! As shown in the following code:
private Final static hashmap<string, bitmap> Shardbitmapcache = new linkedhashmap<string, bitmap> (HARD_CACHE_CA PACITY/2, 0.75f, True) {//private static final long serialversionuid = -7190622541619388252l; @Override protected Boolean removeeldestentry (Map.entry<string, bitmap> eldest) {if (size () > hard_cache_capacity) {//Entries push-out of hard reference cache is transferred to soft reference cache Ssoftbitmapcache.put (Eldest.getkey (), New softreference<bitmap> (Eldest.getvalue ())); return true; } else {return false; } } };
The above code defines a linkedhashmap with a capacity of HARD_CACHE_CAPACITY/2 (the hard_cache_capacity here can be defined according to your actual situation, which is defined as 40), And the Linkedhashmap is used to sort the growth rate at 0.75. The most important thing is to write removeeldestentry this way, so that the last element will be moved into the Ssoftbitmapcache once the capacity is exceeded. The ssoftbitmapcache here is actually a hashmap. That Ssoftbitmapcache is a what hashmap, here first sell a Xiaoguanzi.
It says that when you save a picture resource, you use the MAP's key-value pair: map.entry<string, bitmap> here is the path to the image we need to download (that is, the network address).
(2) Each time you need to get a picture of the first to detect whether Shardbitmapcache and Ssoftbitmapcache already exist, Otherwise you need to open a task to download and process in the background. The background processing here is actually implemented using the asynctask that are commonly used. Here's a look at the entry method for a imageview download image:
private void Forcedownload (string url, ImageView ImageView, string cookie) { //state Sanity:url are guaranteed to Nev ER is null in downloadeddrawable and cache keys. if (url = = null) { imageview.setimagedrawable (null); return; } if (cancelpotentialdownload (URL, ImageView)) { Bitmapdownloadertask task = new Bitmapdownloadertask (imageView); Downloadeddrawable downloadeddrawable = new downloadeddrawable (Task); Imageview.setimagedrawable (downloadeddrawable); Task.execute (URL, cookie); } }
The above code needs to explain downloadeddrawable and Bitmapdownloadertask, where Downloadeddrawable is a colordrawable, which can actually define a pre-existing image.
Static Class Downloadeddrawable extends Colordrawable { private final weakreference<bitmapdownloadertask> bitmapdownloadertaskreference; Public downloadeddrawable (Bitmapdownloadertask bitmapdownloadertask) { super (color.black); Bitmapdownloadertaskreference = new weakreference<bitmapdownloadertask> (Bitmapdownloadertask); } Public Bitmapdownloadertask Getbitmapdownloadertask () { return bitmapdownloadertaskreference.get (); } }
In fact Downloadeddrawable also caches his own bitmapdownloadertask and knows what to use later.
Bitmapdownloadertask is actually a asynctask, In the method Doinbackground, androidhttpclient is used to download the image data from the Internet and save it as bitmap. And then handle the bitmap in OnPostExecute.
protected void OnPostExecute (Bitmap Bitmap) { if (iscancelled ()) { Bitmap = null; } ADD bitmap to cache if (bitmap! = null) { synchronized (shardbitmapcache) { shardbitmapcache.put (URL, bitm AP); } } if (imageviewreference! = null) { ImageView ImageView = Imageviewreference.get (); Bitmapdownloadertask bitmapdownloadertask = Getbitmapdownloadertask (ImageView); Change bitmap only if the process is still associated with the IT if (this = = Bitmapdownloadertask) { ImageView. Setimagebitmap (bitmap);}}}
The above code first caches the obtained bitmap inside the Shardbitmapcache. Then find the corresponding ImageView and set the bitmap display.
(3) We now think of a problem: Generally speaking, the memory of the mobile phone is tense, here Shardbitmapcache and Ssoftbitmapcache due to the loading of a large number of picture resources, it is inevitable that there is no above problems, The most important thing is that both Shardbitmapcache and Ssoftbitmapcache are static variables. So there's no need to keep the data in practice, so you need to clear it if you don't need it.
(4) finally see Ssoftbitmapcache what HashMap can. It says that the bitmap load actually uses the Asynctask multithreading method, Therefore, it is very likely that more than one modification of concurrent operations is possible. So the use of Concurrenthashmap is the most suitable. Concurrenthashmap uses the lock separation technique to solve multi-operation concurrency and is thread-safe.
Soft cache for bitmap kicked out of hard cache private final static concurrenthashmap<string, softreference<b itmap>> Ssoftbitmapcache = new concurrenthashmap<string, softreference<bitmap>> (HARD_CACHE_ CAPACITY/2);
Android Code parsing image cache (Imagedownloader)