Android缓存的一个Demo

Android加载多张图片容易出现oom异常,而用弱引用保存图片容易被系统回收。上网查了一些资料,自己写了一个强弱一起用的Demo,仅供产考。

代码:


import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;

import java.lang.ref.SoftReference;
import java.util.LinkedHashMap;

/**
 * 缓存图片的一个容器
 * Created by yyw on 2016/1/6.
 */
public class BitmapCache {
	/**软引用缓存的集合大小**/
    private static final int SOFT_CACHE_CAPACITY = 10;
	/**单例模式**/
    private static volatile BitmapCache BITMAP_CACHE;
	/**Android提供的硬引用**/
    private  LruCache<String, Bitmap> mHardBitmapCache;
    private  LinkedHashMap<String, SoftReference<Bitmap>> mSoftBitmapCache =
            new  LinkedHashMap<String, SoftReference<Bitmap>>(SOFT_CACHE_CAPACITY , 0.75f, true){
				/**是否删除旧的**/
                @Override
                protected boolean removeEldestEntry(Entry<String, SoftReference<Bitmap>> eldest) {
                    if (size() > SOFT_CACHE_CAPACITY){
                        return true;
                    }
                    return super.removeEldestEntry(eldest);
                }
            };
    private BitmapCache() {
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        // 使用最大可用内存值的1/8作为缓存的大小。
        int cacheSize = maxMemory / 8;
        mHardBitmapCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            public int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight()/1024;
            }
            @Override
            protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
                Log.v("tag", "hard cache is full , push to soft cache");
                //硬引用缓存区满,将一个最不经常使用的oldvalue推入到软引用缓存区
                mSoftBitmapCache.put(key, new SoftReference<Bitmap>(oldValue));
            }
        };
    }

    public static BitmapCache getInstance() {
        if (BITMAP_CACHE == null) {
            synchronized (BitmapCache.class) {
                if (BITMAP_CACHE == null) {
                    BITMAP_CACHE = new BitmapCache();
                }
            }
        }
        return BITMAP_CACHE;
    }

    /**
     * 从缓冲当中获取图片
     * @param key
     * @return
     */
    public Bitmap getBitmap(String key){
        synchronized(mHardBitmapCache){
            final Bitmap bitmap = mHardBitmapCache.get(key);
            if(bitmap != null)
                return bitmap;
        }
        //硬引用缓存区间中读取失败,从软引用缓存区间读取
        synchronized(mSoftBitmapCache){
            SoftReference<Bitmap> bitmapReference = mSoftBitmapCache.get(key);
            if(bitmapReference != null){
                final Bitmap bitmap2 = bitmapReference.get();
                if(bitmap2 != null)
                    return bitmap2;
                else{
                    Log.v("tag", "soft reference 已经被回收");
                    mSoftBitmapCache.remove(key);
                }
            }
        }
        return null;
    }
    //缓存bitmap
    public boolean putBitmap(String key, Bitmap bitmap){
        if(bitmap != null){
            synchronized(mHardBitmapCache){
                mHardBitmapCache.put(key, bitmap);
            }
            return true;
        }
        return false;
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值