Androidアプリケーションレベルのピクチャキャッシュ
1077 ワード
キャッシュはSoftReferenceに使用する必要があります
Step 1:ImageCahceの定義
Step 2:アプリケーションでImageCacheメンバー変数を定義する
Step 1:ImageCahceの定義
public class ImageCache{
private final HashMap> mSoftCache;
public ImageCache(Context context) {
mSoftCache = new HashMap>();
}
public Bitmap get(String url) {
final SoftReference ref = mSoftCache.get(url);
if (ref == null) {
return null;
}
Bitmap bitmap = ref.get();
if (bitmap == null) {
mSoftCache.remove(url);
}
return bitmap;
}
public void put(String url, Bitmap bitmap) {
mSoftCache.put(url, new SoftReference(bitmap));
}
private void flush() {
mSoftCache.clear();
}
}
Step 2:アプリケーションでImageCacheメンバー変数を定義する
private ImageCache mImageCache;
public ImageCache getImageCache() {
if (mImageCache == null) {
mImageCache = new ImageCache(this);
}
return mImageCache;
}