图片缓存 与释放

来源:互联网 发布:下载淘宝网到ipad版 编辑:程序博客网 时间:2024/04/29 11:18

图片缓存 与释放,这里使用了一个类

package com.android.systemui.statusbar;


import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;


public class LruMemoryCache {


private static LruMemoryCache instance;


public static LruMemoryCache getInstance() {
if (instance == null) {
instance = new LruMemoryCache();
}
return instance;
}


public LruMemoryCache() {
super();
}


private final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
private final int cacheSize = maxMemory / 8;
private LruCache<String, Bitmap> mMemoryCache = new LruCache<String, Bitmap>(
cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
}
};


public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (key!=null&&bitmap!=null&&!bitmap.isRecycled()&&getBitmapFromMemCache(key)==null) {
// Log.e("LruMemoryCache",
// "mMemoryCache.size()==" + mMemoryCache.size() + " <= "
// + mMemoryCache.maxSize());
mMemoryCache.put(key, bitmap);
}
}


public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}


public void clear() {
mMemoryCache.evictAll();
}
}

图片预取缓存策略是内存缓存(硬引用LruCache、软引用SoftReference<Bitmap>)


0 0
原创粉丝点击