David Meredith dave@create.aau.dk Android: Animation David Meredith dave@create.aau.dk.

Slides:



Advertisements
Similar presentations
Interaction Design: Visio
Advertisements

INTRODUCTION TO ADOBE FLASH CS4
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Android User Interface
Programming with Microsoft Visual Basic th Edition
Android View Properties & Animations
Graphics & Animation in Android. Android rendering options The Canvas API Renderscript OpenGL wrappers NDK OpenGL
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
VBA Modules, Functions, Variables, and Constants
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
© 2010 Delmar, Cengage Learning Chapter 4: Creating Animation.
Basic Animation. Animation 4 options – Animated.gif – Frame by Frame animation – Tweened animation This is our focus – OpenGL ES Graphics API for more.
Android: Layouts David Meredith
© 2011 Delmar, Cengage Learning Chapter 8 Building Complex Animations.
Custom Views, Drawing, Styles, Themes, ViewProperties, Animations, oh my!
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Chapter 10: Move! Creating Animation
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
© 2012 Adobe Systems Incorporated. All Rights Reserved. Copyright 2012 Adobe Systems Incorporated. All rights reserved. ® INTRODUCTION TO FLASH ANIMATION.
® Copyright 2010 Adobe Systems Incorporated. All rights reserved. ® ® 1 INTRODUCTION TO ADOBE FLASH PROFESSIONAL CS5.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 7: Reveal! Displaying Pictures in a GridView.
Chapter 7: Reveal! Displaying Pictures in a Gallery.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Android – Fragments L. Grewe.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Chapter 5: Windows and Frames
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Introduction to Flash Animation CS 318. Topics Introduction to Flash and animation The Flash development environment Creating Flash animations  Layers.
Game Maker Terminology
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CIS 3.5 Lecture 2.2 More programming with "Processing"
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
CS324e - Elements of Graphics and Visualization Timing Framework.
Mobile Programming Lecture 11 Animation and TraceView.
MOBILE COMPUTING D10K-7D02 MC05: Android UI Design Dr. Setiawan Hadi, M.Sc.CS. Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran.
Computer Game Design ActionScript is… Object-oriented programming Everything you do in ActionScript does something to some object* Some objects.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Animation in Java Disclaimer Rendering Performance Rendering or Performance issues are not covered here.
Flag Quiz Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 10: Move! Creating Animation 1 Android.
By: Eliav Menachi.  Android custom 2D graphics library  OpenGL ES 1.0 for high performance 3D graphics.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Graphics & Animation Radan Ganchev Astea Solutions.
Graphics & Animation in Android
Module Road Map Refactoring Why Refactoring? Examples
Lecture 8: Graphics By: Eliav Menachi.
Mobile Application Development BSCS-7 Lecture # 8
S.RENUKADEVI/AP/SCD/ANDROID - Notifications
Activities and Intents
Stacks.
Interfaces.
Cannon Game App Android How to Program
INTRODUCTION TO FLASH ANIMATION
Stacks.
Lecture 8: Graphics By: Eliav Menachi.
Presentation transcript:

David Meredith dave@create.aau.dk Android: Animation David Meredith dave@create.aau.dk

Source http://developer.android.com/guide/topics/graphics/index.html

Overview Animation Android provides 2 animation systems Property animation (from API 11, Android 3.0) (preferred – more flexible and offers more features) View animation Also drawable animation For displaying series of Drawable resources (e.g., bitmaps) one after the other

Property Animation Property animation allows you to animate almost anything Can change any object property over time regardless of whether it draws to the screen Changes a property’s value over a specified length of time To animate a property, specify property you want to animate (e.g., position) how long you want to animate for what values you want to animate between

Property animation Property animation system lets you define Duration (default is 300ms) Time interpolation How property values depend on elapsed time of animation Repeat count and behaviour Specify number of times animation should run and whether it should run in reverse Animator sets Group animation into logical sets that play together or sequentially Frame refresh delay How often to refresh frames of your animation (default 10ms) Actually depends on how busy system is and how fast system can service the timer

