BitmapFun样品具有缓存问题 [英] BitmapFun sample has a caching issue

查看:108
本文介绍了BitmapFun样品具有缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Android BitmapFun样品code来管理我的应用程序的位图。我一直在经历了ViewPager乱码或复制的图像。我在ImageCache.java跟踪下来以下code:

I am using the Android BitmapFun sample code to manage bitmaps in my application. I have been experiencing garbled or duplicated images in a ViewPager. I have tracked this down to the following code in 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()));
                    }
                }
            }

当它被从缓存中移除位图被添加到可重复使用的位图列表。在这种情况下,位图仍然在由ViewPager视图用途。当稍后视图创建​​位图(仍然在使用)被再利用和位图显示在ViewPager

The bitmap is added to the reusable bitmap list when it is removed from the cache. In this case the bitmap is still in use by a ViewPager view. When a later view is created the bitmap (still in use) is reused and the bitmap appears in two positions in the ViewPager.

这是从LruCache除去位图不一定是可以重复使用。我已经停用​​位图的重用这个code和我不再有一个问题。因为同时的ViewPager的屏幕外极限的范围内的位图不从缓存中移除这个问题不会低分辨率图像发生。我没有与60 DPI图像的问题,而是看到这个问题经常在160 DPI。我认为这将具有更高分辨率的图像原始BitmapFun样品中显示出来。

A bitmap that is removed from the LruCache isn't necessarily available for reuse. I have disabled the reuse of bitmaps in this code and am no longer having an issue. This problem doesn't occur with lower resolution images because the bitmaps aren't removed from the cache while in the range of the ViewPager's offscreen limit. I don't have an issue with 60 DPI images but see this issue frequently at 160 DPI. I think this would show up in the original BitmapFun sample with higher resolution images.

其他人遇到这个问题,或者我不正确理解这个问题?

Anyone else experienced this problem or I am not understanding the issue properly?

凯文

推荐答案

我觉得用code中的问题是该行

What I think the problem with the code is in the line

mReusableBitmaps.add(new SoftReference<Bitmap>(oldValue.getBitmap()));

该行还指出,从LRU缓存中移除到一个可重复使用的位图设置成用于inBitmap重新使用的位图。它不检查它是否仍正在使用一个ImageView的或没有。如果试图再次使用该仍在使用由ImageView的位图,底层位图将与另一个位使它不再有效代替。我的建议是跟踪是否位图仍在使用由ImageView的其加入到可重复使用的位图组之前。我创建了针对此问题的样本github上项目。告诉我你的想法和我的解决方案。

That line adds a bitmap that was removed from LRU cache to a reusable bitmap set to be used for inBitmap re-use. It doesn't check whether it is still being used by an ImageView or not. If you try to re-use a bitmap that is still being used by an ImageView, the underlying bitmap will be replaced with another bitmap making it not valid anymore. My suggestion is to track whether a bitmap is still being used by an ImageView before adding it to the reusable bitmap set. I've created a sample github project for this issue. Tell me what you think with my solution.

这篇关于BitmapFun样品具有缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