listview 异步加载图片

本文介绍了一种在Android应用中实现异步加载图片到ListView的方法。通过自定义适配器和图片加载器,可以有效地避免UI阻塞,并提高用户体验。文章详细展示了各个组件类的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)定义类MapListImageAndText管理ListViewItem中控件的内容 
Java代码   收藏代码
  1. package com.google.zxing.client.android.AsyncLoadImage;  
  2.   
  3.   
  4.   
  5. public class MapListImageAndText {  
  6.         private String imageUrl;  
  7.         private String shopname;  
  8.         private String activitynifo;  
  9.         private String address;  
  10.         private String telephone;  
  11.         private String distance;  
  12.           
  13.         public MapListImageAndText(String imageUrl, String shopname, String activitynifo, String address, String telephone,String distance) {  
  14.             this.imageUrl = imageUrl;  
  15.             this.shopname = shopname;  
  16.             this.activitynifo = activitynifo;  
  17.             this.address = address;  
  18.             this.telephone = telephone;  
  19.             this.distance=distance;  
  20.         }  
  21.   
  22.         public String getImageUrl() {  
  23.             return imageUrl;  
  24.         }  
  25.           
  26.         public String getShopname() {  
  27.             return shopname;  
  28.         }  
  29.   
  30.         public String getActivitynifo() {  
  31.             return activitynifo;  
  32.         }  
  33.   
  34.         public String getAddress() {  
  35.             return address;  
  36.         }  
  37.   
  38.         public String getTelephone() {  
  39.             return telephone;  
  40.         }  
  41.           
  42.         public String getDistance() {  
  43.             return distance;  
  44.         }  
  45.   
  46.           
  47. }  

(2)定义类MapListViewCache实例化ListViewItem中的控件 
Java代码   收藏代码
  1. package com.google.zxing.client.android.AsyncLoadImage;  
  2.   
  3. import com.google.zxing.client.android.R;  
  4.   
  5. import android.view.View;  
  6. import android.widget.ImageView;  
  7. import android.widget.TextView;  
  8.   
  9. public class MapListViewCache {  
  10.   
  11.         private View baseView;  
  12.         private TextView shopname;  
  13.         private TextView activitynifo;  
  14.         private TextView address;  
  15.         private TextView telephone;  
  16.         private TextView distance;  
  17.   
  18.         private ImageView imageView;  
  19.   
  20.         public MapListViewCache(View baseView) {  
  21.             this.baseView = baseView;  
  22.         }  
  23.   
  24.         public TextView getShopname() {  
  25.             if (shopname == null) {  
  26.                 shopname = (TextView) baseView.findViewById(R.id.maplistviewitemshopname);  
  27.             }  
  28.             return shopname;  
  29.         }  
  30.           
  31.         public TextView getActivitynifo() {  
  32.             if (activitynifo == null) {  
  33.                 activitynifo = (TextView) baseView.findViewById(R.id.maplistviewitemActi);  
  34.             }  
  35.             return activitynifo;  
  36.         }  
  37.           
  38.         public TextView getAddress() {  
  39.             if (address == null) {  
  40.                 address = (TextView) baseView.findViewById(R.id.maplistviewitemaddr);  
  41.             }  
  42.             return address;  
  43.         }  
  44.           
  45.         public TextView getTelephone() {  
  46.             if (telephone == null) {  
  47.                 telephone = (TextView) baseView.findViewById(R.id.maplistviewitemtelphone);  
  48.             }  
  49.             return telephone;  
  50.         }  
  51.   
  52.         public ImageView getImageView() {  
  53.             if (imageView == null) {  
  54.                 imageView = (ImageView) baseView.findViewById(R.id.maplistviewitemImage);  
  55.             }  
  56.             return imageView;  
  57.         }  
  58.           
  59.         public TextView getDistance() {  
  60.             if (distance == null) {  
  61.                 distance = (TextView) baseView.findViewById(R.id.maplistviewitemdistance);  
  62.             }  
  63.             return distance;  
  64.         }  
  65.   
  66. }  

(3)定义类AsyncImageLoader,开启线程下载指定图片 
Java代码   收藏代码
  1. package com.google.zxing.client.android.AsyncLoadImage;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.lang.ref.SoftReference;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.util.HashMap;  
  9.   
  10. import android.graphics.drawable.Drawable;  
  11. import android.os.Handler;  
  12. import android.os.Message;  
  13.   
  14. public class AsyncImageLoader {  
  15.   
  16.      private HashMap<String, SoftReference<Drawable>> imageCache;  
  17.         
  18.          public AsyncImageLoader() {  
  19.              imageCache = new HashMap<String, SoftReference<Drawable>>();  
  20.          }  
  21.         
  22.          public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {  
  23.              if (imageCache.containsKey(imageUrl)) {  
  24.                  SoftReference<Drawable> softReference = imageCache.get(imageUrl);  
  25.                  Drawable drawable = softReference.get();  
  26.                  if (drawable != null) {  
  27.                      return drawable;  
  28.                  }  
  29.              }  
  30.              final Handler handler = new Handler() {  
  31.                  public void handleMessage(Message message) {  
  32.                      imageCallback.imageLoaded((Drawable) message.obj, imageUrl);  
  33.                  }  
  34.              };  
  35.              new Thread() {  
  36.                  @Override  
  37.                  public void run() {  
  38.                      Drawable drawable = loadImageFromUrl(imageUrl);  
  39.                      imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));  
  40.                      Message message = handler.obtainMessage(0, drawable);  
  41.                      handler.sendMessage(message);  
  42.                  }  
  43.              }.start();  
  44.              return null;  
  45.          }  
  46.         
  47.         public static Drawable loadImageFromUrl(String url) {  
  48.             URL m;  
  49.             InputStream i = null;  
  50.             try {  
  51.                 m = new URL(url);  
  52.                 i = (InputStream) m.getContent();  
  53.             } catch (MalformedURLException e1) {  
  54.                 e1.printStackTrace();  
  55.             } catch (IOException e) {  
  56.                 e.printStackTrace();  
  57.             }  
  58.             Drawable d = Drawable.createFromStream(i, "src");  
  59.             return d;  
  60.         }  
  61.         
  62.          public interface ImageCallback {  
  63.              public void imageLoaded(Drawable imageDrawable, String imageUrl);  
  64.          }  
  65.   
  66. }  

