In this chapter, you created the Cannon Game app, which challenges the player to destroy a seven-piece target before a 10-second time limit expires. The user aims and fires the cannon by touching the screen. To draw on the screen from a separate thread, you created a custom view by extending class SurfaceView.
You learned that custom component class names must be fully qualified in the XML layout element that represents the component. We presented additional Fragment
lifecycle methods. You learned that method onPause
is called when a Fragment
is paused and method onDestroy
is called when the Fragment
is destroyed. You handled touches by overriding View
’s onTouchEvent
method. You added sound effects to the app’s res/raw
folder and managed them with a SoundPool
. You also used the system’s AudioManager
service to obtain the device’s current music volume and use it as the playback volume.
This app manually performs its animations by updating the game elements on a SurfaceView
from a separate thread of execution. To do this, you extended class Thread
and created a run
method that displays graphics by calling methods of class Canvas
. You used the SurfaceView
’s SurfaceHolder
to obtain the appropriate Canvas
. You also learned how to build a game loop that controls a game based on the amount of time that has elapsed between animation frames, so that the game will operate at the same overall speed on all devices, regardless of their processor speeds.
In Chapter 7, we present the Doodlz app, which uses Android’s graphics capabilities to turn a device’s screen into a virtual canvas. You’ll also learn about Android 4.4’s new immersive mode and printing capabilities.