我试图在android中使用LruCache来缓存一些图像,但它不是缓存
这是代码
int cacheSize1 = 4 * 1024 * 1024; // 4MiB
bitmapCache = new LruCache(cacheSize1) {
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}};
这里有其他方法
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
bitmapCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
Bitmap b = (Bitmap)bitmapCache.get(key);
return b;
}
这里我正在使用它们这是我的代码
for (int i = 0; i < HomeActivity.globalObj.categoriesList.size(); i++) {
ImageView iv = new ImageView(getApplicationContext());
Bitmap bb = getBitmapFromMemCache(HomeActivity.globalObj.categoriesList.get(i).name);
if (bb != null) {
iv.setImageBitmap(bb);
imageViewList.add(iv);
}
else{
Bitmap b = getImageBitmap(HomeActivity.globalObj.categoriesList.get(i).large_image);
addBitmapToMemoryCache(HomeActivity.globalObj.categoriesList.get(i).name, b);
iv.setImageBitmap(b);
imageViewList.add(iv);
}
}