Chapter 6 Cannon Game App
No questions.
- Test-Driving the Cannon Game App
No questions.
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
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.
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
No questions.
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.
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
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.
No questions.
- Instance Variables and Constructor
No questions.
No questions.
No questions.
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.
No questions.
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.
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
No questions.
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
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
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
- 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
- (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.
- You use the type of a ________ to determine whether the user has touched the screen, dragged across the screen or lifted a finger from the screen.
Ans: touch event
- Android 4.4 Immersive Mode
- Android’s full-screen immersive mode enables an app to take advantage of the entire screen, but still allows the user to access the ________ when necessary.
Ans: system bars
- GestureDetector and SimpleOnGestureListener
- (True/False) A GestureDetector allows an app to react to user interactions such as flings, single taps, double taps, long presses and scrolls by implementing the methods of interfaces GestureDetector.OnGestureListener and Gesture-Detector.On-Double-Tap-Listener interfaces.
Ans: True.
- Saving the Drawing to the Device’s Gallery
- The default location in which photos taken with the device are stored is the device’s ________.
Ans: gallery
- The ________ manages media files (images, audio and video) stored on a device.
Ans: MediaStore
- Android 4.4 Printing and the Android Support Library’s PrintHelper Class
- Class ________ provides a user interface for selecting a printer, has a method for determining whether a given device supports printing and provides a method for printing a Bitmap.
Ans: PrintHelper
- The Android Support Library is a set of libraries that are commonly used to provide new Android features for use in older Android versions.
Ans: True.
- Building the App’s GUI and Resource Files
No questions
- What does the following code do?
if (screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Ans: If this app is running on an extra large device, we set the orientation to landscape; otherwise, we set it to portrait.
- (True/False) One problem with using a shake event is that even unintentional small movements always result in events.
Ans: False. You can set a threshold to ensure that movements less than this amount are not interpreted as shakes.
- (True/False) Like an Activity, a Fragment can place items in the app’s action bar and options menu. To do so, the Fragment must call its setHasOptionsMenu method with the argument true.
Ans: True.
- SensorManager’s GRAVITY_EARTH constant represents ________.
Ans: the acceleration due to gravity on earth
- You register to receive accelerometer events using SensorManager’s registerListener method. One of its arguments is the rate at which sensor events should be delivered to the app. Why would you specify SENSOR_DELAY_NORMAL to receive sensor events at the default rate, instead of choosing a faster rate?
Ans: Because although a faster rate can be used to get more accurate data, it would be more CPU and battery intensive.
- We override SensorEventListener method ________ to process accelerometer events.
Ans: onSensorChanged
- Why would you provide an empty body for a method?
Ans: Because the method is required by an interface.
- It’s important to handle sensor events quickly or to copy the event data because ________.
Ans: the array of sensor values is reused for each sensor event.
- (True/False) When the user selects a menu item, Fragment method onOptionsItemSelected responds to the selection.
Ans: True.
- When processing a menu selection you use the MenuItem’s ________ method to get the resource ID of the selected menu item, then take appropriate actions based on the selection.
Ans: getItemID
- Why would you pass true to Paint’s setAntiAlias method?
Ans: To enable anti-aliasing, which smoothes the edges of lines.
- (True/False) You set a line’s width using Paint’s setLineWidth method.
Ans: False. You use Paint’s setStrokeWidth method to do this.
- Why would you call Paint’s setStrokeCap method with Paint.Cap.ROUND?
Ans: To round the ends of lines.
- Which of the following statements is false?
- Bitmap’s staticcreateBitmap method creates a Bitmap of the specified width and height.
- The last argument to createBitmap is the Bitmap’s encoding, which specifies how each pixel in the Bitmap is stored. The constant Bitmap.Config.ARGB_8888 indicates that each pixel’s color has a default value of 8 for the alpha, red, green and blue components.
- A Canvas is used to draw shapes directly to a Bitmap.
- You can use Bitmap’s eraseColor method to fill the Bitmap with white pixels.
Ans: b. The last argument to createBitmap is the Bitmap’s encoding, which specifies how each pixel in the Bitmap is stored. The constant Bitmap.Config.ARGB_8888 indicates that each pixel’s color has a default value of 8 for the alpha, red, green and blue components. Actually, the constant indicates that each pixel’s color is represented by a 4-byte ARGB value.
- Why would you call the inherited View method invalidate?
Ans: To indicate that the View needs to be redrawn.
- When a View needs to be redrawn, its onDraw method is called. The first argument is the Bitmap to draw, the next two arguments are the x-y coordinates where the upper-left corner of the Bitmap should be placed on the View and the last argument is the ________ object that specifies the drawing characteristics.
Ans: Paint
- SimpleOn-Gesture-Listener is an ________ class that implements interfaces OnGestureListener and OnDoubleTap-Listener.
Ans: adapter
- Which of the following statements is false?
- Method onTouchEvent is called when the View receives a touch event.
- Android supports multitouch—that is, having multiple fingers touching the screen.
- The user must maintain the same number of fingers touching the screen for the duration of the multitouch event.
- Each finger—known as a pointer—has a unique ID that identifies it across touch events.
Ans: c. The user must maintain the same number of fingers touching the screen for the duration of the multitouch event. Actually, at any time, the user can touch the screen with more fingers or remove fingers from the screen.
- Which of the following statements is false?
- Motion-Event’s getActionMasked method returns an int representing the MotionEvent type, which you can use with constants from class MotionEvent to determine how to handle each event.
- Motion-Event’s getActionIndex method returns an integer index representing which finger caused the event. This index is the finger’s unique ID.
- To get the finger’s unique ID that persists across MotionEvents until the user removes that finger from the screen, we’ll use Motion-Event’s getPointerID method, passing the finger index as an argument.
- If the action is MotionEvent.ACTION_DOWN or MotionEvent.ACTION_POINTER_DOWN, the user touched the screen with a new finger.
Ans: b. Motion-Event’s getActionIndex method returns an integer index representing which finger caused the event. Actually, this index is not the finger’s unique ID—it’s simply the index at which that finger’s information is located in this MotionEvent object.
- Call Path’s ________ method to set the Path’s starting coordinates and specify the new Point’s x and y values.
Ans: moveTo
- (True/False) The system MotionEvent passed from onTouchEvent contains touch information for multiple moves on the screen if they occur at the same time. MotionEvent method getPointerCount returns the number of touches the Motion-Event describes.
Ans: True.
- (True/False) Store the image in the device’s Gallery by calling class MediaStore.Images.Media’s insertImage method. Method insertImage returns a String representing the image’s location on the device, or null if the image could not be saved.
Ans: True.
- PrintHelper method printBitmap displays Android’s print dialog, which allows the user to choose whether to save the image as a ________ on the device or to print the image to an available printer.
Ans: PDF document
- ColorDialogFragment Class
- Color’s static methods alpha-, red, green and blue extract the ARGB values from the color, and SeekBar’s ________ method positions the thumbs.
Ans: setProgress
- When the ColorDialogFragment is added to a parent Activity, method ________ is called.
Ans: onAttach
- Method onProgressChanged is called when the position of a SeekBar’s ________ changes.
Ans: thumb
- LineWidthDialogFragment Class
No questions
- EraseImageDialogFragment Class