(Do consider accepting some answers)
Generally, invalidate()
means 'redraw on screen' and results to a call of the view's onDraw()
method. So if something changes and it needs to be reflected on screen, you need to call invalidate()
. However, for built-in widgets you rarely, if ever, need to call it yourself. When you change the state of a widget, internal code will call invalidate()
as necessary and your change will be reflected on screen. For example, if you call TextView.setText()
, after doing a lot of internal processing (will the text fit on screen, does it need to be ellipsised, etc.), TextView
will call invalidate()
before setText()
returns. Similarly for other widgets.
If you implement a custom view, you will need to call invalidate()
whenever the backing model changes and you need to redraw your view. It can also be used to create simple animations, where you change state, then call invalidate()
, change state again, etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…