Making More Improvements

Although the code presented in this chapter performs acceptably for a Sudoku game, more complex programs will likely need to be more carefully written in order to squeeze the last drop of performance out of the device. In particular, the onDraw() method is a very performance-critical piece of code, so it's best to do as little as possible there.

Here are some ideas for speeding up this method:

• If possible, avoid doing any object allocations in the method onDraw( ).

• prefetch things such as color constants elsewhere (for example, in the view's constructor).

• Create your Paint objects up front, and just use existing instances in onDraw( ).

• For values used multiple times, such as the width returned by getWidth(), retrieve the value at the beginning of the method and then access it from your local copy.

As a further exercise for the reader, I encourage you to think about how you could make the Sudoku game graphically richer. For example, you could add some fireworks when the player solves the puzzle or make the tiles spin around like vanna White does. A moving background behind the puzzle might be interesting. Let your imagination go wild. If you want to make a top-notch product, touches like this can add pizzazz to an otherwise ordinary offering.

In Chapter 5, Multimedia, on page 105, we'll enhance the program with a little mood music, and in Chapter 6, Storing Local Data, on page 120, we'll see how to remember the puzzle state and finally implement that Continue button.

0 0

Post a comment