我建议使用Singleton位图缓存,这样这个缓存将在您的应用程序的整个生命周期中可用。
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);
}
}
非常感谢,顺便说一句,你如何确定缓存的大小合适?他谈到它的屏幕尺寸功能似乎合乎逻辑,但我怎样才能使它更精确? – urSus
@Vlasto Benny Lava,我不确定,但是像'N * screen_width * screen_height'和'N〜10'这样的东西对我来说似乎是合乎逻辑的。 –
,因为现在发生的事很好,但是如果图像离开屏幕并返回,图像再次加载,所以我认为它被踢出了内存缓存?任何想法可能是什么? – urSus