Android实现ListView异步加载图片

ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:

  1. packagecn.wangmeng.test;
  2. importjava.io.IOException;
  3. importjava.io.InputStream;
  4. importjava.lang.ref.SoftReference;
  5. importjava.net.MalformedURLException;
  6. importjava.net.URL;
  7. importjava.util.HashMap;
  8. importandroid.graphics.drawable.Drawable;
  9. importandroid.os.Handler;
  10. importandroid.os.Message;
  11. publicclassAsyncImageLoader{
  12. privateHashMap<String,SoftReference<Drawable>>imageCache;
  13. publicAsyncImageLoader(){
  14. imageCache=newHashMap<String,SoftReference<Drawable>>();
  15. }
  16. publicDrawableloadDrawable(finalStringimageUrl,finalImageCallbackimageCallback){
  17. if(imageCache.containsKey(imageUrl)){
  18. SoftReference<Drawable>softReference=imageCache.get(imageUrl);
  19. Drawabledrawable=softReference.get();
  20. if(drawable!=null){
  21. returndrawable;
  22. }
  23. }
  24. finalHandlerhandler=newHandler(){
  25. publicvoidhandleMessage(Messagemessage){
  26. imageCallback.imageLoaded((Drawable)message.obj,imageUrl);
  27. }
  28. };
  29. newThread(){
  30. @Override
  31. publicvoidrun(){
  32. Drawabledrawable=loadImageFromUrl(imageUrl);
  33. imageCache.put(imageUrl,newSoftReference<Drawable>(drawable));
  34. Messagemessage=handler.obtainMessage(0,drawable);
  35. handler.sendMessage(message);
  36. }
  37. }.start();
  38. returnnull;
  39. }
  40. publicstaticDrawableloadImageFromUrl(Stringurl){
  41. URLm;
  42. InputStreami=null;
  43. try{
  44. m=newURL(url);
  45. i=(InputStream)m.getContent();
  46. }catch(MalformedURLExceptione1){
  47. e1.printStackTrace();
  48. }catch(IOExceptione){
  49. e.printStackTrace();
  50. }
  51. Drawabled=Drawable.createFromStream(i,"src");
  52. returnd;
  53. }
  54. publicinterfaceImageCallback{
  55. publicvoidimageLoaded(DrawableimageDrawable,StringimageUrl);
  56. }
  57. }
以上代码是实现异步获取图片的主方法,SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。
几个辅助类文件:

  1. packagecn.wangmeng.test;
  2. publicclassImageAndText{
  3. privateStringimageUrl;
  4. privateStringtext;
  5. publicImageAndText(StringimageUrl,Stringtext){
  6. this.imageUrl=imageUrl;
  7. this.text=text;
  8. }
  9. publicStringgetImageUrl(){
  10. returnimageUrl;
  11. }
  12. publicStringgetText(){
  13. returntext;
  14. }
  15. }
  1. packagecn.wangmeng.test;
  2. importandroid.view.View;
  3. importandroid.widget.ImageView;
  4. importandroid.widget.TextView;
  5. publicclassViewCache{
  6. privateViewbaseView;
  7. privateTextViewtextView;
  8. privateImageViewimageView;
  9. publicViewCache(ViewbaseView){
  10. this.baseView=baseView;
  11. }
  12. publicTextViewgetTextView(){
  13. if(textView==null){
  14. textView=(TextView)baseView.findViewById(R.id.text);
  15. }
  16. returntextView;
  17. }
  18. publicImageViewgetImageView(){
  19. if(imageView==null){
  20. imageView=(ImageView)baseView.findViewById(R.id.image);
  21. }
  22. returnimageView;
  23. }
  24. }
ViewCache是辅助获取adapter的子元素布局

  1. packagecn.wangmeng.test;
  2. importjava.util.List;
  3. importcn.wangmeng.test.AsyncImageLoader.ImageCallback;
  4. importandroid.app.Activity;
  5. importandroid.graphics.drawable.Drawable;
  6. importandroid.view.LayoutInflater;
  7. importandroid.view.View;
  8. importandroid.view.ViewGroup;
  9. importandroid.widget.ArrayAdapter;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.ListView;
  12. importandroid.widget.TextView;
  13. publicclassImageAndTextListAdapterextendsArrayAdapter<ImageAndText>{
  14. privateListViewlistView;
  15. privateAsyncImageLoaderasyncImageLoader;
  16. publicImageAndTextListAdapter(Activityactivity,List<ImageAndText>imageAndTexts,ListViewlistView){
  17. super(activity,0,imageAndTexts);
  18. this.listView=listView;
  19. asyncImageLoader=newAsyncImageLoader();
  20. }
  21. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  22. Activityactivity=(Activity)getContext();
  23. //InflatetheviewsfromXML
  24. ViewrowView=convertView;
  25. ViewCacheviewCache;
  26. if(rowView==null){
  27. LayoutInflaterinflater=activity.getLayoutInflater();
  28. rowView=inflater.inflate(R.layout.image_and_text_row,null);
  29. viewCache=newViewCache(rowView);
  30. rowView.setTag(viewCache);
  31. }else{
  32. viewCache=(ViewCache)rowView.getTag();
  33. }
  34. ImageAndTextimageAndText=getItem(position);
  35. //LoadtheimageandsetitontheImageView
  36. StringimageUrl=imageAndText.getImageUrl();
  37. ImageViewimageView=viewCache.getImageView();
  38. imageView.setTag(imageUrl);
  39. DrawablecachedImage=asyncImageLoader.loadDrawable(imageUrl,newImageCallback(){
  40. publicvoidimageLoaded(DrawableimageDrawable,StringimageUrl){
  41. ImageViewimageViewByTag=(ImageView)listView.findViewWithTag(imageUrl);
  42. if(imageViewByTag!=null){
  43. imageViewByTag.setImageDrawable(imageDrawable);
  44. }
  45. }
  46. });
  47. if(cachedImage==null){
  48. imageView.setImageResource(R.drawable.default_image);
  49. }else{
  50. imageView.setImageDrawable(cachedImage);
  51. }
  52. //SetthetextontheTextView
  53. TextViewtextView=viewCache.getTextView();
  54. textView.setText(imageAndText.getText());
  55. returnrowView;
  56. }
  57. }
ImageAndTextListAdapter是实现ListView的Adapter,里面有个技巧就是imageView.setTag(imageUrl),setTag是存储数据的,这样是为了保证在回调函数时,listview去更新自己对应item,大家仔细阅读就知道了。
最后贴出布局文件:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content">
  6. <ImageViewandroid:id="@+id/image"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. />
  10. <TextViewandroid:id="@+id/text"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"/>
  13. </LinearLayout>





你可能感兴趣的:(ListView)