package com.basic.security.utils.bitmap; import android.graphics.Bitmap; import android.util.LruCache; /** * 内存缓存 * * @author Kevin */ public class MemoryCacheUtils { private LruCache mMemoryCache; public MemoryCacheUtils() { long maxMemory = Runtime.getRuntime().maxMemory() / 8;// 模拟器默认是16M mMemoryCache = new LruCache((int) maxMemory) { @Override protected int sizeOf(String key, Bitmap value) { int byteCount = value.getRowBytes() * value.getHeight();// 获取图片占用内存大小 return byteCount; } }; } /** * 从内存读 * * @param url */ public Bitmap getBitmapFromMemory(String url) { return mMemoryCache.get(url); } /** * 写内存 * * @param url * @param bitmap */ public void setBitmapToMemory(String url, Bitmap bitmap) { mMemoryCache.put(url, bitmap); } }