Home » Android » android – Is it a good programming practice to call invalidate() inside onDraw()?

android – Is it a good programming practice to call invalidate() inside onDraw()?

Posted by: admin June 15, 2020 Leave a comment

Questions:
  1. Is it a good programming practice to call invalidate() inside onDraw()?

As per my understanding, calling invalidate() inside onDraw() is expensive and is not required if there is no change to the canvas.

  1. Is invalidate() equivalent to an asynchronous version of onDraw()?

As per my understanding, they are equivalent. Correct me if I am wrong. Thank you.

How to&Answers:

Only call invalidate() if your data has changed and needs to be redrawn. You generally don’t do this in onDraw(), 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.)