(4)定义类MapListImageAndTextListAdapter继承ArrayAdapter 
Java代码   收藏代码
  1. package com.google.zxing.client.android.AsyncLoadImage;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.google.zxing.client.android.R;  
  6.   
  7. import com.google.zxing.client.android.AsyncLoadImage.AsyncImageLoader.ImageCallback;  
  8.   
  9. import android.app.Activity;  
  10. import android.graphics.drawable.Drawable;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.ArrayAdapter;  
  15. import android.widget.ImageView;  
  16. import android.widget.ListView;  
  17. import android.widget.TextView;  
  18.   
  19. public class MapListImageAndTextListAdapter extends ArrayAdapter<MapListImageAndText> {  
  20.   
  21.         private ListView listView;  
  22.         private AsyncImageLoader asyncImageLoader;  
  23.   
  24.         public MapListImageAndTextListAdapter(Activity activity, List<MapListImageAndText> imageAndTexts, ListView listView) {  
  25.             super(activity, 0, imageAndTexts);  
  26.             this.listView = listView;  
  27.             asyncImageLoader = new AsyncImageLoader();  
  28.         }  
  29.   
  30.         public View getView(int position, View convertView, ViewGroup parent) {  
  31.             Activity activity = (Activity) getContext();  
  32.   
  33.             // Inflate the views from XML  
  34.             View rowView = convertView;  
  35.             MapListViewCache viewCache;  
  36.             if (rowView == null) {  
  37.                 LayoutInflater inflater = activity.getLayoutInflater();  
  38.                 rowView = inflater.inflate(R.layout.maplistviewitem, null);  
  39.                 viewCache = new MapListViewCache(rowView);  
  40.                 rowView.setTag(viewCache);  
  41.             } else {  
  42.                 viewCache = (MapListViewCache) rowView.getTag();  
  43.             }  
  44.             MapListImageAndText imageAndText = getItem(position);  
  45.   
  46.             // Load the image and set it on the ImageView  
  47.             String imageUrl = imageAndText.getImageUrl();  
  48.             ImageView imageView = viewCache.getImageView();  
  49.             imageView.setTag(imageUrl);  
  50.             Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {  
  51.                   
  52.                   
  53.                 public void imageLoaded(Drawable imageDrawable, String imageUrl) {  
  54.                     ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);  
  55.                     if (imageViewByTag != null) {  
  56.                         imageViewByTag.setImageDrawable(imageDrawable);  
  57.                     }  
  58.                 }  
  59.             });  
  60.             if (cachedImage == null) {  
  61.                 imageView.setImageResource(R.drawable.refresh);  
  62.             }else{  
  63.                 imageView.setImageDrawable(cachedImage);  
  64.             }  
  65.             // Set the text on the TextView  
  66.             TextView shopname = viewCache.getShopname();  
  67.             shopname.setText(imageAndText.getShopname());  
  68.               
  69.             TextView activitynifo = viewCache.getActivitynifo();  
  70.             activitynifo.setText(imageAndText.getActivitynifo());  
  71.               
  72.             TextView address = viewCache.getAddress();  
  73.             address.setText(imageAndText.getAddress());  
  74.               
  75.             TextView telephone = viewCache.getTelephone();  
  76.             telephone.setText(imageAndText.getTelephone());  
  77.               
  78.             TextView distance = viewCache.getDistance();  
  79.             distance.setText(imageAndText.getDistance());  
  80.               
  81.             return rowView;  
  82.         }  
  83.   
  84. }  

(5)主程序中Listview与MapListImageAndTextListAdapter的捆绑 
Java代码   收藏代码
  1. //tuangoupoints为对后台传回来的数据解析后得到的字符串  
  2. String[] mtuangoupoints =tuangoupoints.split("@");  
  3.   
  4. List<MapListImageAndText> dataArray=new ArrayList<MapListImageAndText>();  
  5.       
  6. for(int i=0; i<mtuangoupoints.length;i++){  
  7.     String[] tonepoint=mtuangoupoints[i].split("#");  
  8.       
  9.     String shopname=String.valueOf(i+1)+tonepoint[2];  
  10.     String activityinfo=tonepoint[1];  
  11.     String address=tonepoint[6];  
  12.     String telephone=tonepoint[7];  
  13.     String imageurl=tonepoint[8];  
  14.     String distance=tonepoint[5];  
  15.       
  16.     MapListImageAndText test=new MapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance);  
  17.     dataArray.add(test);  
  18. }        
  19. MapListImageAndTextListAdapter adapter=new MapListImageAndTextListAdapter(this, dataArray, mlistView);  
  20. mlistView.setAdapter(adapter);  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值