歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android裡圖片下載工具類AsyncImageLoader分析

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

日期:2017/3/1 10:55:52   编辑:Linux編程

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

  但是,它好像也有一些不完美的地方,比如說可能會造成同時下載二十多個圖片的線程(甚至更多),它沒有對線程的數量做一個限制。那就使用固定數據的線程池吧,再比如出現重復加在一個圖片怎麼處理,再比如線程池裡線程的優先級安排怎麼弄呢?(比如你想要最近添加進入的線程擁有的優先級最高,因為你總是想最先看到當前的界面的內容,而不在乎跳過界面的內容什麼時候加在完畢,這裡可以說的就太多了,事實上完成上面的已經可以應付大多數應用了)

Copyright © Linux教程網 All Rights Reserved