LinkedHashMap.Entry<String, Bitmap> eldest) {
if (size() > HARD_CACHE_CAPACITY) {
// Entries push-out of hard reference cache are transferred to
// soft reference cache
mSoftBitmapCache.put(eldest.getKey(),
new SoftReference<Bitmap>(eldest.getValue()));
return true;
} else
return false;
}
};
/*
* SoftReference 指到的对象,即使没有任何 Direct Reference,也不会被清除。 一直要到 JVM 内存不足时且 没有
* Direct Reference 时才会清除,SoftReference 是用来设计 object-cache 之用的。softreference 不但可以把对象
* cache 起来,也不会造成内存不足的错误 (OutOfMemoryError)
*/
private final static ConcurrentHashMap<String, SoftReference<Bitmap>> mSoftBitmapCache = new ConcurrentHashMap<String, SoftReference<Bitmap>>(
HARD_CACHE_CAPACITY);
private final static Handler mPurgeHandler = new Handler();
private final static Runnable mPurgerRunnable = new Runnable() {
@Override
public void run() {
clearCache();
}
};
private static Bitmap getBitmapFromCache(String url) {
synchronized (mHardBitmapCache) {
final Bitmap bitmap = mHardBitmapCache.get(url);
if (bitmap != null) {
mHardBitmapCache.remove(url);
mHardBitmapCache.put(url, bitmap);
return bitmap;
}
}
SoftReference<Bitmap> bitmapReference = mSoftBitmapCache.get(url);
if (bitmapReference != null) {
final Bitmap bitmap = bitmapReference.get();
if (bitmap != null) {
return bitmap;
} else {
mSoftBitmapCache.remove(url);
}
}
return null;
}
public static void clearCache() {
mHardBitmapCache.clear();
mSoftBitmapCache.clear();
}
private static void resetPurgeTimer() {
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-49624-2.html
男饭力挺到底