//内存缓存图片 private final int maxMemory = (int) Runtime.getRuntime().maxMemory();// 获取当前应用程序所分配的最大内存 private final int cacheSize = maxMemory / 5;// 只分5分之一用来做图片缓存 private LruCache<String, Bitmap> mLruCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) {// 复写sizeof()方法 // replaced by getByteCount() in API 12 return bitmap.getRowBytes() * bitmap.getHeight() / 1024; // 这里是按多少KB来算 } };
任务中缓存后 调用方法
// 调用LruCache的put 方法将图片加入内存缓存中,要给这个图片一个key 方便下次从缓存中取出来 private void addBitmapToMemoryCache(String key, Bitmap bitmap) { LogUtil.i(TAG, "addBitmapToMemoryCache"+"==="+key+"======="); if (getBitmapFromMemoryCache(key) == null) { lruCache.put(key, bitmap); } }//取出对象 public Bitmap getBitmapFromMemoryCache(String key) { return lruCache.get(key); }
转载于:https://www.cnblogs.com/childyngy/p/5474683.html