How property animation works Figure depicts animation of x property of an object Must travel 40 pixels in 40 ms Linear interpolation means object moves 10 pixels every 10 ms

Non-linear interpolation Can also specify non-linear interpolation Figure shows object that accelerates at beginning of animation and decelerates at the end

How animations are calculated ValueAnimator keeps track of animation’s timing (e.g., how long it’s been running, current value of animated property) TimeInterpolator – defines interpolation e.g., AccelerateDecelerateInterpolator TypeEvaluator – defines how to calculate values for the animated property e.g., IntEvaluator

Running an animation Create a ValueAnimator, specifying start value of animated property end value of animated property duration of animation Call the ValueAnimator object’s start() method During animation, ValueAnimator continuously calculates an elapsed fraction between 0 (beginning of animation) and 1 (end of animation period) e.g., in animation above, 0.25 corresponds to 10 ms

Example see BouncingBalls.java

Interpolated fractions ValueAnimator calculates elapsed fraction (i.e., fraction of animation time period elapsed) Then TimeInterpolator uses elapsed fraction to calculate interpolated fraction fraction of total change in animated property for this elapsed fraction e.g., in example above, interpolated fraction is 0.15 when elapsed fraction is 0.25

Calculating the property value ValueAnimator uses TypeEvaluator to calculate the value of the animated property, based on the starting value the interpolated fraction the ending value e.g., in above example, property value for interpolated value of 0.15 is 0.15 x (40-0)=6

Limitations of View animation View animation only allows you to animate View objects You need to write your own code if you want to animate non-View objects View animation only lets you animate certain aspects of a View (e.g., scaling and rotation) View animation only affects where View is drawn, not where it “actually” is e.g., if you animate a button, the place where you click does not move!

Property vs. View animation Previous constraints of View animation completely removed Can animate any property of any object – View or non-View! Object itself is modified You assign animators to the properties you want to animate e.g., color, position, size You define how animation happens e.g., interpolations and synchronization of multiple animators

Advantages of View animation View animation takes less time to set up and needs less code So use it if it achieves everything you need to do

Property animation API overview Most of the property animation system’s API is in the android.animation package http://developer.android.com/reference/android/animation/package-summary.html You can also use the View animation interpolators in property animation available in android.view.animation http://developer.android.com/reference/android/view/animation/package-summary.html

Animators Animators are used to create animations usually done by a subclass of Animator The following subclasses extend Animator ValueAnimator main timing engine for property animation computes values for animated property contains information about repeats, behaviour, listeners that receive update events can set custom types to evaluate only calculates property values does NOT set values on the animated object ObjectAnimator subclass of ValueAnimator allows target object and property to be set updates animated property value used most of the time requires specific accessors to be available on the target object AnimatorSet allows animations to be grouped so that they play together, sequentially or after a delay

Evaluators Evaluators tell property animation system how to calculate values for animated properties Calculate animated property values using timing data provided by Animator class and start and end values

Evaluators Property animation system provides following Evaluators IntEvaluator – default for calculating int values FloatEvaluator – default for calculating floats ArgbEvaluator – default for calculating hexadecimal colour values TypeEvaluator – interface that lets you create your own evaluator if type of animated property is not int, float or colour Can also use for custom evaluator of int, float or colour

Interpolators Interpolator defines how animated property values in an animation are calculated as a function of time, e.g., linear interpolator – constant rate of animated property value change throughout or non-linear – e.g., accelerating at the beginning, decelerating at the end

Interpolators Interpolators provided by android.view.animation AccelerateDecelerateInterpolator – rate increases then decreases AccelerateInterpolator – rate increases AnticipateInterpolator – rate starts backward then flings forward AnticipateOvershootInterpolator – rate starts backwards, flings forward, overshoots and returns to target end value BounceInterpolator – change bounces at the end CycleInterpolator – repeats for a specified number of cycles DecelerateInterpolator – rate decreases LinearInterpolator – constant rate OvershootInterpolator – flings forwards, overshoots and returns to target end value TimeInterpolator – interface for defining your own interpolator Can implement your own interpolator by implementing TimeInterpolator interface

