slider

还是菜鸟
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

android里图片下载工具类AsyncImageLoader分析

Posted on 2011-11-22 21:21  slider  阅读(9405)  评论(7编辑  收藏  举报

这段时间看见很多人做listview(比如模拟新浪客户端)用到这一个图片下载的类,我也不知道这个类到底是哪个大神写的,反正我使用这个类的时候接手别人的,刚开始,感觉这个类写的听不错,比我写的AsyncImageTask.java好多了,先说说我最开始写的吧,也算是抛砖引玉:

public class AsyncImageTask extends AsyncTask<String, Void, InputStream>{
private ImageView imageView;
public AsyncImageTask(ImageView imageView){
this.imageView = imageView;
}
@Override
protected InputStream doInBackground(String... params) {

InputStream inputStream = null;
try {
URL url = new URL(params[0]);
inputStream = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream result) {
if(imageView!=null && result!=null){
Bitmap bmp = BitmapFactory.decodeStream(result);
imageView.setImageBitmap(bmp);
}
super.onPostExecute(result);
}
}

我写完的时候感觉就挺别扭的的,能满足我当时的需求,也就这么用了(有哪位高手指点其中的缺点哈),这个类里,我们只需要传入显示图片的ImageView和图片的网络地址,当图片下载完成后,显示出来就可以了。后来我的主管把它换掉了,用了AsyncImageLoader.java这个类,仔细看看,写的确实不错。不说废话,先把这个类的代码贴上去(AsyncImageLoader.java):

  

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;
}

   但是,它好像也有一些不完美的地方,比如说可能会造成同时下载二十多个图片的线程(甚至更多),它没有对线程的数量做一个限制。那就使用固定数据的线程池吧,再比如出现重复加在一个图片怎么处理,再比如线程池里线程的优先级安排怎么弄呢?(比如你想要最近添加进入的线程拥有的优先级最高,因为你总是想最先看到当前的界面的内容,而不在乎跳过界面的内容什么时候加在完毕,这里可以说的就太多了,事实上完成上面的已经可以应付大多数应用了)

  最近刚开始写博客,有不妥的地方,欢迎指点。


参考文章:

http://www.cnblogs.com/enricozhang/archive/2010/06/12/1756904.html

http://www.oschina.net/code/snippet_176897_7207