1,实现效果
-
内存不足的时候使用Map集合强引用 ,不容易被释放,我使用SoftReference
2,实现逻辑
引用
【1】使用软引用进行,叫系统内存不足时能快点进行释放 ,软引用在版本更改后会被很快的释放
package com.utils.pic;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class MemCacheUtils {
// SoftReference<> 软引用
// WeakReference<T> 弱引用
// PhantomReference<T> 虚引用
// 存的是bitmap对应的软引用
HashMap<String, SoftReference<Bitmap>> mMaps;// 存图片
public MemCacheUtils() {
super();
mMaps = new HashMap<String, SoftReference<Bitmap>>();
}
public void setCache(String url, Bitmap bimBitmap) {
SoftReference<Bitmap> softReference = new SoftReference<Bitmap>(bimBitmap);
}
public Bitmap getCache(String url) {
Bitmap bitmap = null;
SoftReference<Bitmap> softReference = mMaps.get(url);
if (softReference != null) {
// 取出bitmap
bitmap = softReference.get();
}
return bitmap;
}
}