How to update Android Views upon modifications?

牧云@^-^@ 提交于 2019-12-07 00:12:19

问题


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

}

回答1:


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().



来源:https://stackoverflow.com/questions/6755748/how-to-update-android-views-upon-modifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!