ImageView 性能 - 当它前面的内容发生变化时是否应该重绘?
我有一个使用 FrameLayout 在屏幕上绘制两层的活动。第一个位于后面,是一个显示位图的 ImageView - 使用 match_parent
缩放到全屏尺寸。第二个,在前面,是一个用 animation-list
实现的小型可绘制对象 - 本质上是一组组成动画图标的 PNG 帧(大约 10 帧,每帧 android:duration=100 毫秒)。动画中的 PNG 图像使用透明度,因此您可以看到其后面的背景 ImageView,并且整个图像相对于 ImageView 来说很小 - 它实际上是图像顶部的一个图标。
我注意到这个简单的应用程序似乎使用了相当多的 CPU 能力。为了帮助追踪它,我扩展了 ImageView 以覆盖背景图像的 onDraw,并发现每次前景动画更新时都会调用 onDraw 例程。这是预期的行为吗?我希望 ImageView 内容应该缓存在某个地方,这样如果没有任何变化,它就不必重绘其位图。
有没有更好的方法来获得此功能?
I have an activity that uses a FrameLayout to draw two layers to the screen. The first, in back, is an ImageView displaying a bitmap - scaled to the full screen size with match_parent
. The second, in front, is a small drawable implemented with animation-list
- essentially a set of PNG frames that make up an animated icon (roughly 10 frames, with android:duration=100 ms each). The PNG images in the animation use transparency, so you can see the background ImageView behind it, and the whole thing is small relative to the ImageView - it really is effectively an icon on top of an image.
I've noticed that this simple application seems to use quite a bit of CPU power. To help track it down, I extended ImageView to override onDraw for the background image, and found that the onDraw routine appears to be called everytime the foreground animation updates. Is this expected behavior? I would expect that the ImageView contents should be getting cached somewhere so it doesn't have to redraw its bitmap if nothing changes.
Is there a better way to go about getting this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所看到的是预期的。要重绘正在制作动画的 PNG,必须清除帧缓冲区,这意味着必须重绘背景 ImageView。缓存不会有任何帮助。
What you are seeing is expected. To redraw the PNGs you are animating, the framebuffer has to be cleared, which means the background ImageView must be redrawn. Caching would not help in any way.