- •Contents
- •Acknowledgments
- •Preface
- •What Makes Android Special?
- •Who Should Read This Book?
- •Online Resources
- •Fast-Forward >>
- •Introducing Android
- •Quick Start
- •Installing the Tools
- •Creating Your First Program
- •Running on the Emulator
- •Running on a Real Phone
- •Key Concepts
- •The Big Picture
- •Building Blocks
- •Using Resources
- •Safe and Secure
- •Android Basics
- •Designing the User Interface
- •Introducing the Sudoku Example
- •Designing by Declaration
- •Creating the Opening Screen
- •Using Alternate Resources
- •Implementing an About Box
- •Applying a Theme
- •Adding a Menu
- •Adding Settings
- •Starting a New Game
- •Debugging
- •Exiting the Game
- •Exploring 2D Graphics
- •Learning the Basics
- •Adding Graphics to Sudoku
- •Handling Input
- •The Rest of the Story
- •Making More Improvements
- •Multimedia
- •Playing Audio
- •Playing Video
- •Adding Sounds to Sudoku
- •Storing Local Data
- •Adding Options to Sudoku
- •Continuing an Old Game
- •Remembering the Current Position
- •Accessing the Internal File System
- •Accessing SD Cards
- •Beyond the Basics
- •The Connected World
- •Browsing by Intent
- •Web with a View
- •From JavaScript to Java and Back
- •Using Web Services
- •Locating and Sensing
- •Location, Location, Location
- •Set Sensors to Maximum
- •Putting SQL to Work
- •Introducing SQLite
- •Hello, Database
- •Data Binding
- •Using a ContentProvider
- •Implementing a ContentProvider
- •3D Graphics in OpenGL
- •Understanding 3D Graphics
- •Introducing OpenGL
- •Building an OpenGL Program
- •Rendering the Scene
- •Building a Model
- •Lights, Camera, ...
- •Action!
- •Applying Texture
- •Peekaboo
- •Measuring Smoothness
- •Fast-Forward >>
- •The Next Generation
- •Multi-Touch
- •Building the Touch Example
- •Understanding Touch Events
- •Setting Up for Image Transformation
- •Implementing the Drag Gesture
- •Implementing the Pinch Zoom Gesture
- •Hello, Widget
- •Live Wallpaper
- •Write Once, Test Everywhere
- •Gentlemen, Start Your Emulators
- •Building for Multiple Versions
- •Evolving with Android APIs
- •Bug on Parade
- •All Screens Great and Small
- •Installing on the SD Card
- •Publishing to the Android Market
- •Preparing
- •Signing
- •Publishing
- •Updating
- •Closing Thoughts
- •Appendixes
- •Bibliography
- •Index
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.