您應該在設備中緩存圖像以防止再次加載圖像。
創建一個名爲LruBitmapCache.java的類並添加以下代碼。這個類負責在磁盤上緩存網絡映像。
LruBitmapCache.java
import com.android.volley.toolbox.ImageLoader.ImageCache;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class LruBitmapCache extends LruCache<String, Bitmap> implements
ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
final int cacheSize = maxMemory/8;
return cacheSize;
}
public LruBitmapCache() {
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight()/1024;
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}
創建名爲AppController.java類,並粘貼以下內容。這是一個單例類,用於初始化所需類的全局實例。凌空相關的所有對象都在這裏初始化:
import YourPakageName.LruBitmapCache;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
LruBitmapCache mLruBitmapCache;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
getLruBitmapCache();
mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
}
return this.mImageLoader;
}
public LruBitmapCache getLruBitmapCache() {
if (mLruBitmapCache == null)
mLruBitmapCache = new LruBitmapCache();
return this.mLruBitmapCache;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
現在打開你的AndroidManifest.xml文件,並在標籤中添加Application.java類:
<application
android:name="YourPakageName.app.AppController"> ....</application>
之後,它進入您的適配器,並添加如下代碼:
private Context context;
ImageLoader imageLoader =
YourPakageName.AppController.getInstance().getImageLoader();
private RequestQueue queue;
private LruCache<Integer, Bitmap> imageCache;
和:
public MyCustommAdapter(Context context, int resource, List<YourModel> objects) {
super(context, resource, objects);
this.context = context;
this.postList = objects;
final int maxMemory = (int)(Runtime.getRuntime().maxMemory() /1024);
final int cacheSize = maxMemory/5;
imageCache = new LruCache<Integer, Bitmap>(cacheSize);
queue = Volley.newRequestQueue(context);
}
現在,你可以簡單地得到URL的圖像,並顯示在列表視圖:
imageLoader.get(image_url, new ImageLoader.ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Log.e(TAG, "Image Load Error: " + error.getMessage());
}
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
holder.title_img.setImageBitmap(response.getBitmap());
}
}
});
如果緩存的圖像重用,那麼shud不是一個滯後。此外,當我斷開中間的互聯網時,圖像抖動以任何方式再現。但是,這些都不是這樣的... –
甚至listview可以回收視圖! –
普通列表視圖不回收視圖,Recycler View是ListView的一個擴展,它向您顯示onBindViewHolder,並且此方法創建或回收視圖。 – rik194