android裡圖片下載工具類AsyncImageLoader分析

來源:互聯網
上載者:User

這段時間看見很多人做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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.