图片异步加载1

2016/09/18 15:37
阅读数 17

在android的ListView里面、我们常常需要用到图片的异步加载来展示一些图片、

实现异步加载的核心代码如下、

public class ImageLoaderByTag {
	
	private HashMap<String,SoftReference<Drawable>> imageCache;//使用软引用,可以由系统在恰当的时候更容易回收
	
	public ImageLoaderByTag(){
		imageCache = new HashMap<String,SoftReference<Drawable>>();
	}
	
	
	/**
	 * 通过url获取图片
	 * @param url
	 * @return
	 */
	public Drawable getDrawable(String url){
		
		 Drawable drawable = null;
		 URL request; 
		 InputStream is;
		 try{
			 request = new URL(url);
			 is = (InputStream)request.getContent();
			 drawable = Drawable.createFromStream(is, "src");
			 
		 }catch(Exception e){
			 drawable = null;
			 LogUtils.logE(e.getMessage());
		 }catch(OutOfMemoryError e){
			 LogUtils.logE("错误:"+e.getMessage());
			 drawable = null;
			 System.gc();
		 }
		  
		return drawable;
	}
	
	/**
	 * 异步加载图片
	 * @param url
	 * @param imageCallback
	 */
	public void asynLoadDrawable(final TagInfo tagInfo,final ImageCallback imageCallback){
		
		
		Drawable drawable = null;
		if(imageCache.containsKey(tagInfo.getUrl())){
			drawable = imageCache.get(tagInfo.getUrl()).get();
			if(null!=drawable){
				imageCallback.Image(tagInfo,drawable,false);
				return;
			}
		}
		
		
		final Handler handler = new Handler(){
			
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				
				Drawable drawable = (Drawable)msg.obj;
				if(drawable!=null){
					imageCache.put(tagInfo.getUrl(), new SoftReference<Drawable>(drawable));
					TagInfo tag = tagInfo;
					tag.setDrawable(drawable);
					imageCallback.Image(tag,drawable,true);
					
				}
				
			}
		};
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				
				handler.obtainMessage(1, getDrawable(tagInfo.getUrl())).sendToTarget();
				
			}
		}).start();
		
		
	}
	
	
	/**
	 * 回调接口
	 *
	 */
	public interface ImageCallback{
		
		public void Image(TagInfo tagInfo,Drawable drawable,boolean isFirst);
		
	}

}

用到的TagInfo实体类、

public class TagInfo {
	
	private int position;
	private String url;
	private Drawable drawable;
	public int getPosition() {
		return position;
	}
	public void setPosition(int position) {
		this.position = position;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Drawable getDrawable() {
		return drawable;
	}
	public void setDrawable(Drawable drawable) {
		this.drawable = drawable;
	}

	
	
}

在这里、图片异步加载就实现了、想要用到异步加载的时候、我们只需要在自定义Adapter的里面实例化一个ImageLoaderTag、然后在自定义的Adapter的getView();方法里面直接调用方法asynLoadDrawable、

TagInfo tag = new TagInfo();  
        tag.setPosition(position);  // 保存了当前在adapter中的位置  
    	tag.setUrl(url);         // 保存当前项所要加载的url 
	imageView.setTag(tag); 
	
	imgeLoader.asynLoadDrawable(tag,new ImageCallback() {
		@Override
		public void Image(TagInfo tagInfo,Drawable drawable,boolean isFirst) {

			ImageView iv = (ImageView)lv_list.findViewWithTag(url);
			if(null!=iv&&drawable!=null){
				iv.setImageDrawable(drawable);
			}
		}
	});

这样ListView图片异步加载就实现了、


测试效果图:

   




展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部