弄明白android網路程式庫之Volley listView載入大量圖片,
一、載入一張圖片
Volley是通過ImageRequest來擷取網路上的圖片的,指定一個URL,返回一個已經編碼號的bitmap。當然它也提供了其他便利特性,比如調整圖片大小。使用它它主要的好處是 Volley的計劃線程確保了片編碼、調整大小等昂貴的操作自動地在一個背景工作執行緒完成,不會給主線程帶來太多的麻煩和幹擾。
a cannedrequest for getting an image at a given URL and calling back with a decodedbitmap. It also provides convenience features like specifying a size to resizeto. Its main benefit is that Volley's thread scheduling ensures that expensiveimage operations (decoding, resizing) automatically happen on a worker thread.
(1)初始化一個請求隊列,最好寫在application(記得在manifest裡修改application)裡,如此一開始就啟動了。
public class VolleyApplication extends Application {@Overridepublic void onCreate() { super.onCreate();RequestManager.initVolley(this);}}
這裡的RequestManager是我自訂的要求管理類,專門用來管理請求的。裡面就有一個請求隊列,然後在初始化方法裡執行個體化。
private static RequestQueue mRequestQueue;public RequestManager() {// TODO Auto-generated constructor stub} public static void initVolley(Context context){mRequestQueue = Volley.newRequestQueue(context);}
(2)然後編寫圖片請求
//Retrieves an image specified by the URL, displays it in the UI. request = new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { singleImageView.setImageBitmap(response); } }, 0, 0, null, new Response.ErrorListener() { public void onErrorResponse(VolleyError error) { Toast.makeText(ImageActivity.this, "download image error", Toast.LENGTH_LONG).show(); } })
(3)執行這個載入圖片的請求:
RequestManager.addRequest(request);
(4)在manifest中添加網路訪問的許可權。
<uses-permission android:name="android.permission.INTERNET" />
當點擊下載一張大圖片的時候,瞬間就列印下面的記憶體不足的警告了。
因此多張圖片下載就不能多次重複上次操作了。得使用ImageLoader。看第二點
二、多張圖片下載
通過執行個體化一個ImageRequest可以下載到一張圖片,問題是如果多張圖片呢?還是這麼玩嗎?你可以看到一張就出現記憶體不足,那麼多張必然出現OOM。因此,Volley提供了ImageLoader and NetworkImageView 來下載大量圖片,比如listview中。
(1)建立記憶體緩衝,android提供記憶體緩衝類庫,LruCache;注意還有一個類提供了LruCache,但是我們需要的是android.support.v4.util.LruCache
建立一個類繼承自 LruCache類,並實現 ImageCache介面。
import android.graphics.Bitmap;import android.support.v4.util.LruCache;import com.android.volley.toolbox.ImageLoader;public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache{public BitmapLruCache(int maxSize) {super(maxSize);} @Overrideprotected int sizeOf(String key, Bitmap bitmap) {return bitmap.getRowBytes() * bitmap.getHeight();} @Overridepublic Bitmap getBitmap(String url) {return get(url);} @Overridepublic void putBitmap(String url, Bitmap bitmap) {put(url, bitmap);}}
(2)執行個體化ImageLoader
android系統給每個應用程式分配的記憶體大小是不一樣的。通過 Runtime.getRuntime().maxMemory()或者
((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE))
.getMemoryClass();
可以獲得被分配的具體最大記憶體是多少。注意到兩個單位是不一樣的。
private static ImageLoader mImageLoader; //get the app's available memory given by system,note that its unit is MB int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass(); //or you can get the memory value by this//memClass = (int) Runtime.getRuntime().maxMemory() ;// Use 1/8th of the available memory for this memory cache.int cacheSize = 1024 * 1024 * memClass /4;mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize)); /** * get imageLoader */public static ImageLoader getImageLoader(){if(mImageLoader != null){return mImageLoader;}else{throw new IllegalStateException("the ImageLoader is null and not be initialized! ");}}
(3)在Adapter中通過NetworkImageView的 setImageUrl方法載入ImageLoader下載URL指定的圖片。
holder.showImage.setImageUrl(imageArrayList.get(position), imageLoader);
注意一點,就是由於普通imageview中沒有使用 ImageLoader的方法,因此必須使用 Volley開發的繼承自 ImageView的子類NetworkImageView才能使用。
如下:
測試Demo下載