android 安卓异步加载网络图片,与viewpager结合使用示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 安卓异步加载网络图片,与viewpager结合使用示例相关的知识,希望对你有一定的参考价值。
【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>> imageCache; public AsyncImageLoader() { imageCache = new HashMap<String, SoftReference<Drawable>>(); } /** * 定义回调接口 */ public interface ImageCallback { public void imageLoaded(Drawable imageDrawable, String imageUrl); } /** * 创建子线程加载图片 * 子线程加载完图片交给handler处理(子线程不能更新ui,而handler处在主线程,可以更新ui) * handler又交给imageCallback,imageCallback须要自己来实现,在这里可以对回调参数进行处理 * @param imageUrl :须要加载的图片url * @param imageCallback: * @return */ public Drawable loadDrawable(final String imageUrl,final ImageCallback imageCallback) { //如果缓存中存在图片 ,则首先使用缓存 if (imageCache.containsKey(imageUrl)) { SoftReference<Drawable> softReference = imageCache.get(imageUrl); Drawable drawable = softReference.get(); if (drawable != null) { System.out.println("loadDrawable"); imageCallback.imageLoaded(drawable, imageUrl);//执行回调 return drawable; } } /** * 在主线程里执行回调,更新视图 */ final Handler handler = new Handler() { public void handleMessage(Message message) { System.out.println("handleMessage"); imageCallback.imageLoaded((Drawable) message.obj, imageUrl); } }; /** * 创建子线程访问网络并加载图片 ,把结果交给handler处理 */ new Thread() { @Override public void run() { Drawable drawable = loadImageFromUrl(imageUrl); // 下载完的图片放到缓存里 imageCache.put(imageUrl, 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.imageCache.size() > 0) { this.imageCache.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 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 安卓异步加载网络图片,与viewpager结合使用示例的主要内容,如果未能解决你的问题,请参考以下文章
Android之ListView异步加载网络图片(优化缓存机制)