我有一个列表视图,其中显示图像,这些图像下载和正确缓存.当我向下滚动列表并加载新项目时,我想回收最近最少使用的项目以节省内存.我已经成功实现了LRU Map,并在位图上调用了回收,这些位图在屏幕上是不可见的.以下是代码的一部分:
imageView是一个ViewGroup
public void recycleImage(int res) {
if (imageView != null) {
imageView.setBackgroundDrawable(null);
if (res > 0)
imageView.setBackgroundResource(res);
}
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
Log.i(TAG, "Bitmap recycled");
}
}
这些值保存在适配器的getView方法中.问题是,当我回收图像时,我可以看到屏幕上可见的图像也会丢失(请注意我只回收我知道的图像不在屏幕上).
当我在listview上回收其他人的位图时,有没有人知道为什么我会丢失当前显示的图像?
这是我访问存储的位图或下载它的地方.
if (mImageMap.containsKey(url)) {
ImageCacheModel cache = mImageMap.get(url);
return cache.getBitmap();
} else if (cacheManager.isUrlCached(url)) {
mImageMap.put(url, new ImageCacheModel(imageView, cacheManager.getCachedBitmapOrNull(url, 2)));
ImageCacheModel cache = mImageMap.get(url);
return cache.getBitmap();
} else {
cacheManager.addUrlToQueue(url, this, true);
}