在 onDraw() 中调用 invalidate() 是一个好的编程习惯吗?
- 在
onDraw()
内调用invalidate()
是一个好的编程习惯吗?
根据我的理解,在 onDraw()
内部调用 invalidate()
的成本很高,如果画布没有发生变化,则不需要。
invalidate()
是否等同于onDraw()
的异步版本?
根据我的理解,它们是等价的。如果我错了请纠正我。谢谢。
- Is it a good programming practice to call
invalidate()
insideonDraw()
?
As per my understanding, calling invalidate()
inside onDraw()
is expensive and is not required if there is no change to the canvas.
- Is
invalidate()
equivalent to an asynchronous version ofonDraw()
?
As per my understanding, they are equivalent. Correct me if I am wrong. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您的数据已更改且需要重新绘制时才调用
invalidate()
。您通常不会在onDraw()
中执行此操作,因为此时您正在绘制当前数据,而不是更改它。 (在某些情况下,您可能想要这样做,例如运行动画,但通常我会建议使用延迟消息来控制您自己的更新时间。)Only call
invalidate()
if your data has changed and needs to be redrawn. You generally don't do this inonDraw()
, because at that point you are drawing your current data, not changing it. (There are some cases where you may want to do this, such as for running animations, but generally I would recommend instead using a delayed message to control your own timing of the updates.)