Animating with ValueAnimator ValueAnimator lets you define a range of int, float or colour values to animate through Get a ValueAnimator by calling one of its factory methods ofInt(), ofFloat() or ofObject() See above

Animating with ValueAnimator ValueAnimator animation = ValueAnimator.ofObject( new MyTypeEvaluator(), startPropertyValue, endPropertyValue); animation.setDuration(1000); animation.start(); Can also specify a custom type to animate see above Here, ValueAnimator calculates values between startPropertyValue and endPropertyValue, using MyTypeEvaluator()

Animation Listeners ValueAnimator does not set values in animated object properties Only computes values Define AnimatorUpdateListeners in ValueAnimator to handle important events during the animation (e.g., frame updates) Need to implement onAnimationUpdate(ValueAnimator animation) Get calculated value for the current frame refresh by calling ValueAnimator’s getAnimatedValue() method

Animating with ObjectAnimator ObjectAnimator is a subclass of ValueAnimator Combines timing and calculation functionality of ValueAnimator with ability to set values in animated properties No need to define AnimatorUpdateListeners Create an ObjectAnimator by using one of the factory methods and passing object property (as a String) starting and ending value of animated property See above

Using an ObjectAnimator In order for the ObjectAnimator to update properties correctly Object property must have a setter in camel case of form “set<propertyName> e.g., must be a “setFoo()” method if property is “foo” If you don’t have such a setter, you can implement one define a wrapper class that has one and forwards value to the original object use a ValueAnimator and define an AnimatorUpdateListener

Using an ObjectAnimator If you only specify ending value of animation when constructing an ObjectAnimator then object property must have a getter function, “get<propertyName>” to get the start value e.g., “getFoo()” if property name is “foo”

Using an ObjectAnimator Return type of getter and argument type of setter must be the same as the type of the starting and ending values you specify when constructing the ObjectAnimator e.g., in example above targetObject.getPropName() returns a float targetObject.setPropName(float f) is passed a float

Forcing a screen redraw Depending on animated property, you may need to call View.invalidate() on a View to redraw the screen when animated values are updated Do this in ValueAnimator.AnimatorUpdateListener.onAnimationUpdate(ValueAnimator) e.g., if change colour of a Drawable object, must wait for object to redraw itself All property setters on View invalidate the View and cause it to be redrawn

Using AnimatorSet When you play an animation may depend on when another one starts or finishes AnimatorSet lets you bundle animations together and start them simultaneously sequentially after a specified delay AnimatorSets can be nested, one inside the other

Example of an AnimatorSet Example plays Animator objects in the following manner: Plays bounceAnim Plays squashAnim1, squashAnim2, stretchAnim1 and stretchAnim2 at the same time Plays bounceBackAnim Plays fadeAnim

Example Bouncing Ball demo

Animation Listeners During an animation, you can listen for important events using these listeners: Animator.AnimatorListener onAnimationStart() – called when animation starts onAnimationEnd() – called when animation ends onAnimationRepeat() – called when animation repeats onAnimationCancel() – called when animation canceled onAnimationEnd() also called ValueAnimator.AnimatorUpdateListener onAnimationUpdate() – called on every frame implement to use values calculated by ValueAnimator call valueAnimator.getAnimatedValue() on ValueAnimator passed to this callback must implement this listener if using ValueAnimator

AnimatorListenerAdapter AnimatorListenerAdapter provides empty implementations of AnimatorListener methods So you don’t have to implement all the methods in AnimatorListener – just override the ones that are necessary e.g., Bouncing Balls sample creates AnimatorListenerAdapter just for onAnimationEnd() callback see BouncingBalls.java, line 136

Animating layout changes to ViewGroups Property animation allows ViewGroups to be animated (not just Views) Use LayoutTransition class to animate layout changes Views inside a ViewGroup can go through an appearing or disappearing animation when added to or removed from a ViewGroup when setVisibility() method called

