您好,登錄后才能下訂單哦!
【1】異步加載圖片類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; /** * 異步加載圖片 */ public class AsyncImageLoader { // 軟引用,使用內存做臨時緩存 (程序退出,或內存不夠則清除軟引用) private HashMap<String, SoftReference<Drawable>> p_w_picpathCache; public AsyncImageLoader() { p_w_picpathCache = new HashMap<String, SoftReference<Drawable>>(); } /** * 定義回調接口 */ public interface ImageCallback { public void p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl); } /** * 創建子線程加載圖片 * 子線程加載完圖片交給handler處理(子線程不能更新ui,而handler處在主線程,可以更新ui) * handler又交給p_w_picpathCallback,p_w_picpathCallback須要自己來實現,在這里可以對回調參數進行處理 * @param p_w_picpathUrl :須要加載的圖片url * @param p_w_picpathCallback: * @return */ public Drawable loadDrawable(final String p_w_picpathUrl,final ImageCallback p_w_picpathCallback) { //如果緩存中存在圖片 ,則首先使用緩存 if (p_w_picpathCache.containsKey(p_w_picpathUrl)) { SoftReference<Drawable> softReference = p_w_picpathCache.get(p_w_picpathUrl); Drawable drawable = softReference.get(); if (drawable != null) { System.out.println("loadDrawable"); p_w_picpathCallback.p_w_picpathLoaded(drawable, p_w_picpathUrl);//執行回調 return drawable; } } /** * 在主線程里執行回調,更新視圖 */ final Handler handler = new Handler() { public void handleMessage(Message message) { System.out.println("handleMessage"); p_w_picpathCallback.p_w_picpathLoaded((Drawable) message.obj, p_w_picpathUrl); } }; /** * 創建子線程訪問網絡并加載圖片 ,把結果交給handler處理 */ new Thread() { @Override public void run() { Drawable drawable = loadImageFromUrl(p_w_picpathUrl); // 下載完的圖片放到緩存里 p_w_picpathCache.put(p_w_picpathUrl, new SoftReference<Drawable>(drawable)); Message message = handler.obtainMessage(0, drawable); handler.sendMessage(message); } }.start(); return null; } /** * 下載圖片 (注意HttpClient 和httpUrlConnection的區別) */ public Drawable loadImageFromUrl(String url) { try { HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000*15); HttpGet get = new HttpGet(url); HttpResponse response; response = client.execute(get); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); Drawable d = Drawable.createFromStream(entity.getContent(),"src"); return d; } else { return null; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //清除緩存 public void clearCache() { if (this.p_w_picpathCache.size() > 0) { this.p_w_picpathCache.clear(); } } }
【2】 pagerAdapter的instantiateItem方法
@SuppressLint("NewApi") @Override public Object instantiateItem(final ViewGroup container, final int position) { String url=imgsUrls[position]; Drawable cachedImage = asyncImageLoader.loadDrawable(url, new ImageCallback() { @SuppressLint("NewApi") public void p_w_picpathLoaded(Drawable p_w_picpathDrawable,String p_w_picpathUrl) { ImageView img=(ImageView) viewList.get(position).findViewById(R.id.img); img.setBackground(p_w_picpathDrawable); container.addView(view); } }); return viewList.get(position); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。