Android How to Program Chapter 07 Solution Manual
Download file with the answers
Android How to Program Chapter 06 Solution Manual
1 file(s) 269.83 KB
If you are not a member register here to download this file
7 Cannon Game App
Obj e c t i v e s
In this chapter you’ll:
■ Create a simple game app
that’s easy to code and fun to
play.
■ Create a custom
SurfaceView subclass and
use it to display the game’s
graphics from a separate
thread of execution.
■ Draw graphics using Paints
and a Canvas.
■ Play sounds in respone to
various game events.
■ Manually perform frame-byframe
animations using a
game loop that accounts for
varying frame rates across
devices.
■ Override Activity’s
onTouchEvent to process
touch events when the user
touches the screen or drags a
finger on the screen.
■ Use a GestureDetector to
recognize more sophisticated
user touch motions, such as
double taps.
■ Perform simple collision
detection.
■ Add sound to your app using
a SoundPool and the
AudioManager.
■ Override three additional
Activity lifecycle
methods.
2 Chapter 7 Cannon Game App
Self-Review Exercises
7.1 Fill in the blanks in each of the following statements:
a) You can create a custom view by extending class View or .
b) To process simple touch events for an Activity, you can override class Activity’s on-
TouchEvent method then use constants from class (package android.view)
to test which type of event occurred and process it accordingly.
c) Each SurfaceView subclass should implement the interface , which contains
methods that are called when the SurfaceView is created, changed (e.g., its size or
orientation changes) or destroyed.
d) The d in a format specifier indicates that we’re formatting a decimal integer and the f
in a format specifier indicates that we’re formatting a value.
e) Sound files are stored in the app’s folder.
f) You add sound effects to an app’s folder and manage them with a Sound-
Pool.
7.2 State whether each of the following is true or false. If false, explain why.
a) (True/False) One use of onStop is to suspend a game play so that it does not continue
executing when the user cannot interact with it.
when the user cannot interact with it.
b) (True/False) The Android documentation recommends that games use the music audio
stream to play sounds.
c) (True/False) In Android, it’s important to maximize the amount of work you do in the
GUI thread to ensure that the GUI remains responsive and does not display ANR (Application
Not Responding) dialogs.
d) (True/False) A Canvas draws on a View’s Bitmap.
e) (True/False) Format Strings that contain multiple format specifiers must number the
format specifiers for localization purposes.
f) (True/False) There are seven sound streams identified by constants in class AudioManager,
but the documentation for class SoundPool recommends using the stream for
playing music (AudioManager.STREAM_MUSIC) for sound in games.
g) (True/False) Custom component class names must be fully qualified in the XML layout
element that represents the component.
Exercises
7.3 Fill in the blanks in each of the following statements:
Exercises 3
a) Method is called for the current Activity when another activity receives
the focus, which sends the current activity to the background.
b) When an Activity is shut down, its method is called.
c) A allows an app to react to more sophisticated user interactions such as
flings, double-taps, long presses and scrolls.
d) We use the Activity’s method to specify that the game’s volume can be
controlled with the device’s volume keys and should be the same as the device’s music
playback volume. The method receives a constant from class AudioManager (package
android.media).
e) Games often require complex logic that should be performed in separate threads of execution
and those threads often need to draw to the screen. For such cases, Android provides
class —a subclass of View to which any thread can draw.
f) Method is called for the current Activity when another activity receives
the focus.
7.4 State whether each of the following is true or false. If false, explain why.
a) (True/False) Class SurfaceHolder also provides methods that give a thread shared access
to the Canvas for drawing, because only one thread at a time can draw to a SurfaceView.
b) (True/False) A MotionEvent.ACTION_TOUCH indicates that the user touched the screen
and indicates that the user moved a finger across the screen (Motion-
Event.ACTION_MOVE).
that the user moved a finger across the screen (MotionEvent.ACTION_MOVE).
c) (True/False) When a View is inflated, its constructor is called and passed a Context and
an AttributeSet as arguments.
d) (True/False) SoundPool method start receives three arguments—the application’s Context,
a resource ID representing the sound file to load and the sound’s priority.
e) (True/False) When a game loop controls a game based on the amount of time that has
elapsed between animation frames, the game will operate at different speeds as appropriate
for each device.
Leave a reply