Chapter 6 Cannon Game App
- Introduction
No questions.
- Test-Driving the Cannon Game App
No questions.
- Technologies Overview
No questions.
- Using the Resource Folder res/raw
Q1. Media files (such as the sounds we used in the Cannon Game app), are placed in the app’s resource folder ________.
- res/media
- res/sounds
- res/raw
- res/multimedia
Answer: c. res/raw
- Activity and Fragment Lifecycle Methods
Q1. Which of the following statements is false?
- When an Activity is shut down, its onDestroy method is called, which in turn calls the onDestroy methods of all the Fragments hosted by the Activity.
- We used the onDestroy method in the MainActivityFragment to release the CannonView’s sound resources.
- Method onDestroy is guaranteed to be called, so it can be used to release resources and to save critical data.
- The Android documentation recommends that you save data in methods onPause or onSaveInstanceState.
Answer: c. Method onDestroy is guaranteed to be called, so it can be used to release resources and to save critical data. Actually, method onDestroy is not guaranteed to be called, so it should be used only to release resources, not to save critical data.
- Overriding View Method onTouchEvent
Q1. To process simple touch events in an app, override View method onTouchEvent, then use constants from class ________ (package android.view) to test which type of event occurred and process it accordingly.
- Event
- MotionEvent
- Motion
- View
Answer: b. MotionEvent
- Adding Sound with SoundPool and AudioManager
Q1. An app’s sound effects are managed with a(n) ________ (package android.media), which can be used to load, play and unload sounds.
- Effects
- SoundPool
- EffectsPool
- SoundManager
Answer: SoundPool
Q2. Sounds are played using one of Android’s audio- ________ for alarms, music, notifications, phone rings, system sounds, phone calls and more.
- streams
- players
- tools
- None of the above
Answer: a. streams
Q3. The Android documentation recommends that games use the ________ to play sounds, because that stream’s volume can be controlled via the device’s volume buttons.
- phone audio stream
- alarm audio stream
- notifications audio stream
- music audio stream
Answer: d. music audio stream
Q4. Which of the following statements is false?
- You configure and create a SoundPool object using a SoundPool.Builder object.
- You use an AudioAttributes.Builder object to create an AudioAttributes object that will be associated with the SoundPool.
- AudioAttributes’s setGameAudio method is used to designate the audio as game audio.
- Activity’s setVolumeControlStream method allows the game’s volume to be controlled with the device’s volume buttons.
Answer: c. AudioAttributes’s setGameAudio method is used to designate the audio as game audio. Actually, AudioAttributes’s setUsage method is used to designate the audio as game audio.
- Frame-by-Frame Animation with Threads, SurfaceView and SurfaceHolder
Q1. All updates to an app’s user interface must be performed in the GUI thread of execution, because GUI components are not ________.
- thread ready
- thread safe
- thread capable
- None of the above
Answer: b. thread safe
Q2. 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 that provides a dedicated drawing area in which other threads can display graphics on the screen in a thread-safe manner.
- GameView
- ThreadView
- ThreadSafeView
- SurfaceView
Answer: d. SurfaceView
Q3. 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.
- SurfaceHolder.Callback
- SurfaceHolderCallback
- Surface.Callback
- SurfaceCallback
Answer: a. SurfaceHolder.Callback
- Simple Collision Detection
Q1. Game-development frameworks typically provide sophisticated ________ collision-detection capabilities.
- pixel-based
- perfect
- pixel-perfect
- None of the above
Answer: c. pixel-perfect
- Immersive Mode
Q1. Which of the next three statements is false?
- Full-screen immersive mode enables an app to take advantage of the entire screen.
- When an app is in immersive mode, the user can swipe down from the top of the screen to display the system bars temporarily.
- If the user does not interact with the system bars, they disappear after a few seconds.
- All of the above are true.
Answer: d. All of the above are true.
- Building the GUI and Resource Files
No questions.
- Creating the Project
Q1. Which of the next three statements is false?
- You can add a Fragment and its layout manually.
- You can design an app for only landscape orientation.
- To set the screen orientation to landscape, set android:-orientation to landscape.
- Each of the above is true.
Answer: c. To set the screen orientation to landscape, set android:-orientation to landscape. Actually, to set the screen orientation to landscape, set android:screen-Orientation to landscape.
- Adjusting the Theme to Remove the App Title and App Bar
Q1. Game developers often use full-screen themes, such as
Theme.Material.Light.NoActionBar.Fullscreen
that display only the bottom system bar, which in landscape orientation appears at the screen’s ________.
- top
- bottom
- left edge
- right edge.
Answer: d. right edge
- xml
No questions.
- Colors
No questions.
- Adding the Sounds to the App
No questions.
- Adding Class MainActivityFragment
Q1. A Frame-Layout is designed to display one View, but can also be used to ________ views.
- stack
- nest
- position
- layer
Answers: d. layer.
- Editing xml
No questions.
- Adding the CannonView to xml
No questions.
- Overview of This App’s Classes
No questions.
- MainActivity Subclass of Activity
No questions.
- MainActivityFragment Subclass of Fragment
Q1. There are seven sound streams identified by AudioManager constants. The music stream ________ is recommended for sound in games, because this stream’s volume can be controlled via the device’s buttons.
- AudioManager.STREAM
- AudioManager.MUSIC
- AudioManager.STREAM_MUSIC
- AudioManager.SYMPHONY
Answer: c. AudioManager.STREAM_MUSIC
- Class GameElement
No questions.
- Instance Variables and Constructor
No questions.
- Methods update, draw, and playSound
No questions.
- Blocker Subclass of GameElement
No questions.
- Target Subclass of GameElement
No questions.
- Cannon Class
No questions.
- Instance Variables and Constructor
No questions.
- Method align
No questions.
- Method fireCannonball
No questions.
- Method draw
No questions.
- Methods getCannonball and removeCannonball
No questions.
- Cannonball Subclass of GameElement
No questions.
- Instance Variables and Constructor
No questions.
- Methods getRadius, collidesWith, isOnScreen, and reverseVelocityX
No questions.
- Method u pdate
No questions.
- Method draw
Q1. Canvas’s ________ method draws a circle.
- drawOval
- drawEllipse
- drawCircle
- None of the above
Answer: c. drawCircle
- CannonView Subclass of SurfaceView
No questions.
- package and import Statements
No questions.
- Instance Variables and Constants
No questions.
- Constructor
Q1. When a View is inflated, its constructor is called with a Context and a(n) ________ as arguments.
- SetOfAttributes
- Attributes
- OptionsSet
- AttributeSet
Answer: d. AttributeSet
Q2. Pass ________ to an AudioAttributes.Builder object’s setUsage method to indicate that the audio is being used as game audio.
- AudioAttribute.USAGE_GAME
- AudioAttribute.GAME
- AudioAttribute.GAME_USAGE
- None of the above
Answer: a. AudioAttribute.USAGE_GAME
Q3. Which of the following statements is false?
- More complex games might play many sounds at the same time. SoundPool.Builder’s setMaxStreams method takes an argument that represents the maximum number of simultaneous sound streams that can play at once.
- A SparseIntArray maps integer keys to integer values. HashMap<Integer, Integer> is similar to—but more efficient than—a SparseIntArray for small numbers of key–value pairs.
- Each sound ID can be used to play a sound (and later to return its resources to the system).
- SoundPool method load receives three arguments—the application’s Context, a resource ID representing the sound file to load and the sound’s priority.
Ans: b. A SparseIntArray maps integer keys to integer values. HashMap<Integer, Integer> is similar to—but more efficient than—a SparseIntArray for small numbers of key–value pairs. Actually, a SparseIntArray is similar to—but more efficient than—a HashMap<Integer, Integer> for small numbers of key–value pairs.
- Overriding View Method onSizeChanged
Q1. Class View’s onSizeChanged method is called whenever the View’s size changes, including when the View is first added to the View hierarchy as the layout is inflated. The method receives the View’s new width and height and its old width and height. The first time this method is called, the old width and height are ________ and ________.
- 0, 0
- 0, 1
- 1, 0
- 1, 1
Answer: a. 0, 0
- Methods getScreenWidth, getScreenHeight, and playSound
Q1. Methods getScreenWidth and getScreenHeight return the width and height of the screen, which are updated in the ________ method.
- onOrientationChanged
- onSizeChanged
- onConfigurationChanged
- None of the above
Answer: b. onSizeChanged
- Method newGame
No questions.
- Method updatePositions
Q1. You can terminate a Thread by calling its setRunning method with the argument ________.
- terminate
- true
- kill
- false
Answer: d. false
- Method alignAndFireCannonball
No questions.
- Method showGameOverDialog
Q1. A dialog must be displayed from the GUI thread. You call Activity method ________ to specify a Runnable that should execute in the GUI thread.
- runOnUiThread
- executeOnUIThread
- uiThread
- None of the above
Answer: a. runOnUiThread
- Method drawGameElements
Q1. You can call Canvas’s drawRect method to clear the Canvas. The method receives the rectangle’s upper-left x-y coordinates, width and height, and the ________ object that specifies the drawing characteristics.
- Draw
- PaintSettings
- Paint
- DrawSettings
Answer: c. Paint
- Method testForCollisions
No questions.
- Methods stopGame and releaseResources
Q1. Class SoundPool’s ________ method gives the resources associated with a SoundPool back to the system.
- reset
- release
- reinitialize
- restore
Answer: b. release
- Implementing the Callback Methods
Q1. Method ________ of interface SurfaceHolder.Callback is called when the SurfaceView’s size or orientation changes, and would typically be used to redisplay graphics based on those changes.
- surfaceSizeChanged
- surfaceOrientationChanged
- onSizeChanged
- surfaceChanged
Answer: d. surfaceChanged
Q2. Method surfaceCreated is called when the SurfaceView is created—e.g., ________.
- when the app first loads
- when the app resumes from the background
- Both of the above
- Neither of the above
Answer: c. Both of the above
- Overriding View Method onTouchEvent
Q1. View method onTouchEvent determines when the user touches the screen. The MotionEvent parameter contains information about the event that occurred. If the user touched the screen, the parameter’s value is ________.
- MotionEvent.ACTION_DOWN
- MotionEvent.ACTION_TOUCH
- MotionEvent.ACTION_TAP
- MotionEvent.ACTION_DRAG
Answer: a. MotionEvent.ACTION_DOWN
- CannonThread: Using a Thread to Create a Game Loop
Q1. To obtain the Canvas for drawing on the SurfaceView, call SurfaceHolder method ________.
- getCanvas
- createCanvas
- newCanvas
- lockCanvas
Answer: d. lockCanvas
Q2. Only one thread at a time can draw to a SurfaceView. To ensure this, you must first lock the SurfaceHolder by ________.
- calling lockSurfaceHolder
- specifying it as the expression in the parentheses of a synchronized block.
- calling getSurfaceHolderLock
- None of the above
Answer: b. specifying it as the expression in the parentheses of a synchronized block
- Methods hideSystemBars and showSystemBars
Q1. Immersive mode is available in Android 4.4 (API level 19) and higher. You can check at runtime whether the device’s version is at least Android 4.4 (API level 19) by checking whether ________ is at least Build.VERSION_CODES_KITKAT.
- Build.VERSION_SDK
- Build.VERSION_SDK_INT
- Build.VERSION_INT
- Build.SDK_INT
Answer. b. Build.VERSION_SDK_INT
Chapter 7
Doodlz App
- Introduction
- With Android’s ________ you can draw on the entire screen—the device’s system bars and action bar toggle between displayed and hidden when you tap the screen.
Ans: immersive mode
- An ARGB color contains ________ (transparency), ________, ________ and ________ componnts.
Ans: alpha, red, green, blue
- Technologies Overview
- Using SensorManager to Listen for Accelerometer Events
- Most Android devices have a(n) ________ that allows apps to detect movement.
Ans: accelerometer
- Custom DialogFragments
- (True/False) AlertDialogs are often created using anonymous inner classes that extend DialogFragment and display only text and buttons. AlertDialogs may also contain custom Views.
Ans: True
- The first Fragment lifecycle method called when a Fragment is attached to a parent Activity is ________.
Ans: onAttach
- The last Fragment lifecycle method called when a Fragment is about to be detached from a parent Activity is ________.
Ans: onDetach
- Drawing with Canvas and Bitmap
- (True/False) You can associate a Canvas with a Bitmap, then use the Canvas to draw on the Bitmap, which can then be displayed on the screen. A Bitmap can also be saved into a file.
Ans: True.
- Processing Multiple Touch Events and Storing Lines in Paths
- (True/False) You process touch events by overriding the View method onTouchEvent.
Ans: False. Actually, you override the View method onTouchEvent.