异步 加载 图片

引用:http://www.oschina.net/code/snippet_163910_6086

[代码] AsyncImageLoader.java

01 package cn.com.mzba.service;
02  
03 import java.io.InputStream;
04 import java.lang.ref.SoftReference;
05 import java.net.URL;
06 import java.util.HashMap;
07  
08 import android.graphics.drawable.Drawable;
09 import android.os.Handler;
10 import android.os.Message;
11 import android.widget.ImageView;
12 /**
13  * 实现图片的异步载入显示
14  * @author kandanle
15  *
16  */
17 public class AsyncImageLoader {
18  
19     /**
20      * 软引用对象,在响应内存需要时,由垃圾回收器决定是否清除此对象。软引用对象最常用于实现内存敏感的缓存。
21      */
22     private HashMap<String, SoftReference<Drawable>> imageCache;
23     
24     public AsyncImageLoader(){
25         imageCache = new HashMap<String, SoftReference<Drawable>>();
26     }
27     
28     public Drawable loadDrawable(final String imageUrl,final ImageView imageView,finalImageCallback imagecallback){
29         if(imageCache.containsKey(imageUrl)){
30             //从缓存中读取人人
31             SoftReference<Drawable> softReference = imageCache.get(imageUrl);
32             Drawable drawable = softReference.get();
33             if(drawable != null){
34                 return drawable;
35             }
36         }
37         final Handler handler = new Handler(){
38             @Override
39             public void handleMessage(Message msg) {
40                 super.handleMessage(msg);
41                 imagecallback.imageLoaded((Drawable)msg.obj, imageView, imageUrl);
42             }
43         };
44         new Thread(){
45             public void run() {
46                 Drawable drawable = loadImageFromUrl(imageUrl);
47                 imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
48                 Message message = handler.obtainMessage(0, drawable);
49                 handler.sendMessage(message);
50             }
51         }.start();
52         
53         return null;
54     }
55     
56     public static Drawable loadImageFromUrl(String urlPath){
57         URL url;
58         InputStream is = null;
59         try{
60             url = new URL(urlPath);
61             is = (InputStream)url.getContent();
62         }catch(Exception e){
63             e.printStackTrace();
64         }
65         Drawable drawable = Drawable.createFromStream(is, "src");
66         return drawable;
67     }
68     
69 }

[代码] ImageCallback.java

01 package cn.com.mzba.service;
02  
03 import android.graphics.drawable.Drawable;
04 import android.widget.ImageView;
05  
06 public interface ImageCallback {
07     public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl);
08 }
09  
10 Drawable drawable = asyncImageLoader.loadDrawable(weiboInfo.getUserIcon(), viewUserIcon,new ImageCallback() {
11     @Override
12     public void imageLoaded(Drawable imageDrawable, ImageView imageView,String imageUrl) {
13     imageView.setImageDrawable(imageDrawable);
14         }
15 });
16 if(drawable == null){
17     viewUserIcon.setImageResource(R.drawable.portrait);
18 }else{
19     viewUserIcon.setImageDrawable(drawable);
20 }
posted @ 2011-09-23 15:11  镇水古月  阅读(232)  评论(0编辑  收藏  举报