Android - 减少位图绘制的内存使用量
我的应用程序中有一张地图,显示了 Gowalla 的位置。我使用带有简单默认标记的 ItemizedOverlay,但在绘制项目时,我将默认标记替换为从 Gowalla 下载(9 并缓存在磁盘上)的位置图标。
问题是,如果屏幕上有很多位置(例如 30 个),则内存中位图的大小足以使 Activity 崩溃。为了防止这种情况,我只保留位图的软引用。不幸的是,这意味着位置将在默认标记和图标之间闪烁(即,图标已加载,但随后存在内存压力,因此它们被删除,但随后重新加载,因为它们在屏幕上......)。
当我从 Gowalla 下载图像时,我已经根据屏幕尺寸缩小了它们(HDPI 为 48x48,MDPI 为 32x32 等),所以这些图像并不是很大,但我仍然被迫选择可能会导致我的应用程序崩溃或图标闪烁。有其他方法可以减少位图使用的内存吗?
I have a map in my application that shows locations from Gowalla. I use an ItemizedOverlay with a simple default marker, but as the items are drawn, I swap the default marker out with the location's icon downloaded 9and cached on disk) from Gowalla.
The problem is that if there are a lot of locations on screen (say 30), the size of the bitmaps in memory is enough to crash the activity. To prevent this, I only keep SoftReferences to the bitmaps. Unfortunately, this means that locations will flicker between the default marker and the icon (i.e., icons are loaded, but then there is memory pressure, so they're removed, but then reloaded because they're on screen...).
When I download the images from Gowalla, I'm already scaling them down based on the screen size (48x48 for HDPI, so 32x32 for MDPI, etc.), so these aren't huge images, but I'm still forced to choose between potentially crashing my app or having the icons flicker in and out. Is there some other way I can reduce the memory that a Bitmap uses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这永远是一个问题。您可以尝试下采样。 将图像加载到 Bitmap 对象时出现奇怪的内存不足问题
您可以尝试将图像保存到临时文件系统,这样您就不必每次都访问网络。这应该对消除闪烁有很大帮助,甚至可能 100% 有帮助。将其与基于访问的堆缓存(如果您愿意构建一个)结合起来,您可能会做得很好。
This will always be a problem unfortunately. You can try downsampling. Strange out of memory issue while loading an image to a Bitmap object
You might try saving the images to a temporary file system so that you don't have to hit the web every time. That should help a lot or maybe even 100% with the flicker. Couple that with an access-based heap cache (if you feel up to building one), and you're probably good.
闪烁问题是由于使用了软引用造成的。 Android 会积极清除它们,并且文档中不推荐它们。我猜他们只是为了兼容性而出现在 Android 中。我应该使用 LruCache 代替。
The flickering problem is due to the use of Soft References. Android aggressively clears them and they are not recommended in the docs. I would guess they are only in Android for compatibility. I should have been using an LruCache instead.