enjoy the code ImageLoader.ImageCache of Volley

it is so concise.

/**
* Simple cache adapter interface. If provided to the ImageLoader, it
* will be used as an L1 cache before dispatch to Volley. Implementations
* must not block. Implementation with an LruCache is recommended.
*/
public interface ImageCache {
public Bitmap getBitmap(String url);
public void putBitmap(String url, Bitmap bitmap);
}

/**
* 使用LruCache来缓存图片
*/
public class BitmapCache implements ImageLoader.ImageCache {

private LruCache<String, Bitmap> mCache;

public BitmapCache() {
// 获取应用程序最大可用内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
mCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}

@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}

}
posted @ 2017-08-31 16:20  天长地久-无为  阅读(171)  评论(0编辑  收藏  举报