Android ListView 性能优化-----(异步加载图片资源)

asdasdasdasdasdsa

   1: public class AsyncImageLoader {
   2:     
   3:     //图片缓存(此处软引用,这样虚拟机只会在内存不足的时候才会回收此对象)
   4:     HashMap<String, SoftReference<Drawable>> imgCache;
   5:     
   6:     public AsyncImageLoader(){
   7:         
   8:         imgCache = new HashMap<String, SoftReference<Drawable>>();
   9:         
  10:     }
  11:     /**
  12:      * 
  13:      * @param url 
  14:      *             要下载图片的url路径
  15:      * @param imageView
  16:      *             图片要显示在的ImageView
  17:      * @param imageDownloadCallBack
  18:      *             下载完成后执行的回调函数
  19:      * @return
  20:      */
  21:     public Drawable downloadDrawableFromURL(final String url,final ImageView imageView,final ImageDownloadCallBack imageDownloadCallBack){
  22:         
  23:         //先判断缓存中是否已有下载好的图片
  24:         if(imgCache.containsKey(url) == true){
  25:             
  26:             return imgCache.get(url).get();
  27:             
  28:         }
  29:         
  30:         
  31:         //如果缓存没有则需要开线程进行异步下载
  32:         
  33:         final Handler handler = new Handler(){
  34:  
  35:             @Override
  36:             public void handleMessage(Message msg) {
  37:                 
  38:                 //获取message中保存的下载好的Drawable
  39:                 Drawable downloadedDrawable = (Drawable)msg.obj;
  40:                 //执行回调函数
  41:                 imageDownloadCallBack.downloaded(url, downloadedDrawable, imageView);
  42:                 
  43:             }
  44:             
  45:             
  46:         };
  47:         
  48:         //新开一个线程负责下载图片
  49:         new Thread(new Runnable() {
  50:             
  51:             public void run() {
  52:                 
  53:                 Drawable drawable = downloadDrawable(url);
  54:                 //缓存下载后的图片
  55:                 imgCache.put(url, new SoftReference<Drawable>(drawable));
  56:                 
  57:                 Message message = handler.obtainMessage();
  58:                 message.obj = drawable;
  59:                 handler.sendMessage(message);
  60:                 
  61:             }
  62:         });
  63:         
  64:         
  65:         return null;
  66:     }
  67:     
  68:     
  69:     
  70:     //定义一个回调接口
  71:     public interface ImageDownloadCallBack{
  72:         
  73:         void downloaded(String imgURL,Drawable downloadedDrawable,ImageView imageView);
  74:         
  75:     }
  76:     
  77:     
  78:     //下载图片
  79:     private Drawable downloadDrawable(String imgURL){
  80:         
  81:         Drawable drawable = null;
  82:         try {
  83:             URL url = new URL(imgURL);
  84:             drawable = Drawable.createFromStream(url.openStream(), "src");
  85:         } catch (MalformedURLException e) {
  86:             
  87:             e.printStackTrace();
  88:             
  89:         } catch (IOException e) {
  90:             e.printStackTrace();
  91:         }
  92:         
  93:         return drawable;
  94:     }
  95:     
  96:     
  97:     
  98: }

 

使用:

创建一个Adapter,在getView()方法中进行调用

   1: public class MyAdapter extends BaseAdapter{
   2:  
   3:     //代码部分省略
   4:     
   5:     public View getView(int position, View convertView, ViewGroup parent) {
   6:         
   7:         String url = "";
   8:         ImageView imageView = null;
   9:         
  10:         AsyncImageLoader imageLoader = new AsyncImageLoader();
  11:         
  12:         Drawable cachedDrawable = imageLoader.downloadDrawableFromURL(url, imageView, new AsyncImageLoader.ImageDownloadCallBack() {
  13:             
  14:             public void downloaded(String imgURL, Drawable downloadedDrawable,
  15:                     ImageView imageView) {
  16:                 
  17:                 //此处的downloadedDrawable 是通过线程下载的图片
  18:                 if(downloadedDrawable!=null){
  19:  
  20:                     imageView.setImageDrawable(downloadedDrawable);
  21:                     
  22:                 }
  23:                 
  24:             }
  25:         });
  26:         
  27:         
  28:         //说明此图片已被下载并缓存
  29:         if(cachedDrawable != null){
  30:             
  31:             imageView.setImageDrawable(cachedDrawable);
  32:             
  33:         }
  34:         
  35:         
  36:         
  37:         
  38:         return null;
  39:     }
  40:     
  41:     
  42: }
posted @ 2011-06-28 22:43  胡言乱语  阅读(1853)  评论(0编辑  收藏  举报