ListView Asynchronous loading picture is a very practical method, usually to get pictures through the network resources generally use this method is better, the user experience is good, the following is said to implement the method, first paste the code of the Main method:
Package cn.wangmeng.test;import java.io.ioexception;import java.io.inputstream;import java.lang.ref. Softreference;import Java.net.malformedurlexception;import Java.net.url;import Java.util.HashMap;import Android.graphics.drawable.drawable;import Android.os.handler;import android.os.Message; Public classAsyncimageloader {PrivateHashmap<string, softreference<drawable>>Imagecache; PublicAsyncimageloader () {Imagecache=NewHashmap<string, softreference<drawable>>(); } Publicdrawable loaddrawable (final String imageUrl, final imagecallback imagecallback) {if(Imagecache.containskey (IMAGEURL)) {SoftReference<Drawable> softreference = Imagecache.Get(IMAGEURL); Drawable drawable= SoftReference.Get(); if(Drawable! =NULL) { returndrawable; }} final Handler Handler=NewHandler () { Public voidhandlemessage (Message message) {imagecallback.imageloaded ((drawable) message.obj, IMAGEURL); } }; NewThread () {@Override Public voidrun () {drawable drawable=Loadimagefromurl (IMAGEURL); Imagecache.put (IMAGEURL,NewSoftreference<drawable>(drawable)); Message Message= Handler.obtainmessage (0, drawable); Handler.sendmessage (message); }}.start (); return NULL; } Public Staticdrawable loadimagefromurl (String url) {URL m; InputStream I=NULL; Try{m=Newurl (URL); I=(InputStream) m.getcontent (); } Catch(malformedurlexception E1) {e1.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } drawable D= Drawable.createfromstream (I,"src"); returnD; } Public InterfaceImagecallback { Public voidimageloaded (drawable imagedrawable, String imageUrl); }}
The above code is the main method to achieve the asynchronous acquisition of pictures, SoftReference is a soft reference, is to better for the system to reclaim variables, duplicate URLs directly return the existing resources, implement the callback function, let the data succeed, update to the UI thread.
Several auxiliary class files:
Package cn.wangmeng.test; Public classImageAndText {PrivateString ImageUrl; PrivateString text; PublicImageAndText (String imageUrl, string text) { This. IMAGEURL =ImageUrl; This. Text =text; } PublicString Getimageurl () {returnImageUrl; } PublicString GetText () {returntext; }}
Package Cn.wangmeng.test;import Android.view.view;import android.widget.imageview;import Android.widget.TextView; Public classViewcache {PrivateView Baseview; PrivateTextView TextView; PrivateImageView ImageView; PublicViewcache (View baseview) { This. Baseview =Baseview; } PublicTextView Gettextview () {if(TextView = =NULL) {TextView=(TextView) Baseview.findviewbyid (R.id.text); } returnTextView; } PublicImageView Getimageview () {if(ImageView = =NULL) {ImageView=(ImageView) Baseview.findviewbyid (r.id.image); } returnImageView; }}
Viewcache is a sub-element layout that assists in getting adapter
Package Cn.wangmeng.test;import Java.util.list;import cn.wangmeng.test.asyncimageloader.imagecallback;import Android.app.activity;import Android.graphics.drawable.drawable;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.imageview;import Android.widget.listview;import Android.widget.TextView; Public classImageandtextlistadapter extends Arrayadapter<imageandtext> { PrivateListView ListView; PrivateAsyncimageloader Asyncimageloader; PublicImageandtextlistadapter (activity activity, list<imageandtext>imageandtexts, ListView ListView) {Super (activity,0, imageandtexts); This. ListView =ListView; Asyncimageloader=NewAsyncimageloader (); } PublicView GetView (intposition, View Convertview, ViewGroup parent) {Activity Activity=(Activity) getcontext (); //inflate the views from XMLView Rowview =Convertview; Viewcache Viewcache; if(Rowview = =NULL) {Layoutinflater inflater=Activity.getlayoutinflater (); Rowview= Inflater.inflate (R.layout.image_and_text_row,NULL); Viewcache=NewViewcache (Rowview); Rowview.settag (Viewcache); } Else{Viewcache=(Viewcache) Rowview.gettag (); } imageandtext ImageAndText=GetItem (position); //Load the image and set it on the ImageViewString IMAGEURL =Imageandtext.getimageurl (); ImageView ImageView=Viewcache.getimageview (); Imageview.settag (IMAGEURL); drawable Cachedimage= Asyncimageloader.loaddrawable (IMAGEURL,NewImagecallback () { Public voidimageloaded (drawable imagedrawable, String imageUrl) {ImageView Imageviewbytag=(ImageView) Listview.findviewwithtag (IMAGEURL); if(Imageviewbytag! =NULL) {imageviewbytag.setimagedrawable (imagedrawable); } } }); if(Cachedimage = =NULL) {Imageview.setimageresource (r.drawable.default_image); }Else{imageview.setimagedrawable (cachedimage); } //Set The text on the TextViewTextView TextView =Viewcache.gettextview (); Textview.settext (Imageandtext.gettext ()); returnRowview; }}
Imageandtextlistadapter is the implementation of the ListView adapter, there is a trick is Imageview.settag (IMAGEURL), Settag is to store data, so as to ensure that the callback function, ListView to update their corresponding item, we read carefully to know.
Finally post the layout file:
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Horizontal"Android:layout_width="fill_parent"Android:layout_height="wrap_content"> <imageview android:id="@+id/image"Android:layout_width="wrap_content"Android:layout_height="wrap_content"/> <textview android:id="@+id/text"Android:layout_width="wrap_content"Android:layout_height="wrap_content"/></linearlayout>
Original address: Http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2
Android implementation ListView Loading picture asynchronously