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 getpublic static final String TAG = "memory";// hard cache memory sizeprivate final static int HARD_MEMORY_SIZE = 8 * 1024 * 1024;// 8m// sofe cache numprivate final static int SOFT_CACHE_CAPACITY = 6;// hard cacheprivate final static LruCache<String, Object> mHardBitmapCache = new LruCache<String, Object>(HARD_MEMORY_SIZE) {@Overridepublic int sizeOf(String key, Object value) {return ((Bitmap) value).getRowBytes() * ((Bitmap) value).getHeight();// return 0;//not recycle memory}@Overrideprotected 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) {@Overridepublic SoftReference<Object> put(String key, SoftReference<Object> value) {Log.v(TAG, "*******SoftReference******get put bitmap******");return super.put(key, value);}@Overrideprotected boolean removeEldestEntry(Entry<String, SoftReference<Object>> eldest) {if (size() > SOFT_CACHE_CAPACITY) {Log.v(TAG, "Soft Reference  limit , purge one");return true;}return false;}};// 缓存bitmappublic 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;}// 从缓存中获取bitmappublic 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.}}

更多相关文章

  1. 【Android 内存优化】Bitmap 内存缓存 ( Bitmap 内存复用 | 弱引
  2. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理
  3. Android的Bitmap和缓存机制
  4. 缓存工具类封装
  5. Android内存缓存图片的标准方法
  6. Android 缓存的工具类
  7. Android临时数据缓存方法
  8. android完整资讯App、Kotlin新闻应用MVP + RxJava + Retrofit +
  9. Android第三方异步网路加载库AsyncHttpClient内部实现缓存策略

随机推荐

  1. 数据库语言分类DDL,DCL,DML
  2. mac安装mysql的两种方法(含配置)
  3. 在创建多对多关系后,Sequelize Node.js新
  4. mysql批量删除相同前缀的表和修改表名
  5. MySql_数据库触发器的使用
  6. MySQL数据库root账户的设置和管理
  7. 版本5及更高版本中MySQL数据库的最大大小
  8. Mysql PARTITION 数据表分区技术
  9. mysql批量结束线程
  10. MySQL数据库表名、列名、别名区分大小写