spoton game app android how to program ©1992-2013 by pearson education, inc. all rights reserved

49
SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Upload: hugo-bishop

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

SpotOn Game AppAndroid How to Program ©1992-2013 by Pearson

Education, Inc. All Rights Reserved.

Page 2: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 3: SpotOn Game App Android How to Program ©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.

Page 4: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

Test driving the app©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 5: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 6: SpotOn Game App Android How to Program ©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.

Page 7: SpotOn Game App Android How to Program ©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.

Page 8: SpotOn Game App Android How to Program ©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.

Page 9: SpotOn Game App Android How to Program ©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.

Page 10: SpotOn Game App Android How to Program ©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.

Page 11: SpotOn Game App Android How to Program ©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.

Page 12: SpotOn Game App Android How to Program ©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.

Page 13: SpotOn Game App Android How to Program ©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.

Page 14: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 15: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 16: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 17: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 18: SpotOn Game App Android How to Program ©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.

Page 19: SpotOn Game App Android How to Program ©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 each time a new life is added to the Screen during the game.

Page 20: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

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.

Page 21: SpotOn Game App Android How to Program ©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

Page 22: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©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

Page 23: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

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.

Page 24: SpotOn Game App Android How to Program ©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

Page 25: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

For collection of spots and animators

Page 26: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 27: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 28: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 29: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 30: SpotOn Game App Android How to Program ©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);

Page 31: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 32: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 33: SpotOn Game App Android How to Program ©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

Page 34: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©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.

Page 35: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Inflate the life.xml file

Page 36: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Schedule game’s first few spots

Page 37: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.Uses CannonGame’s same technique to prepare game’s sound effects

Page 38: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 39: SpotOn Game App Android How to Program ©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.

Page 40: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©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

Page 41: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Each new added spot has a listener

Page 42: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Chained methods

Only override two of thefour AnimatorListener’smethods

Page 43: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 44: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 45: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 46: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 47: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 48: SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved

©1992-2013 by Pearson Education, Inc. All Rights

Reserved.

Page 49: SpotOn Game App Android How to Program ©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.