转载:http://marshal.easymorse.com/archives/2662作者做了修改 原文:http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/
ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码:
public class AsyncListImage extends ListActivity { private MyImageAndTextListAdapter listAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setListAdapter(getAdapter()); } private MyImageAndTextListAdapter getAdapter(){ if(this.listAdapter==null){ listAdapter=new MyImageAndTextListAdapter(this, getList()); } return listAdapter; } @SuppressWarnings("unchecked") private List<NewsBean> getList() { try { XStream xStream = new XStream(); xStream.alias("item", NewsBean.class); return (List<NewsBean>) xStream.fromXML(this.getAssets().open( "list.xml")); } catch (IOException e) { throw new RuntimeException(e); } } }
以上代码是实现异步获取图片的主方法,SoftReference是软引用,是为了更好的为了系统回收变量,重复的URL直接返回已有的资源,实现回调函数,让数据成功后,更新到UI线程。
NewsBean.ava:
public class NewsBean { private String title; private String content; private String image; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return title + "-" + image; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } }
MyImageAndTextListAdapter.java:
public class MyImageAndTextListAdapter extends ArrayAdapter<NewsBean> { public MyImageAndTextListAdapter(Activity activity, List<NewsBean> newsList) { super(activity, 0, newsList); } private AsyncImageLoader imageLoader = new AsyncImageLoader(); private Map<Integer, View> viewMap = new HashMap<Integer, View>(); @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = this.viewMap.get(position); if (rowView == null) { LayoutInflater inflater = ((Activity) this.getContext()) .getLayoutInflater(); rowView = inflater.inflate(R.layout.news_row, null); NewsBean newsBean = this.getItem(position); TextView textView = (TextView) rowView.findViewById(R.id.title); textView.setText(newsBean.getTitle()); final ImageView imageView = (ImageView) rowView .findViewById(R.id.image); imageLoader.loadDrawable(newsBean.getImage(), new ImageCallback() { public void imageLoaded(Drawable imageDrawable, String imageUrl) { imageView.setImageDrawable(imageDrawable); } }); viewMap.put(position, rowView); } return rowView; } }
AsyncImageLoader.java:
public class AsyncImageLoader { private Map<String, SoftReference<Drawable>> imageCache=new HashMap<String, SoftReference<Drawable>>(); public Drawable loadDrawable(final String imageUrl,final ImageCallback callback){ if(imageCache.containsKey(imageUrl)){ SoftReference<Drawable> softReference=imageCache.get(imageUrl); if(softReference.get()!=null){ return softReference.get(); } } final Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { callback.imageLoaded((Drawable) msg.obj, imageUrl); } }; new Thread(){ public void run() { Drawable drawable=loadImageFromUrl(imageUrl); imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); handler.sendMessage(handler.obtainMessage(0,drawable)); }; }.start(); return null; } protected Drawable loadImageFromUrl(String imageUrl) { try { return Drawable.createFromStream(new URL(imageUrl).openStream(), "src"); } catch (Exception e) { throw new RuntimeException(e); } } public interface ImageCallback{ public void imageLoaded(Drawable imageDrawable,String imageUrl); } }
list.xml:
<list> <item> <title>萨马兰奇遗体告别仪式举行</title> <image>http://g4.ykimg.com/01270F1F464BD0E447AE2100000000B07DBC93-FD8E-0AE4-3CDF-8383410521BC </image> <content>萨马兰奇在任期间先后主持了洛杉矶、汉城、巴塞罗那、亚特兰大和悉尼奥运会。在2001年7月宣布北京获得2008年奥运会举办权。 </content> </item> <item> <title>黄光裕案控辩大腕对垒</title> <image>http://g4.ykimg.com/0100641F464BD062FB70400030BFFF0003153B-2367-5FF8-478C-E57C39832E26 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>降雪增加玉树救灾难度</title> <image>http://g3.ykimg.com/0130391F464BD13FAEE914027E41339D78F2C3-E409-1EEB-FDE5-A5A214BC043C?u=1272004526 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>气质导游美女惊艳世博</title> <image>http://g2.ykimg.com/0100641F464BD06E29EF440073F0ED876B3A5E-CAB1-0A4F-80D8-2B7A1D2020D3 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>黄晓明:我有那么二吗</title> <image>http://g3.ykimg.com/0100641F464BD095B68A770014D61BB13182B4-6191-0AF0-09C4-988B95D663DB </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>黄光裕案控辩大腕对垒</title> <image>http://g4.ykimg.com/0100641F464BD062FB70400030BFFF0003153B-2367-5FF8-478C-E57C39832E26 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>独家探秘保时捷超跑车</title> <image>http://g3.ykimg.com/0100641F464BCD9D06502E00332D63960CC1A6-8792-E3A9-51F4-B09AE5FB0BD9 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>非常爱情狂:钱花不出去的徐总</title> <image>http://g4.ykimg.com/0100641F464BC7FBAF3ACD0326EA53D930E38A-D8BD-26D6-5FC3-5D9ED60EDE35 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>一部穿越时光的手机</title> <image>http://g4.ykimg.com/0100641F464BCC832F5534038D33EF10AB6F7B-09DB-9803-5039-37287414AE11 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> <item> <title>原创中国风仿皮影动画</title> <image>http://g4.ykimg.com/0100641F464BCDEFBBE79200E2905F1B885C92-F04A-09A7-A31C-A08EF54F79D8 </image> <content>今天是黄光裕案正式开庭的日子,而为了不错过这一天,我已经等待了很长时间。 记得刚刚开始接触这个案子的时候,还是去年年底。我在外地出差,同事打听到了黄光裕的 ... </content> </item> </list>
原文加载数据是自己写的,而作者修改后用的是XStream,今天再复习一下
SoftReference http://www.javaeye.com/topic/