How do I call onDraw periodically?
I am using the ScrollView class in my application. I am drawing some data to canvas from a compass, this data is updated periodically through user interaction.
How can I periodically call the onDraw method without clicking a button on the display? Can I periodically update the content of the canvas without another thread and call view.postInvalidate()
?
source to share
The class is Handler
very handy for such things.
Handler viewHandler = new Handler();
Runnable updateView = new Runnable(){
@Override
public void run(){
globalView.invalidate();
viewHandler.postDelayed(updateView, MILLISECONDS_TILL_UPDATE);
}
};
Call viewHandler.post(updateView);
anywhere on the UI thread and it will infinitely call this for Runnable
every x number of milliseconds specified in MILLISECONDS_TILL_UPDATE
. Call viewHandler.removeCallbacks(updateView);
to end it, or use the boolean flag to prevent another message.
source to share