弄明白android網絡庫之Volley listView加載大量圖片
來源:程序員人生 發布時間:2015-01-12 09:00:36 閱讀次數:6626次
1、加載1張圖片
Volley是通過ImageRequest來獲得網絡上的圖片的,指定1個URL,返回1個已編碼號的bitmap。固然它也提供了其他便利特性,比如調劑圖片大小。使用它它主要的好處是
Volley的計劃線程確保了如圖片編碼、調劑大小等昂貴的操作自動地在1個工作線程完成,不會給主線程帶來太多的麻煩和干擾。
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)初始化1個要求隊列,最好寫在application(記得在manifest里修改application)里,如此1開始就啟動了。
public class VolleyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
RequestManager.initVolley(this);
}
}
這里的RequestManager是我自定義的要求管理類,專門用來管理要求的。里面就有1個要求隊列,然后在初始化方法里實例化。
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" />
當點擊下載1張大圖片的時候,瞬間就打印下面的內存不足的正告了。

因此多張圖片下載就不能屢次重復上次操作了。得使用ImageLoader。看第2點
2、多張圖片下載
通過實例化1個ImageRequest可以下載到1張圖片,問題是如果多張圖片呢?還是這么玩么?你可以看到1張就出現內存不足,那末多張必定出現OOM。因此,Volley提供了ImageLoader
and NetworkImageView
來下載大量圖片,比如listview中。
(1)建立內存緩存,android提供內存緩存類庫,LruCache;注意還有1個類提供了LruCache,但是我們需要的是android.support.v4.util.LruCache
新建1個類繼承自 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);
}
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
(2)實例化ImageLoader
android系統給每一個利用程序分配的內存大小是不1樣的。通過 Runtime.getRuntime().maxMemory()或
((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE))
.getMemoryClass();
可以取得被分配的具體最大內存是多少。注意到兩個單位是不1樣的。
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);
注意1點,就是由于普通imageview中沒有使用
ImageLoader的方法,因此必須使用 Volley開發的繼承自
ImageView的子類NetworkImageView才能使用。
效果圖以下:

測試Demo下載
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