如何在修改后更新 Android 视图?
我的视图中有一些方法可以修改调用时绘制的一些形状。在 Java 中,为了确保组件已更新,我会调用 repaint()
。有什么可以确保我的视图正确更新吗?
我在某处读到,在 onDraw()
方法中调用 invalidate()
将使内容保持最新,因此我不需要像 repaint 这样的东西()
在我的方法中修改绘制的形状。
这是正确的吗,还是我还需要做其他事情?
编辑
添加一个例子,我在我看来调用的方法是:
public void setLineThickness(int thickness) {
aLineThickness = thickness;
if(aLineThicness > 1)
//repaint(); - Okay in Java but not in Android
}
I have some methods in my View that modify some of the shapes that are drawn when called. In Java in order to make sure the component is updated I would call repaint()
. Is there something that will make sure my view is updated correctly?
I had read somewhere that calling invalidate()
in the onDraw()
method would keep things up to date and therefore I wouldn't need to have something like repaint()
in my methods that modify that shapes that are drawn.
Is this correct, or is there something else I have to do?
EDIT
To add in an example, a method I call in my view is:
public void setLineThickness(int thickness) {
aLineThickness = thickness;
if(aLineThicness > 1)
//repaint(); - Okay in Java but not in Android
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用 invalidate() 将告诉视图它将来需要重绘自身(调用 onDraw)。因此,如果您更改视图中的某些内容(例如线条粗细),请在其后调用 invalidate() 。这样您就知道您的视图最终会更新。
所有绘图代码都应该在 onDraw() 中实现,而其他方法应该只更改视图的状态,然后在调用 invalidate() 后使用该状态来绘制视图。
Calling invalidate() will tell the view it needs to redraw itself (call onDraw) sometime in the future. So if you change something in your view, like the line thickness, call invalidate() after it. That way you know your view will eventually be updated.
All your drawing code should be implemented in onDraw() and your other methods should just change the state of your view, which will then be used to draw it, after you call invalidate().