Android ListView pictures asynchronous load and picture memory cache
Source: Internet
Author: User
<span id="Label3"></p><p><p>Developing Android apps often requires handling picture loading Issues. Because pictures are generally stored on the server side, need to be networked to load, and this is a more time-consuming process, so Android is to open an asynchronous thread to Load. In order to increase the user experience, to the user to save traffic, generally load the picture first cached down, The next time you load it will not need to network to the server side Load. The picture cache is generally divided into a primary cache (memory Cache) and a level two cache (that is, the disk cache). Only one level cache is spoken Here.</p></p><p><p>The memory cache is to put the loaded images first in the phone memory, and so on the next time the load from the memory Out.</p></p><p><p>the <strong>Advantage</strong> is that the speed is fast, the <strong>disadvantage</strong> is that it cannot be saved long, and the memory cache is recycled after the user exits the Application. and loading too much will throw java.lang.OutOfMemory Exceptions.</p></p><p><p>The disk cache is where the loaded images are placed on the Phone's internal memory card or SD Card. The next time you load it, take it from the Inside.</p></p><p><p>the <strong>Advantage</strong> is to be able to save for a long time, the <strong>disadvantage</strong> is slow, in order not to affect the user experience, but also generally through the asynchronous thread to fetch the cache image on the disk, there is a user uninstall the application, these cache files will not be deleted, the User's disk space is wasted, User needs to be deleted manually.</p></p><p><p></p></p><p><p>Image caching Many people use soft-referenced softrreference to handle it, but Android is not recommended.</p></p><p><p>Android official reasons Are: Note:in the past, a popular memory cache implementation <code>SoftReference</code> is a or <code>WeakReference</code> bitmap cache, however this I s not recommended. Starting from Android 2.3 (API level 9) The garbage collector are more aggressive with collecting soft/weak references whic H makes them fairly ineffective. In addition, prior to Android 3.0 (API level one), the backing data of a bitmap was stored in native memory which are not re Leased in a predictable manner, potentially causing the application to briefly exceed its memory limits and Crash.</p></p><p><p></p></p><p><p>So we generally use the LRUCache class, as this class can understand: it is a memory cache object, and your picture is in this class. (everything in Java is an Object) this class is only available in API 12, but fortunately Android officially adds it to the V4 package, so you can use this class after 1.6. This class is also relatively simple to use (the official documentation is also very detailed).</p></p><p><p></p></p><p><p>Here's How I use It:</p></p><p><p>The first is to customize a adapter (inherited from Baseadapter) to the listview, and then declare a member variable LRUCache in the class, not in the GetView () method. Because all item items in a ListView can only maintain this one lrucache. LRUCache has a constructor method LRUCache (int maxSize), which is the maximum capacity (in bytes) of this cache space. Affirm that a LRUCache object generally needs to replicate its sizeof (K key, V Value) in this way to change the size of each cache entry. Android source code in the default is to return 1, said by default is calculated by the Number. (in fact, I do not understand here, have to understand the friends also look at the Liberal Enlighten. )</p></p><p><p></p></p><p><p>Source code for Android sizeof ():</p></p><pre><pre><span style="color: #008000;">/*</span> <span style="color: #008000;">* * Returns the size of the entry for {@code key} and {@code value} in * user-defined units. The default implementation returns 1 so, size * is the number of entries and max size is the maximum number of ent Ries. * * <p>an entry ' s size must </span> not change while it's in the Cache. <span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">int</span> <span style="color: #000000;">sizeOf (K key, V value) { </span><span style="color: #0000ff;">return</span><span style="color: #800080;">1</span><span style="color: #000000;">; }</span></pre></pre><p><p></p></p><p><p><br>But I wrote it by reference to the official Document.</p></p><p><p></p></p><p><p></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Final<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>MaxMemory = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) Runtime.getruntime (). maxmemory ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the maximum memory allocated by the current application</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Final<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>CacheSize = maxmemory/<span style="color: #800080;"><span style="color: #800080;">5</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">It's a <span style="color: #008000;">5-point one for the picture Cache.</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>lrucache<string, bitmap> Mlrucache =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>lrucache<string, bitmap><span style="color: #000000;"><span style="color: #000000;">(cacheSize) {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>SizeOf (String key, Bitmap Bitmap) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">replication sizeof () method</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">replaced by GetByteCount () in API</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Bitmap.getrowbytes () * Bitmap.getheight ()/<span style="color: #800080;"><span style="color: #800080;">1024x768</span></span>;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Here's How many kilobytes to Count.</span></span><span style="color: #000000;"><span style="color: #000000;"> } };</span></span></pre><p><p></p></p><p><p>Then write a class asyncimageloader that asynchronously loads the picture; This class uses Asynctask to load images asynchronously.</p></p><p><p></p></p><p><p></p></p><pre><span style="color: #000000;">package <span style="color: #000000;">Com.folyd.tuan.util;import Java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.url;import Android.graphics.bitmap;import android.graphics.BitmapFactory;</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * Simple picture Loading tool class, This class does not involve the picture Cache. * @author Folyd *</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Simpleimageloader {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmap getbitmap (String Urlstr) throws Ioexception{Bitmap Bitmap; URL URL</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">URL (urlstr); HttpURLConnection Conn</span></span>=<span style="color: #000000;"><span style="color: #000000;">(httpurlconnection) url.openconnection (); Conn.setrequestmethod (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">GET</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); Conn.setreadtimeout (</span></span><span style="color: #800080;"><span style="color: #800080;">5</span></span>*<span style="color: #800080;"><span style="color: #800080;"></span> +</span><span style="color: #000000;"><span style="color: #000000;">); Conn.setdoinput (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); Conn.connect (); InputStream</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> is</span>=<span style="color: #000000;"><span style="color: #000000;">Conn.getinputstream (); Bitmap</span></span>= Bitmapfactory.decodestream (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> is</span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> is</span><span style="color: #000000;"><span style="color: #000000;">. Close (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">bitmap; }}</span></span></pre><p><p></p></p><p><p></p></p><p><p>Simpleimageloader's Getbitmap method is a simple way to get pictures from the Internet.</p></p><p><p></p></p><pre><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* * * * <span style="color: #008000;">@param urlstr The URL of the image to be loaded, in string form, You can use this URL as the key to the cached image @param image ImageView control</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">loadbitmap (String urlstr, ImageView image) {asyncimageloader asyncloader</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Asyncimageloader (image, mlrucache);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">What an asynchronous picture loads an object</span></span>Bitmap Bitmap = Asyncloader.getbitmapfrommemorycache (urlstr);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">first get the picture from the memory cache</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(bitmap! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {image.setimagebitmap (bitmap);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If this image is present in the cache, it is set directly to ImageView</span></span>}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{image.setimageresource (r.drawable.thum);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">otherwise set the default picture first</span></span>Asyncloader.execute (urlstr);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">then execute the asynchronous task Asycntask to load the picture online</span></span><span style="color: #000000;"><span style="color: #000000;"> } }</span></span></pre><p><p></p></p><p><p><br>Then encapsulate a method of loading the picture in a custom adapter. Here are some of the Asynctask's knowledge to view Official document Learning.</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View plaincopyprint? <ol class="dp-j" start="1"> <li class="alt"><li class="alt"><span class="comment">/**</span></li></li> <li><li><span class="comment">*</span></li></li> <li class="alt"><li class="alt"><span class="comment">* @param urlstr The URL of the image to be loaded, in string form, You can use this URL as the key to cache the image</span></li></li> <li><li><span class="comment">* @param image ImageView Control</span></li></li> <li class="alt"><li class="alt"><span class="comment">*/</span></li></li> <li><li><span class="keyword">private <span class="keyword">void LoadBitmap (String urlstr, ImageView image) {</span> </span></li></li> <li class="alt"><li class="alt">Asyncimageloader Asyncloader = <span class="keyword">new Asyncimageloader (image, mlrucache); <span class="comment"> What an asynchronous picture loads an object</span> </span></li></li> <li><li>Bitmap Bitmap = Asyncloader.getbitmapfrommemorycache (urlstr); <span class="comment">//get the picture from the memory cache first</span></li></li> <li class="alt"><li class="alt"><span class="keyword">if (bitmap! = <span class="keyword">Null) {</span> </span></li></li> <li><li>Image.setimagebitmap (bitmap); <span class="comment">//if This image is present in the cache, it is set directly to ImageView</span></li></li> <li class="alt"><li class="alt">} <span class="keyword">Else {</span></li></li> <li><li>Image.setimageresource (r.drawable.thum); <span class="comment">//otherwise Set the default picture first</span></li></li> <li class="alt"><li class="alt">Asyncloader.execute (urlstr); <span class="comment">//then perform asynchronous task Asycntask to load pictures online</span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt">}</li></li> </ol><p><p></p></p><p><p></p></p><p><p>The above LoadBitmap () method is then called in the GetView () method method of the custom Adapter.</p></p><p><p></p></p><p><p>This is an internship with the image asynchronous loading and memory Caching.</p></p><p><p>Take a look:</p></p><p><p></p></p><p><p></p></p><p><p>of course, a ListView without a disk cache is absolutely no good. The ListView also has a lot of skills that need to be optimized to keep learning. For example, pictures Lazy loading.</p></p><p><p></p></p><p><p>Android ListView pictures asynchronous load and picture memory cache</p></p></span>
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