缩放动态壁纸位图的最节省内存的方法?
我不认为这是一个仅限于动态壁纸的问题,但这是我正在尝试处理的用例。
我为我的动态壁纸创建了一个 256 x 256 位图作为“图块”。我正在寻找一种内存有效的方法来缩放它以适应整个设备屏幕。每当设备来回旋转时,我都会遇到“超出虚拟机内存”问题,这表明我没有正确处理问题。
图像是使用 Canvas 的 drawBitmap()
方法绘制的。我尝试了以下选项:
- 创建位图,然后调用
createScaledBitmap()
- 创建位图,然后在画布上调用
scale()
- 创建位图,然后创建矩阵“拉伸”宽度和高度,并使用
preScale()
应用它。
如果我调用 recycle()
动态壁纸会运行一帧,然后崩溃。如果我根本不旋转设备,就不会出现内存不足崩溃的情况。
感谢您的任何建议!
I don't believe this is a question limited to live wallpapers, but it is the use case I am trying to work with.
I have a 256 x 256 bitmap created as a "tile" for my live wallpaper. I am looking for a memory efficient way to scale this to fit an entire device screen. I continue to run into "exceeding VM memory" issues whenever the device rotates back and forth, which suggests I am not handling the problem correctly.
The image is being drawn using Canvas' drawBitmap()
method. I have tried the following options:
- Create the bitmap, then call
createScaledBitmap()
- Create the bitmap, then call
scale()
on my canvas - Create the bitmap, then create a matrix to "stretch" the width and height, and apply it with a
preScale()
If I call recycle()
the live wallpaper runs for one frame, then crashes. If I don't rotate the device at all, I get no out of memory crashes.
Thanks for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您旋转时,您是否会像大多数 Android 应用程序那样阻止应用程序“重新启动”?如果它“重新启动”(不确定术语),它将触发您再次创建图像。很可能是上一张图像尚未收集,因此内存不足。您应该做的是确保应用程序在旋转时不会“重置”(我会记住正确的措辞),我相信这是一个应用程序属性。然后,您捕获旋转事件并处理更改绘图方向,而不分配任何新对象。
希望这是有道理的。
When you rotate, are you preventing the application from 'restarting' like most android apps do? If it 'restarts' (not sure of the terminology off hand) it will trigger you to create the image again. It is likely the case that the previous image has not yet been collected, so you are running out of memory. What you should be doing is making sure the application does not 'reset' (I will remember the correct wording) when you rotate, which is an application property I believe. Then you trap the rotate event and handle changing the drawing orientation there without allocating any new objects.
Hope that makes sense.