Arduino Uno

Header

Corner

Threaded drawings

On this page I try to explain how to create a threaded drawing surface, meaning that the surface that is painted on is not being drawn by the main but by a separate thread.

Before lookting at the code, which simply draws random lines to the screen, I want to pass you some important information:

  • The drawing thread has to be created in the surfaceCreated event handler or in any other method but not in the constructor. It has also to be stopped properly, which is done in the surfaceDestroyed event handler.
  • As the drawing thread is not allowed to create other threads, this has to be done inside the drawing surface. In the given example I've created a timer, which actually does nothing.
  • Don't create a Paint object in your updating and rendering thread, it will drive the GC crazy!

By the time I write these lines, I've coded two different approaches to repaint threaded drawings:

  • Continuously, meaning as fast as the GPU can handle it.
  • On request, meaning each time the application requests to repaint the drawing.