我正在使用Android BitmapFun示例代码来管理我的应用程序中的位图。我在ViewPager中遇到了乱码或重复的图像。当它从缓存中删除BitmapFun示例有一个缓存问题
/**
* Notify the removed entry that is no longer being cached
*/
@Override
protected void entryRemoved(boolean evicted, String key,
BitmapDrawable oldValue, BitmapDrawable newValue) {
if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
// The removed entry is a recycling drawable, so notify it
// that it has been removed from the memory cache
((RecyclingBitmapDrawable) oldValue).setIsCached(false);
} else {
// The removed entry is a standard BitmapDrawable
if (Utils.hasHoneycomb()) {
// We're running on Honeycomb or later, so add the bitmap
// to a SoftRefrence set for possible use with inBitmap later
mReusableBitmaps.add(new SoftReference<Bitmap>(oldValue.getBitmap()));
}
}
}
位图被添加到可重复使用位图列表:我在ImageCache.java跟踪下来到下面的代码。在这种情况下,位图仍然由ViewPager视图使用。当创建后面的视图时,位图(仍在使用中)被重用,并且位图出现在ViewPager中的两个位置。
从LruCache中删除的位图不一定可用于重复使用。我已禁用此代码中的位图重用,并且不再有问题。较低分辨率的图像不会发生此问题,因为在ViewPager的屏幕外限制范围内,位图不会从缓存中删除。我没有60 DPI图像的问题,但经常以160 DPI查看此问题。我认为这将显示在具有更高分辨率图像的原始BitmapFun样本中。
任何人都遇到过这个问题,或者我没有正确理解这个问题?
凯文
谢谢,看来你的解决方案会起作用。我通过不在可重用位图集中设置位图来解决此问题,这可能会导致一些性能损失,但至少可以工作。 –