MemoryCache

               

android 内存缓冲机制:


import java.lang.ref.SoftReference;import java.util.LinkedHashMap;import android.graphics.Bitmap;import android.util.Log;import android.util.LruCache;/** * @ClassName: MemoryCache * @date 2014-6-11 下午05:30:25 * @Description: idea:1,two memory cache 2,file cache 3,network get * @old modify: * @new modify: * @Version: */public class MemoryCache // 1,two memory cache 2,file cache 3,network get public static final String TAG = "memory"// hard cache memory size private final static int HARD_MEMORY_SIZE = 8 * 1024 * 1024;// 8m // sofe cache num private final static int SOFT_CACHE_CAPACITY = 6// hard cache private final static LruCache<String, Object> mHardBitmapCache = new LruCache<String, Object>(   HARD_MEMORY_SIZE) {  @Override  public int sizeOf(String key, Object value) {   return ((Bitmap) value).getRowBytes() * ((Bitmap) value).getHeight();   // return 0;//not recycle memory  }  @Override  protected void entryRemoved(boolean evicted, String key,    Object oldValue, Object newValue) {   Log.v(TAG, "*********hard cache is full , push to soft cache*************");   // 硬引用缓存区满,将一个最不经常使用的oldvalue推入到软引用缓存区   mSoftBitmapCache.put(key, new SoftReference<Object>(oldValue));  } }; @SuppressWarnings("serial"private final static LinkedHashMap<String, SoftReference<Object>> mSoftBitmapCache = new LinkedHashMap<String, SoftReference<Object>>(   SOFT_CACHE_CAPACITY, 0.75f, true) {  @Override  public SoftReference<Object> put(String key, SoftReference<Object> value) {   Log.v(TAG, "*******SoftReference******get put bitmap******");   return super.put(key, value);  }  @Override  protected boolean removeEldestEntry(    Entry<String, SoftReference<Object>> eldest) {   if (size() > SOFT_CACHE_CAPACITY) {    Log.v(TAG, "Soft Reference  limit , purge one");    return true;   }   return false;  } }; // 缓存bitmap public static boolean putBitmapInCache(String key, Object bitmap) {  if (bitmap != null) {   synchronized (mHardBitmapCache) {    Log.v(TAG, "putBitmapInCache...HardBitmapCache.size->" + mHardBitmapCache.size() + " key:" + key);    mHardBitmapCache.put(key, bitmap);   }   return true;  }  return false; } // 从缓存中获取bitmap public static Bitmap getBitmapFromCache(String key) {  Bitmap bitmap = null;  synchronized (mHardBitmapCache) {   bitmap = (Bitmap) mHardBitmapCache.get(key);   if (bitmap != null) {    Log.v(TAG, "getBitmapFrom  Hard  Cache.............key:" + key);    return bitmap;   }  }  // 硬引用缓存区间中读取失败,从软引用缓存区间读取  synchronized (mSoftBitmapCache) {   SoftReference<Object> bitmapReference = mSoftBitmapCache.get(key);   if (bitmapReference != null) {    bitmap = (Bitmap) bitmapReference.get();    if (bitmap != null) {     Log.v(TAG, "getBitmapFrom  Soft Cache..........key:" + key);     return bitmap;    } else {     Log.v(TAG, "soft reference is recycle...");     mSoftBitmapCache.remove(key);    }   }  }  return bitmap; }; public static void freeMemory() {  Log.v(TAG, "freeMemory.............");  mHardBitmapCache.evictAll();  mSoftBitmapCache.clear();  // Clear the cache, calling entryRemoved(boolean, K, V, V) on each  // removed entry. }}


           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值