ImageView:如果 ImageView 不可见(在 ScrollView 内),则自动回收位图
所以,我已经研究 ImageView 源代码有一段时间了,但我还没有找到一个钩子来做到这一点。
问题:假设 ScrollView 中有 30 个 400x800 图像(图像数量是可变的)。由于它们正好适合屏幕,因此每个需要 1.3 MB 的 RAM。
我想要的:能够加载/卸载当前在 ScrollView 中可见的 ImageView 的位图。如果用户滚动并且位图不再可见(在距离阈值内),则应回收位图,以便同一 ScrollView 中的其他位图可以使用内存。我正在做下采样等等,所以不用担心。如果你只通过扩展 ImageView 来做到这一点,那就加分了(如果可能的话,我希望不要弄乱 ScrollView)。
摘要:我可以使图像仅在 ImageView 变得可见后才加载(使用巧妙的技巧),但我不知道何时卸载它们。
注意:由于其他可用性原因,我无法使用 ListView 执行此操作。
So, I've been looking at the ImageView source for a while, but I haven't figured out a hook to do this yet.
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.
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).
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.
Notes: I can't do this with a ListView due to other usability reasons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您想要回收位图时,请调用此方法。
Call this method when you want to recycle your bitmaps.
查看关于 "如何绘制位图" 的 android 文档该代码示例称为位图乐趣。
如果您使用 ListView 或 GridView,则 ImageView 对象将被回收,并且实际的位图将被释放以供 GC 清理。
您还希望将原始图像的大小调整为屏幕大小并将它们缓存在磁盘和/或 RAM 上。这将为您节省大量空间,实际大小应减少到几百 KB。您还可以尝试以显示器分辨率的一半来显示图像。当使用更少的 RAM 时,结果应该仍然不错。
Have a look at the android documentation about "How to draw bitmaps" Also the code example called bitmap fun.
If you use a ListView or a GridView the ImageView objects are getting recycled and the actual bitmap gets released to be cleaned up by the GC.
You also want to resize the original images to the screen size and cache them on disk and/or RAM. This will safe you a lot of space and the actual size should get down to a couple of hundert KB. You can also try to display the images in half the resolution of your display. The result should still be good while using much less RAM.
您真正想要的是为此使用 ArrayAdapter。如果像您的注释所建议的那样,这是不可能的(尽管我不明白为什么),请查看数组适配器的源代码并根据您自己的需要重写一个。
What you really want is to use an ArrayAdapter for this. If, like your notes suggest, that is not possible (although i don't see why) take a look at the source for array adapter and rewrite one to your own needs.