android图片缓存之softReference

softReference缓存的对象,只有当虚拟机内存不足的才会回收,很好的避免了OOM。


package cache;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.concurrent.ConcurrentHashMap;
import android.graphics.Bitmap;
import android.os.Handler;
import android.widget.ImageView;


public class FileServiceByMemoary extends FileServiceAbstractDetails {


@Override
public void loadFileToMap() {
// for memory, nothing to do.
}


@Override
public void getBitmap(String url, ImageView imageView,
Bitmap defaultBitmap, Integer scrollState) {


resetPurgeTimer();
// scrollState 0:停止状态,需要加载图片(cache -> network)
// scrollState 1/2:滚动状态,此时就用cache图片,如果cache没有就用默认defaultBitmap
Bitmap bitmap = getBitmapFromCache(url);
if (scrollState != SCROLL_STATUS_STOP) {// 处于滚动状态
if (bitmap == null) {
imageView.setImageBitmap(defaultBitmap);
} else {
imageView.setImageBitmap(bitmap);
}
return;
}


if (bitmap == null) {
forceDownload(url, imageView, defaultBitmap);
} else {
cancelPotentialDownload(url, imageView);
imageView.setImageBitmap(bitmap);
}
}


// public void setMode(Mode mode) {
// this.mode = mode;
// clearCache();
// }


private static final int HARD_CACHE_CAPACITY = 30;
private static final int DELAY_BEFORE_PURGE = 10 * 1000; // in milliseconds
private final static HashMap<String, Bitmap> mHardBitmapCache = new LinkedHashMap<String, Bitmap>(
HARD_CACHE_CAPACITY, 0.75f, true) {
private static final long serialVersionUID = 1L;


@Override
protected boolean removeEldestEntry(
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 之用的。 不但可以把对象
* 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() {
mPurgeHandler.removeCallbacks(mPurgerRunnable);
mPurgeHandler.postDelayed(mPurgerRunnable, DELAY_BEFORE_PURGE);
}


@Override
public boolean getFile(String path, String targetPath) {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean clearFile(String path) {
// TODO Auto-generated method stub
return false;
}


@Override
public void saveBitmap(String url, Bitmap bitmap) {
if (bitmap != null) {
synchronized (mHardBitmapCache) {
mHardBitmapCache.put(url, bitmap);
}
}
}


@Override
public int getCurrentCacheBitmapNumbers() {
int numbers = 0;
if (mHardBitmapCache != null) {
numbers = mHardBitmapCache.size();
}
if (mSoftBitmapCache != null) {
numbers = numbers + mSoftBitmapCache.size();
}
return numbers;
}


@Override
public boolean clearCacheBitmaps() {
try {
mHardBitmapCache.clear();
mSoftBitmapCache.clear();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值