Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Hello.Android.3rd.Edition.pdf
Скачиваний:
33
Добавлен:
02.02.2015
Размер:
3.24 Mб
Скачать

MAKING MORE IMPROVEMENTS 103

4.5Making 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 performancecritical 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.

4.6Fast-Forward >>

In this chapter, we just scratched the surface of Android’s graphics capabilities. The native 2D library is quite large, so as you’re actually writing your programs, be sure to take advantage of the tooltips, autocompletion, and Javadoc provided by the Android Eclipse plug-in. The online documentation for the android.graphics3 package goes into much more detail if you need it.

3. http://d.android.com/reference/android/graphics/package-summary.html

FAST -FORWARD >> 104

If your program needs more advanced graphics, you may want to look ahead a bit and read Chapter 10, 3D Graphics in OpenGL, on page 198. There you’ll find information on how to use Android’s 3D graphics library, which is based on the OpenGL ES standard. Otherwise, turn to the next chapter for an introduction to the wonderful world of Android audio and video.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]