Android Asynchronous Load learning note four: using cache to optimize the Network load image and ListView load optimization

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:14px">If you do not do any processing, directly with the network loading pictures in the case of fast speed may not be a bad feeling, but if the use of mobile traffic or network is not good, the problem comes, or users will complain that traffic is too much, or complain that the picture loading is too slow, such as from which point of view, is not a good experience! To improve the user experience, we're going to use Caching. </span> <span style="font-size:14px">There are many ways to cache data in android, and there are more articles about it, such as</span> http://blog.csdn.net/dahuaishu2010_/article/details/17093139 <span style="font-size:14px">and http://www.jb51.net/article/38162.htm and</span> <span style="font-size:14px">so On. Learning today is the LRU Cache. </span></p></p><p><p><span style="font-size:14px">LRU (Least recently used) The least recently used algorithm, that is, under certain conditions, the <span style="font-family:Arial; line-height:26px">LRU cache is to remove the least recently used data, to the latest read Data. And the most frequently read, but also the most read, so, using the LRU cache, we can improve the efficiency of the application and user Experience. The Andorid itself provides the LRUCache class to implement this caching algorithm. </span></span></p></p><p><p><span style="font-size:14px"><span style="font-family:Arial; line-height:26px">Using the LRUCache cache in Imageloader:</span></span></p></p><p><span style="font-size:14px"><span style="font-family:Arial; line-height:26px">public class Imageloader {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private lrucache<string, bitmap> mcaches;//create LruCache Object<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private ImageView mimageview;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private ListView listview;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private Set<imageasynctask> mtask;<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@SuppressLint ("newapi")<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public Imageloader (listview Listview) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>This.listview = listView;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mtask = new Hashset<imageasynctask> ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>int maxmemory = (int) runtime.getruntime (). maxmemory ();//get maximum available memory<br><span style="white-space:pre"><span style="white-space:pre"></span></span>int cacheSize = Maxmemory/8;//maximum amount of memory to set cache data is 1/8<br><span style="white-space:pre"><span style="white-space:pre"></span></span>mcaches = new lrucache<string, bitmap> (cacheSize) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>protected int sizeOf (String key, Bitmap Value) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Return Value.getbytecount ()///every time the cache is cached, the size of the bitmap is Returned.<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>};<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@SuppressLint ("newapi")<br><span style="white-space:pre"><span style="white-space:pre"></span></span>/**<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* Increase cache data to determine if data exists before<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @description:<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @author LDM<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @date 2015-8-11 7:51:04<br><span style="white-space:pre"><span style="white-space:pre"></span></span>*/<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void setlrucaches (String url, Bitmap Bitmap) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>If (getlrucaches (url) = = Null) {//if There is no URL corresponding to bitmap in the cache, add bitmap to Mcaches<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mcaches.put (url, bitmap);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>/**<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* Get data from the cache<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @description:<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @author LDM<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @date 2015-8-11 7:51:22<br><span style="white-space:pre"><span style="white-space:pre"></span></span>*/<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@SuppressLint ("newapi")<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public Bitmap getlrucaches (String Url) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Return mcaches.get (url);//get The corresponding bitmap in the cache via a URL<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>/**<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* Get to bitmap from URL<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @description:<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @author LDM<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* @date 2015-8-11 1:55:12<br><span style="white-space:pre"><span style="white-space:pre"></span></span>*/<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public Bitmap Getbitmapbyurl (String Urlstr) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Bitmap Bitmap = null;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>InputStream is = null;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>try {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>URL url = new URL (urlstr);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>HttpURLConnection con = (httpurlconnection) url.openconnection ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>is = new Bufferedinputstream (con.getinputstream ());<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Bitmap = Bitmapfactory.decodestream (is);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Con.disconnect ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Return bitmap;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Catch (Exception E) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>E.printstacktrace ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>finally {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>try {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Is.close ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Catch (ioexception E) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>E.printstacktrace ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Return null;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void Loadimgbyasynctask (ImageView img, String Url) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageview = img;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Remove a picture from the cache<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Bitmap Bitmap = getlrucaches (url);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>if (bitmap = = Null) {//if No picture is available in, then download from the network<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageview.setimageresource (r.drawable.ic_launcher);//set Default Picture<br><span style="white-space:pre"><span style="white-space:pre"></span></span>New Imageasynctask (url). Execute (url);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Else {//cache has a picture, it is displayed directly<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageview.setimagebitmap (bitmap);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private class Imageasynctask extends asynctask<string, Void, bitmap> {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private ImageView ImageView;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private String murl;<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public Imageasynctask (String Murl) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>This.murl = murl;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Protected Bitmap doinbackground (String ... params) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Bitmap Bitmap = getbitmapbyurl (params[0]);//get pictures<br><span style="white-space:pre"><span style="white-space:pre"></span></span>If (bitmap! = Null) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Setlrucaches (params[0], bitmap);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>return Getbitmapbyurl (params[0]);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>protected void OnPostExecute (Bitmap Result) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>ImageView img = (ImageView) listview.findviewwithtag (murl);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>If (img! = null && result! = null) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Imageview.setimagebitmap (result);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mtask.remove (this);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void Setimageview (int start, int End) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>for (int i = start; i < end; i++) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>String URL = dataadapter.urls[i];<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Bitmap Bitmap = getlrucaches (url);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>if (bitmap = = Null) {//if No picture is available in, then download from the network<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Imageasynctask task = new Imageasynctask (url);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Task.execute (url);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mtask.add (task);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Else {//cache has a picture, it is displayed directly<br><span style="white-space:pre"><span style="white-space:pre"></span></span>ImageView img = (ImageView) listview.findviewwithtag (url);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Img.setimagebitmap (bitmap);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void Stopalltask () {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>If (mtask.size () >0) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>For (imageasynctask Task:mtask) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Task.cancel (false);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br>}<br></span></span></p><p><p><span style="font-size:14px"><span style="font-family:Arial; line-height:26px">Data adapter DataAdapter that corresponds to the listview:</span></span></p></p><p><span style="font-size:14px"><span style="font-family:Arial; line-height:26px">public class DataAdapter extends Baseadapter implements Onscrolllistener {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private Context mcontext;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private List<databean> List;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private Imageloader mimageloader;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>private int msart;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>private int mEnd;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public static string[] URLS;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private ListView listview;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Private Boolean isfirst;//is The first time to enter<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public DataAdapter (Context mcontext, list<databean> List, listview Listview) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>This.listview = listView;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>This.mcontext = mcontext;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>This.list = list;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageloader = new Imageloader (listView);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>URLS = new String[list.size ()];<br><span style="white-space:pre"><span style="white-space:pre"></span></span>for (int i = 0; i < list.size (); I++) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>urls[i] = List.get (i). getimgurl ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>isfirst=true;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Listview.setonscrolllistener (this);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public int GetCount () {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TODO auto-generated Method Stub<br><span style="white-space:pre"><span style="white-space:pre"></span></span>return list.size ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public Object GetItem (int arg0) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TODO auto-generated Method Stub<br><span style="white-space:pre"><span style="white-space:pre"></span></span>return List.get (arg0);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public long Getitemid (int Arg0) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TODO auto-generated Method Stub<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Return arg0;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Public View GetView (int arg0, View view, viewgroup arg2) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Viewholder holder = null;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>if (view = = Null) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder = new Viewholder ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>View = Layoutinflater.from (mcontext). Inflate (r.layout.item_layout, null);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>HOLDER.IV = (ImageView) View.findviewbyid (r.id.item_iv);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder.titletv = (TextView) View.findviewbyid (r.id.item_title);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder.contenttv = (TextView) View.findviewbyid (r.id.item_content);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>View.settag (holder);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>else {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder = (viewholder) View.gettag ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder.titleTv.setText (list.get (arg0). getTitle ());<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder.contentTv.setText (list.get (arg0). getcontent ());<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Holder.iv.setTag (list.get (arg0). getimgurl ());//set tag for ImageView<br><span style="white-space:pre"><span style="white-space:pre"></span></span>New Imageloader (). loaderimagethread (holder.iv, list.get (arg0). getimgurl ());//line loads load image<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageloader.loadimgbyasynctask (holder.iv, list.get (arg0). getimgurl ());<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Return view;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>/***<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* The ListView is called during the flow<br><span style="white-space:pre"><span style="white-space:pre"></span></span>*/<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int Totalitemcount) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Msart = firstvisibleitem;//visible First item<br><span style="white-space:pre"><span style="white-space:pre"></span></span>MEnd = Firstvisibleitem + visibleitemcount;//the last item visible<br><span style="white-space:pre"><span style="white-space:pre"></span></span>If (isfirst&&visibleitemcount>0) {//data is processed the first time it is loaded<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageloader.setimageview (msart, mEnd);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>isfirst=false;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>/***<br><span style="white-space:pre"><span style="white-space:pre"></span></span>* The ListView is called when the flow state changes<br><span style="white-space:pre"><span style="white-space:pre"></span></span>*/<br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void onscrollstatechanged (abslistview view, int Scrollstate) {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>if (scrollstate = = Scroll_state_idle) {//flow stop, load Visible item data at this time<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageloader.setimageview (msart, mEnd);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>else {//stop Loading Data<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Mimageloader.stopalltask ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Class Viewholder {<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TextView titletv;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TextView contenttv;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>ImageView iv;<br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br>}<br></span></span></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Asynchronous Load learning note four: using cache to optimize the Network load image and ListView load optimization</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.