Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BitmapFun示例存在缓存问题_Android_Caching_Bitmap - Fatal编程技术网

Android BitmapFun示例存在缓存问题

Android BitmapFun示例存在缓存问题,android,caching,bitmap,Android,Caching,Bitmap,我正在使用Android BitmapFun示例代码在我的应用程序中管理位图。我在ViewPager中遇到过图像混乱或重复的情况。我在ImageCache.java中找到了以下代码: /** * Notify the removed entry that is no longer being cached */ @Override protected void entryRe

我正在使用Android BitmapFun示例代码在我的应用程序中管理位图。我在ViewPager中遇到过图像混乱或重复的情况。我在ImageCache.java中找到了以下代码:

           /**
             * 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()));
                    }
                }
            }
/**
*通知不再缓存的已删除项
*/
@凌驾
受保护的void entryRemoved(布尔逐出,字符串键,
BitmapDrawable旧值,BitmapDrawable新值){
if(RecyclingBitmapDrawable.class.isInstance(旧值)){
//删除的条目是可回收的,请通知它
//它已从内存缓存中删除
((RecyclingBitmapDrawable)oldValue).setIsCached(false);
}否则{
//删除的条目是标准的位图绘制条目
if(Utils.hasHoneycomb()){
//我们正在蜂窝或更高版本上运行,因此请添加位图
//到SoftReference集,以便以后可能与inBitmap一起使用
mReusableBitmaps.add(新的软引用(oldValue.getBitmap());
}
}
}
从缓存中删除位图时,会将其添加到可重用位图列表中。在这种情况下,ViewPager视图仍在使用位图。创建以后的视图时,位图(仍在使用)将被重用,并且位图将显示在ViewPager中的两个位置

从LruCache中删除的位图不一定可以重用。我已禁用此代码中位图的重用,不再有问题。低分辨率图像不会出现此问题,因为位图在ViewPager的屏幕外限制范围内时不会从缓存中删除。我没有60 DPI图像的问题,但在160 DPI时经常看到这个问题。我认为这会在原始的BitmapFun示例中显示,并具有更高分辨率的图像

有没有其他人遇到过这个问题,或者我没有正确理解这个问题


凯文

我认为代码的问题在于

mReusableBitmaps.add(new SoftReference<Bitmap>(oldValue.getBitmap()));
mReusableBitmaps.add(新的软参考(oldValue.getBitmap());

该行将从LRU缓存中删除的位图添加到可重用位图集中,以供inBitmap重用。它不会检查ImageView是否仍在使用它。如果尝试重新使用ImageView仍在使用的位图,则基础位图将替换为另一个位图,使其不再有效。我的建议是,在将位图添加到可重用位图集中之前,跟踪它是否仍被ImageView使用。我已经为这个问题创建了一个示例github。告诉我您对我的解决方案的看法。

谢谢,看来您的解决方案会奏效。我解决了这个问题,没有在可重用位图集中设置位图,这可能会导致一些性能损失,但至少可以工作。