我的建议是使用单例位图缓存,以便在您的应用程序的整个生命周期中都可以使用此缓存。
public class BitmapCache implements ImageCache {
private LruCache<String, Bitmap> mMemoryCache;
private static BitmapCache mInstance;
private BitmapCache(Context ctx) {
final int memClass = ((ActivityManager) ctx
.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/16th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 16;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}
public static BitmapCache getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new BitmapCache(ctx);
}
return mInstance;
}
@Override
public Bitmap getBitmap(String url) {
return mMemoryCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mMemoryCache.put(url, bitmap);
}
}