Android进阶篇系统缓存(一)

/**
 * @author gongchaobin
 * 
 * LruCache缓存Bitmap
 * (基于内存的缓存,读取时间比较快)
 */
public class LruCacheUtil {
    private static final String TAG = LruCacheUtil.class.getSimpleName();
    private int memClass;
    private LruCache<String,Bitmap> mMemoryCache;//高速缓存(系统)
    public static final String BITMAP = "bitmap";
    
    public LruCacheUtil(Context context){
        memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        final int cacheSize = 1024 * 1024 * memClass / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize);
    }
    
    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }
    
    public Bitmap getBitmapFromMemCache(String key) {
        return mMemoryCache.get(key);
    }
}
原文地址:https://www.cnblogs.com/gongcb/p/2800946.html