2013.04.03——— android 图片缓存之二LruCache介绍

2013.04.03——— android 图片缓存之二LruCache介绍
参考: http://blog.csdn.net/linghu_java/article/details/8574102

android已经提供了一个lru的缓存数据结构,所以我们已不需要用LinkedhashMap来模拟了

int memClass = ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        int cacheSize = 1024 * 1024 * memClass / 4;  //系统可用内存的1/4
        mLruCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                if (value != null)
                    return value.getRowBytes() * value.getHeight();
                else
                    return 0;
            }
                                                                                          
            @Override
            protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
                if (oldValue != null)
                    // 缓存容量满的时候,会根据LRU算法把最近没有被使用的图片转入此软引用缓存
                    mSoftCache.put(key, new SoftReference<Bitmap>(oldValue));
            }
        };


猜你喜欢

转载自trylovecatch.iteye.com/blog/1841545