LayoutTransition Use LayoutTransition.setAnimator(int transitionType, Animator animator) and use one of the following LayoutTransition constants APPEARING – indicates animation running on items appearing in the container CHANGE_APPEARING – animation runs on items changing because a new item is appearing DISAPPEARING – animation rungs on items disappearing from container CHANGE_DISAPPEARING – animation runs on items changing due to item disappearing from container Can define custom animations or use system defaults

Example See LayoutAnimations sample

Enabling default layout transitions LayoutAnimationsByDefault sample and its layout resource file show how to enable default layout transitions for ViewGroups in XML Just set android:animateLayoutChanges attribute to true – see above This automatically animates Views added to or removed from ViewGroup as well as remaining Views in the ViewGroup

Example See LayoutAnimationsByDefault sample

Using a TypeEvaluator Make your own implementation of the TypeEvaluator interface if you need to animate a type other than int, float or color TypeEvaluator declares one method: evaluate() Must allow the animator to return a correct value for your animated property at the current point in the animation See FloatEvaluator class above Note that evaluate receives interpolated fraction (not elapsed fraction)

Using interpolators Interpolator defines how value in an animation are calculated as a function of elapsed time animations may proceed linearly at a constant rate or the rate at which they proceed may change over the duration of the animation Interpolators receive elapsed fraction from Animator and convert it into an interpolated fraction Android provides interpolators in android.view.animation package Can also make your own by implementing TimeInterpolator interface

Example LinearInterpolator returns elapsed fraction AccelerateDecelerateInterpolator accelerates into animation and decelerates out of it (see above)

PropertyValuesHolder PropertyValuesHolder used to hold information about an animated property Can be used to create animations with ValueAnimator or ObjectAnimator that operate on several different properties in parallel

Keyframes Keyframe is a time-value pair that specifies a particular value at a particular time during the animation Each keyframe can have an interpolator controlling change rate over period since preceding keyframe Keyframe constructed by factory method ofInt(), ofFloat(), ofObject() e.g., Keyframe.ofFloat(.5f, 360f) Then use PropertyValuesHolder.ofKeyFrame() to get a PropertyValuesHolder object Can get ObjectAnimator by passing target object and PropertyValuesHolder to the ObjectAnimator

Example MultiPropertyAnimation sample in API demos

Animating Views View animation transforms objects by changing the way they are drawn without changing the objects themselves causes view object to be in constant position even though drawn in a different place on the screen Property animation animates views by changing their properties – i.e., changing the objects themselves, not just how they are drawn

Changes to the View class to support property animation translationX, translationY vector from original position of View to current position rotation, rotationX, rotationY controls rotation in 2D (rotation) and 3D (rotationX, rotationY) scaleX, scaleY 2D scaling of View around a pivot point pivotX, pivotY location of pivot point for rotation and scaling x, y final location of View alpha transparency: 0 = opaque; 1=invisible.

Animating Views To animate a View property, e.g., color or rotation, create a property animator specify the View property that you want to animate

Animating with ViewPropertyAnimator Use a ViewPropertyAnimator if you want to animate many properties of an object at once more efficient and leads to shorter code Works like an ObjectAnimator – updates values, doesn’t just compute them

Example of using ViewPropertyAnimator

Declaring animations in XML Can define property animations in XML instead of programmatically lets you reuse your animations more easily lets you edit the animation sequence more easily Animations that use the legacy View animation API should be stored in res/anim Animations using the new property animation API should be stored in res/animator Eclipse ADT plugin only looks in res/animator

Declaring animations in XML XML tags for animation classes: ValueAnimator: <animator> ObjectAnimator: <objectAnimator> AnimatorSet: <set> On left, plays a set of two object animations together and then a single object animator afterwards

Declaring animations in XML In order to run previous XML animation, must inflate it to an AnimatorSet and set target objects for all animations before starting the set setTarget() sets a single target object for all children of the AnimatorSet See code