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

?

+3


source to share


1 answer


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.

+8


source







All Articles