O'Reilly logo

Mastering Android Game Development by Raul Portales

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Improving DrawThread

To trigger the draw we have been using a Timer and TimerTask scheduled so we could get 30 frames per second. While this works, it gives better performance to do it like the UpdateThread: run as many calls to onDraw as we can.

This approach works great for SurfaceView since the drawing is done on the same thread. But it may give some message overflow problems while using StandardGameView, which just calls postInvalidate. To prevent the overflow, we will ensure that the time between the calls to onDraw is never shorter than 20 milliseconds, which is enough for 50 frames per second.

The code for the new DrawThread is exactly the same as for UpdateThread, except for the part in the run method that takes care of the overflow. It ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required