spoton game app android how to program ©1992-2013 by pearson education, inc. all rights reserved
TRANSCRIPT
SpotOn Game AppAndroid How to Program ©1992-2013 by Pearson
Education, Inc. All Rights Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
•Android 3.x ▫First app to use features of Android 3.0+▫Using it to animate properties▫Actually this is an Android 3.1 app
•AVDs for Android 3.0+ apps are extremely slow▫Run on an Android device, if possible
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Test driving the app©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Technologies Overview
•Prior to Android 3.0 animation done three ways:▫Tweened View animations to change View’s
appearance, such as where it is displayed, rotation, size the way we did in FlagQuizGame
▫Frame View animations for sequence of images▫Create animations the way we did it with
CannonGame
•Problem: if you animate a button, can only initiate Button’s click event where the Button was originally
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Technologies Overview• Property Animators
▫Used to move and scale ImageViews dynamically▫Can animate ANY property of ANY object▫Not limited to Views▫Can interact with a Button after it has moved
• Property Animators animate values over time• Specify
▫Target object▫Property to animate▫Duration of animation▫Values to animate between for each property▫How to change the values over time
(interpolator)
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Technologies Overview
Two property animation classes: Value Animator and Object Animator
•ValueAnimator▫Calculates property values over time▫Must specify AnimatorUpdateListener▫Programmatically modigy target object’s
property values▫Useful if target object does not have
standard set methods for changing property values
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Technologies Overview
Two property animation classes
•ObjectAnimator▫Subclass of ValueAnimator▫Uses target object’s set methods to modify
object’s animated properties as their values change over time
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
New Utility class
•Added by Android 3.1•ViewPropertyAnimator
▫Simplifies property animation for Views▫Allows multiple properties to be animated
in parallel▫Now each View contains an animate method
Returns a ViewPropertyAnimator on which you can chain method calls to configure the animation
When last method call on chain finishes, the animation starts
Used to animate spots in this game
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Technologies Overview
Two ways to listening for Animation Lifecycle Events
•Implement interface AnimatorListener▫Defines methods called when animation
starts, ends, repeats or is cancelled•Extend class AnimatorListenerAdapter
▫If you do not need all four methods▫Override only the listener method(s) you
need
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Technologies Overview
•Touch Handling▫Introduced in CannonGame – overrode
Activity method onTouchEvent▫Two types of touches in the SpotOn game
Touching a spot Touching elsewhere on the screen
•Register onClickListeners for each spot (i.e. ImageView) to process touching a spot
•Use onTouchEvent to process all other screen touches
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Building the App’s GUI
• Set attributes in string.xml (not shown and not discussed)
• Set attributes in AndroidManifest.xml file• App’s main.xml layout file contains two layouts
▫RelativeLayout Positions the app’s TextViews for displaying
High score, level and current score
▫LinearLayout (nested inside RelativeLayout) Display’s lives remaining
• The layouts and GUI components in this app have all been seen before
• …and two other XML files
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
This ImageView will be inflated and configured dynamically for each new spot in the game.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
This ImageView will be inflated and configured dynamically each time a new life is added to the Screen during the game.
Building the App
•Consists of two classes▫SpotOn – app’s main Activity▫SpotOnView – subclass of View, it defines
the game logic and spot animations
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Construct the SpotOnViewRequires 3 arguments:•Context in which GUI component is displayed (this Activity)•A SharedPreference object•The RelativeLayout (so SpotOnView can interact with other GUI components in the layout)
layout);
Ch5 showed how to read from a SharedPreferences file, here we use default one associated with this Activity
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.Add the SpotOnView to the RelativeLayout at position 0 – that is, behind all the other elements in the layout
Call the SpotOnView’s pauseAnd resume methods
OnPause and OnResume• onPause releases SoundPool resources and
cancels running animations.
• When an Activity begins executing,▫ The method OnCreate is executed, followed by
OnStart, then onResume• onResume is also called when an Activity in the
background returns to the foreground
• In this app’s Activity, SpotOnView’s resume method obtains (or re-obtains) SoundPool resources and starts (or restarts) the game
• This app does not save the game’s state when the app is not on the screen.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
SpotOnView Subclass of View©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
OnCreate OnResumeOnPause
+SpotOnViewconstructor
+pause +resume
+cancelAnimations
+initializeSoundEffects +resetGame
+displayScores+addSpotRunnable
+addNewSpot
+touchedSpot
+missedSpot onTouchEvent
+displayScores
onClickListener
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
For collection of spots and animators
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Call to this from OnCreate view = new SpotOnView (this, getPreferences(Context.MODE_PRIVATE), layout);
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
For each loops
Do not want animationsto continue executing whenapp not on screen
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
If dialogDisplayed is true, it is because the end-of-game dialog is still displayed on the screen and user can click the Reset Game button to start over; otherwise, it willcall resetGame.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Inflate the life.xml file
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Schedule game’s first few spots
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.Uses CannonGame’s same technique to prepare game’s sound effects
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
resetGame’s spotHandler’s postDelayed method receives theaddSpotRunnable as an argument. This Runnable’s run method simply calls addNewSpot.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.Called-Several times near the beginning of game to display initial spots-Whenever the user touches a spot-When a spot’s animation ends without the spot being touched
Set spot’s values
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Each new added spot has a listener
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Chained methods
Only override two of thefour AnimatorListener’smethods
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.
Wrap-up
•Be able to describe three animation mechanisms
•Know what was added in Android 3.0 and 3.1 that was used in the SpotOn game
•Know what 4 methods are available in AnimatorListenerAdapter
•Know how to chain methods to configure an animation
•Know how to cancel animations•Know the methods of the
ConcurrentLinkedQueue class and what it is used for
©1992-2013 by Pearson Education, Inc. All Rights
Reserved.