ImageView的:自动回收​​位图,如果ImageView的不可见(内滚动型) [英] ImageView: automatically recycle bitmap if ImageView is not visible (within ScrollView)

查看:606
本文介绍了ImageView的:自动回收​​位图,如果ImageView的不可见(内滚动型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在寻找的ImageView的来源一段时间,但我还没有想出一个钩子做到这一点呢。

So, I've been looking at the ImageView source for a while, but I haven't figured out a hook to do this yet.

问题:有,比方说,里面滚动型30 400x800的图像(图像的数量是可变的)。由于他们完全适合在屏幕上,他们将采取1.3 MB的RAM。

The problem: having, let's say, 30 400x800 images inside a ScrollView (the number of images is variable). Since they fit exactly in the screen they will take 1.3 MB of RAM each.

我要的是:要能装载/卸载位图是目前内滚动型可见ImageViews。如果用户滚动和位图不再可见(内距离阈值)位图应该回收,以使存储器可在同一滚动型被其他的位图。我做下采样和所有的,所以不用担心在那里。奖励积分,如果你做到这一点,只能延长ImageView的(我想不乱用滚动型如果可能的话)。

What I want: to be able to load/unload bitmaps for the ImageViews that are currently visible inside a ScrollView. If the user scrolls and the bitmap is no longer visible (within a distance threshold) the bitmap should be recycled so that the memory can be used by other bitmaps in the same ScrollView. I'm doing downsampling and all that, so no worries there. Bonus points if you do this by only extending ImageView (I would like to not mess with the ScrollView if possible).

摘要:我可以使图像加载之后才ImageView的变得可见(使用一个聪明的伎俩),但我不知道什么时候它们卸载

Summary: I can make the images load only after the ImageView becomes visible (using a clever trick), but I don't know when to unload them.

注:我不能用一个ListView由于其他原因易用性做到这一点

Notes: I can't do this with a ListView due to other usability reasons.

推荐答案

当你想回收你的位图调用此方法。

Call this method when you want to recycle your bitmaps.

public static void recycleImagesFromView(View view) {
            if(view instanceof ImageView)
            {
                Drawable drawable = ((ImageView)view).getDrawable();
                if(drawable instanceof BitmapDrawable)
                {
                    BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
                    bitmapDrawable.getBitmap().recycle();
                }
            }

            if (view instanceof ViewGroup) {
                for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                    recycleImagesFromView(((ViewGroup) view).getChildAt(i));
                }
            }
        }

这篇关于ImageView的:自动回收​​位图,如果ImageView的不可见(内滚动型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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