package com.martin.simpledevelop.utils.cache; import android.content.Context; import android.graphics.Bitmap; import android.util.LruCache; import com.android.volley.toolbox.ImageLoader; import com.martin.simpledevelop.utils.file.SaFileUtils; import com.martin.simpledevelop.utils.log.SaLogUtils; import java.io.File; /** * @Description 基于Volley的图片缓存 * @File ${FILE_NAME} * @Package com.martin.simpledevelop.utils.cache * @Date 2015/7/421:14 * @Author Donghongyu 1358506549@qq.com * @Version v1.0.0 */ public class BitmapCache implements ImageLoader.ImageCache { private final String TAG=BitmapCache.class.getSimpleName(); /** * 一级缓存 */ private LruCache<String, Bitmap> mCache; /** * 二级缓存 */ private ACache mACache = null; public BitmapCache(Context context) { // 构建指定文件夹的缓存 mACache = ACache .get(new File(SaFileUtils.getCacheDownloadDir(context))); int maxSize = 10 * 1024 * 1024; mCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap value) { // 在每次存入缓存的时候调用 return value.getByteCount(); } }; } @Override public Bitmap getBitmap(String url) { Bitmap bitmap = mCache.get(url); // SaLogUtils.e(TAG,"一级缓存bitmap>>>"+bitmap); if (bitmap == null){ // SaLogUtils.e("","内存中没有图片,去内存卡查找。。。"); bitmap = getBitmapFromDisk(url); // if (bitmap!=null) // //进行一级缓存 // mCache.put(url, bitmap); // SaLogUtils.e(TAG,"二级缓存bitmap>>>"+bitmap); // SaLogUtils.e("","去内存卡查找。。。"+bitmap); } return bitmap; } @Override public void putBitmap(String url, Bitmap bitmap) { if (bitmap != null) { //进行一级缓存 mCache.put(url, bitmap); // SaLogUtils.e("", "putBitmap.." + mCache.size()); //进行二级缓存 mACache.put(url, bitmap); } } /** * 从sd卡中获取缓存 * * @param url * @return */ public Bitmap getBitmapFromDisk(String url) { return mACache.getAsBitmap(url); } }