"1" Loading picture class asynchronously Asyncimageloader
package com.example.testdddleapk.cus;import java.io.ioexception;import java.lang.ref.softreference;import java.util.hashmap;import org.apache.http.httpentity;import org.apache.http.httpresponse;import org.apache.http.httpstatus;import org.apache.http.client.clientprotocolexception;import org.apache.http.client.httpclient;import org.apache.http.client.methods.httpget;import org.apache.http.impl.client.defaulthttpclient;import org.apache.http.params.coreconnectionpnames;import android.graphics.drawable.drawable;import android.os.handler;import android.os.message;/** * Loading images asynchronously */public class asyncimageloader {// soft Reference, use memory to do temporary cache (program exit, or insufficient memory to clear soft reference) private hashmap<string, Softreference<drawable>> imagecache;public asyncimageloader () {imageCache = New hashmap<string, softreference<drawable>> ();} /** * defining callbacksInterface */public interface imagecallback {public void imageloaded (Drawable Imagedrawable, string imageurl);} /** * Create a sub-line loads download Picture * sub-line loads loading the picture to handler processing (the child thread cannot update the UI, and handler is in the main thread, can update the UI) * Handler again to imagecallback,imagecallback need to achieve their own, here can be processed for callback parameters * @param imageurl : Image URL to load * @param imageCallback: * @return */public drawable loaddrawable ( Final string imageurl,final imagecallback imagecallback) {//If there is a picture   in the cache; First use the Cache if (Imagecache.containskey (IMAGEURL)) {SoftReference<Drawable> softReference = imagecache.get (IMAGEURL);D rawable drawable = softreference.get ();if (drawable != null) {system.out.println ("loaddrawable"); imagecallback.imageloaded (drawable, imageurl);// Execute callback return drawable;}} /** * executes callbacks in the main thread to update the view */final handler handler&nbsP;= new handler () {public void handlemessage (message message) { System.out.println ("Handlemessage"); imagecallback.imageloaded ((drawable) message.obj, imageurl);}};/ ** * Create a child thread to access the network and load the picture and give the result to handler processing */new thread () {@Overridepublic void run () {drawable drawable = loadimagefromurl (IMAGEURL);// The downloaded images are placed in the cache Imagecache.put (imageurl, new softreference<drawable> (drawable)); Message message = handler.obtainmessage (0, drawable); handler.sendmessage (message);}}. Start (); return null;} /** * Download Picture (Note the difference between httpclient and httpurlconnection) */public Drawable Loadimagefromurl (String url) {try {HttpClient client = new Defaulthttpclient (); Client.getparams (). Setparameter (CORECONNECTIONPNAMES.CONNECTION_TIMEOUT,&NBSP;1000*15); Httpget get = new httpget (URL); Httpresponse&nbsP;response;response = client.execute (GET);if (Response.getstatusline (). GetStatusCode () == &NBSP;HTTPSTATUS.SC_OK) {httpentity entity = response.getentity ();D rawable d = drawable.createfromstream (Entity.getcontent (), "src"); return d;} else {return null;}} catch (clientprotocolexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} Return null;} Clear Cache Public void clearcache () {if (This.imageCache.size () > 0) { This.imageCache.clear ();}}}
The Instantiateitem method of "2" Pageradapter
@SuppressLint ("Newapi") @Overridepublic Object Instantiateitem (Final ViewGroup container, final int position) {STR ing url=imgsurls[position];D rawable cachedimage = asyncimageloader.loaddrawable (URL, new Imagecallback () {@SuppressLi NT ("Newapi") public void imageloaded (drawable imagedrawable,string imageUrl) {ImageView img= (ImageView) Viewlist.get ( Position). Findviewbyid (r.id.img); Img.setbackground (imagedrawable); Container.addview (view); }}); return viewlist.get (position);}
Android Asynchronous load network picture, combined with Viewpager example