62、LruCache图片缓存

public class ImageCache {

private ImageCache() {
// use 1/8 of available heap size
cache = new LruCache<String, Bitmap>((int) (Runtime.getRuntime().maxMemory() / 8)) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}

private static ImageCache imageCache = null;

public static synchronized ImageCache getInstance() {
if (imageCache == null) {
imageCache = new ImageCache();
}
return imageCache;

}
private LruCache<String, Bitmap> cache = null;

/**
* put bitmap to image cache
* @param key
* @param value
* @return the puts bitmap
*/
public Bitmap put(String key, Bitmap value){
return cache.put(key, value);
}

/**
* return the bitmap
* @param key
* @return
*/
public Bitmap get(String key){
return cache.get(key);
}
}


另外一种图片缓存的方式就是内存缓存技术。在Android中,有一个叫做 LruCache 类专门用来做图片缓存处理的。
它有一个特点,当缓存的图片达到了预先设定的值的时候,那么 近期使用次数最少的图片 就会被回收掉
步骤:(1)要先设置缓存图片的内存大小,我这里设置为手机内存的1/8,
           手机内存的获取方式: int MAXMEMONRY = (int) (Runtime.getRuntime()  .maxMemory() / 1024);
      (2)LruCache里面的键值对分别是URL和对应的图片
      (3)重写了一个叫做sizeOf的方法,返回的是图片数量。

  1. private LruCache<String, Bitmap> mMemoryCache;
  2. private LruCacheUtils() {
  3.         if (mMemoryCache == null)
  4.             mMemoryCache = new LruCache<String, Bitmap>(
  5.                     MAXMEMONRY / 8) {
  6.                 @Override
  7.                 protected int sizeOf(String key, Bitmap bitmap) {
  8.                     // 重写此方法来衡量每张图片的大小,默认返回图片数量。
  9.                     return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
  10.                 }

  11.                 @Override
  12.                 protected void entryRemoved(boolean evicted, String key,
  13.                         Bitmap oldValue, Bitmap newValue) {
  14.                     Log.v("tag", "hard cache is full , push to soft cache");
  15.                    
  16.                 }
  17.             };
  18.     }
      (4)下面的方法分别是清空缓存、添加图片到缓存、从缓存中取得图片、从缓存中移除。
          移除和清除缓存是必须要做的事,因为图片缓存处理不当就会报内存溢出,所以一定要引起注意。
  1. public void clearCache() {
  2.         if (mMemoryCache != null) {
  3.             if (mMemoryCache.size() > 0) {
  4.                 Log.d("CacheUtils",
  5.                         "mMemoryCache.size() " + mMemoryCache.size());
  6.                 mMemoryCache.evictAll();
  7.                 Log.d("CacheUtils", "mMemoryCache.size()" + mMemoryCache.size());
  8.             }
  9.             mMemoryCache = null;
  10.         }
  11.     }

  12.     public synchronized void addBitmapToMemoryCache(String key, Bitmap bitmap) {
  13.         if (mMemoryCache.get(key) == null) {
  14.             if (key != null && bitmap != null)
  15.                 mMemoryCache.put(key, bitmap);
  16.         } else
  17.             Log.w(TAG, "the res is aready exits");
  18.     }

  19.     public synchronized Bitmap getBitmapFromMemCache(String key) {
  20.         Bitmap bm = mMemoryCache.get(key);
  21.         if (key != null) {
  22.             return bm;
  23.         }
  24.         return null;
  25.     }

  26.     /**
  27.      * 移除缓存
  28.      * 
  29.      * @param key
  30.      */
  31.     public synchronized void removeImageCache(String key) {
  32.         if (key != null) {
  33.             if (mMemoryCache != null) {
  34.                 Bitmap bm = mMemoryCache.remove(key);
  35.                 if (bm != null)
  36.                     bm.recycle();
  37.             }
  38.         }
  39.     }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值