ListView性能优化异步加载图片 使滑动效果流畅(转载)
2012-06-15 16:40
375 查看
android里图片下载工具类AsyncImageLoader分析
public class AsyncImageLoader { private HashMap<String, SoftReference<Drawable>> imageCache; public AsyncImageLoader() { imageCache = new HashMap<String, SoftReference<Drawable>>(); } public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { if (imageCache.containsKey(imageUrl)) { SoftReference<Drawable> softReference = imageCache.get(imageUrl); Drawable drawable = softReference.get(); if (drawable != null) { return drawable; } } final Handler handler = new Handler() { public void handleMessage(Message message) { imageCallback.imageLoaded((Drawable) message.obj, imageUrl); } }; new Thread() { @Override public void run() { Drawable drawable = loadImageFromUrl(imageUrl); imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); Message message = handler.obtainMessage(0, drawable); handler.sendMessage(message); } }.start(); return null; } public static Drawable loadImageFromUrl(String url) { URL m; InputStream i = null; try { m = new URL(url); i = (InputStream) m.getContent(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Drawable d = Drawable.createFromStream(i, "src"); return d; } public interface ImageCallback { public void imageLoaded(Drawable imageDrawable, String imageUrl); } }
实现方式:通过传入图片的网络地址和一个实现ImageCallback行为的对象,当imageCache存在这个图片时候,返回这个图片,当imageCache没有这个图片时,实例一个异步线程来下载图片并同时返回为null,最后在图片下载完成的时候,调用imageLoaded方法。
现说说这个类设计的优点吧:1.采用了策略模式;2.使用SoftReference关键字
先说说策略模式,程序里把每次下载图片完成后所进行的操作封装成一个ImageCallback抽象类,使系统更灵活,并易于扩展。
在Java中内存管理,引用分为四大类,强引用HardReference、弱引用WeakReference、软引用SoftReference和虚引用PhantomReference。它们的区别也很明显,HardReference对象是即使虚拟机内存吃紧抛出OOM也不会导致这一引用的对象被回收,而WeakReference等更适合于一些数量不多,但体积稍微庞大的对象,在这四个引用中,它是最容易被垃圾回收的,而我们对于显示类似Android Market中每个应用的App Icon时可以考虑使用SoftReference来解决内存不至于快速回收,同时当内存短缺面临Java
VM崩溃抛出OOM前时,软引用将会强制回收内存,最后的虚引用一般没有实际意义,仅仅观察GC的活动状态,对于测试比较实用同时必须和ReferenceQueue一起使用。对于一组数据,我们可以通过HashMap的方式来添加一组SoftReference对象来临时保留一些数据,同时对于需要反复通过网络获取的不经常改变的内容,可以通过本地的文件系统或数据库来存储缓存。
最后一句话说的很对,事实上大多数情况也是如此。
在说说它的用法吧,通常它作为一个adapter的一个变量如:
class BookAdapter extends ArrayAdapter<BookInfo>{ AsyncImageLoader asyncImageLoader; Context mContext; BookAdapter(Context context,List<BookInfo> data){ super(context, 0, data); asyncImageLoader = new AsyncImageLoader(); mContext = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewCache holder ; if(convertView==null){ LayoutInflater inflate = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflate.inflate(com.slider.cn.R.layout.list_item , null); holder = new ViewCache(); holder.icon = (ImageView)convertView.findViewById(com.slider.cn.R.id.note_icon); holder.name = (TextView)convertView.findViewById(com.slider.cn.R.id.note_name); holder.date = (TextView)convertView.findViewById(com.slider.cn.R.id.note_date); convertView.setTag(holder); }else{ holder = (ViewCache)convertView.getTag(); } final BookInfo bookInfo = getItem(position); holder.name.setText(bookInfo.getName().toString()); holder.date.setText(bookInfo.getInfo()); holder.icon.setTag(bookInfo.getUri()); // Drawable drawable = asyncImageLoader.loadDrawable(bookInfo.getUri(), new ImageCallback() { @Override public void imageLoaded(Drawable imageDrawable, String imageUrl) { ImageView imageViewByTag = (ImageView) BookListView.this.findViewWithTag(bookInfo.getUri()); if (imageViewByTag!=null) { imageViewByTag.setImageDrawable(imageDrawable); }else { //load image failed from Internet } } }); if(drawable==null){ holder.icon.setImageDrawable(drawable_waiting); }else{ holder.icon.setImageDrawable(drawable); } return convertView; } } static class ViewCache{ ImageView icon; TextView name; TextView date; }
但是,它好像也有一些不完美的地方,比如说可能会造成同时下载二十多个图片的线程(甚至更多),它没有对线程的数量做一个限制。那就使用固定数据的线程池吧,再比如出现重复加在一个图片怎么处理,再比如线程池里线程的优先级安排怎么弄呢?(比如你想要最近添加进入的线程拥有的优先级最高,因为你总是想最先看到当前的界面的内容,而不在乎跳过界面的内容什么时候加在完毕,这里可以说的就太多了,事实上完成上面的已经可以应付大多数应用了)
更多资料:
/article/2784811.html
相关文章推荐
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
- android中ListView性能优化异步加载图片 使滑动效果流畅
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
- Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
- Android ListView滑动过程中图片异步加载优化(配合Volley使用)
- Android ListView 性能优化-----(异步加载图片资源)
- listview异步加载图片(深度优化)(转载)
- listview异步加载图片(深度优化)(转载)
- ListView加载网络图片优化(加二级缓存使滑动流畅)
- iOS性能优化---转载《一》iOS 异步图片加载优化与常用开源库分析
- 在优化listview,滑动停止时异步加载图片,absListView.findViewWithTag(i)返回值为null的原因分析
- ListView性能优化异步加载图片
- ListView性能优化+异步加载图片
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- ListView优化使用imageLoader下载和缓存图片,图片异步加载,解决图片多了之后容易卡顿。
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
- Listview异步加载缓存图片,解决快速滑动问题
- Android之ListView异步加载网络图片(优化缓存机制)
- 又优化了一下 Android ListView 异步加载图片
- listview异步加载图片优化