Thứ Năm, 8 tháng 11, 2012

Working with Animations


In Chapter 3, you learned how to define animations as external resources. Now, you get the opportunity
to put them to use.
Android offers two kinds of animation:
➤ Frame-by-Frame Animations Traditional cell-based animations in which a different Draw-
able is displayed in each frame. Frame-by-frame animations are displayed within a View,
using its Canvas as a projection screen.
➤ Tweened Animations Tweened animations are applied to Views, letting you define a series
of changes in position, size, rotation, and opacity that animate the View contents.
Both animation types are restricted t o the original bounds of the View they’re
applied to. Rotations, translations, and sc aling transformations that extend beyond
the original boundaries of the View will result in the contents being clipped.


Introducing Tweened Animations
Tweened animations offer a simple way to provide depth, movement, or feedback to your users at a
minimal resource cost.
Using animations to apply a set of orientation, scale, position, and opacity changes is much less
resource-intensive than manually redrawing the Canvas to achieve similar effects, not to mention far
simpler to implement.
Tweened animations are commonly used to:
➤ Transition between Activities.
➤ Transition between layouts within an Activity.
➤ Transition between different content displayed within the same View.
➤ Provide user feedback such as:
➤ Indicating progress.
➤ ‘‘Shaking’’ an input box to indicate an incorrect or invalid data entry.
Creating Tweened Animations
Tweened animations are created using theAnimation class. The following list explains the animation
types available.
➤ AlphaAnimation Lets you animate a change in the View’s transparency (opacity or alpha
blending).
➤ RotateAnimation Lets you spin the selected View canvas in the XY plane.
➤ ScaleAnimation Allows you to zoom in to or out from the selected View.
➤ TranslateAnimation Lets you move the selected View around the screen (although it will
only be drawn within its original bounds).
Android offers the AnimationSetclass to group and configure animations to be run as a set. You can
define the start time and duration of each animation used within a set to control the timing and order
of the animation sequence.
It’s important to set the start offset and duration for each child animation, or they
will all start and complete at the same time.
Listings 15-12 and 15-13 demonstrate how to create the same animation sequence in code or as an
external resource.
LISTING 15-12: Creating a tweened animation in code
>>>>>>>>>>>>>>>>code

As you can see, it’s generally both easier and more intuitive to create your animation sequences using
an external animation resource.
Applying Tweened Animations
Animations can be applied to any View by calling its startAnimation method and passing in the Ani-
mation or Animation Set to apply.
Animation sequences will run once and then stop, unless you modify this behavior using the
setRepeatMode and setRepeatCount methods on the Animation or Animation Set. You can force an
animation to loop or repeat in reverse by setting the repeat mode of RESTARTor REVERSErespectively.
Setting the repeat count controls the number of times the animation will repeat.
Listing 15-14 shows an Animation that repeats indefinitely.
LISTING 15-14: Applying an Animation that loops continuously
myAnimation.setRepeatMode( Animation.RESTART);
myAnimation.setRepeatCount(Animation.INFINITE );
myView.startAnimation(myAnimation);
Using Animation Listeners
The AnimationListenerlets you create an event handler that’s fired when an animation begins or ends.
This lets you perform actions before or after an animation has completed, such as changing the View
contents or chaining multiple animations.
Call setAnimationListeneron an Animation object, and pass in a new implementation of
AnimationListener, overriding onAnimationEnd, onAnimationStart ,andonAnimationRepeat as
required.
Listing 15-15 shows the basic implementation of an Animation Listener.
>>>>>>>>>>>>>>>>>code


Animating Layouts and View Groups
A LayoutAnimationis used to animate View Groups, applying a single Animation (or Animation Set)
to each child View in a predetermined sequence.
Use aLayoutAnimationControllerto specify an Animation (or Animation Set) that’s applied to each
child View in a View Group. Each View it contains will have the same animation applied, but you can
use the Layout Animation Controller to specify the order and start time for each View.
Android includes twoLayoutAnimationControllerclasses.
➤ LayoutAnimationController Lets you select the start offset of each View (in milliseconds)
and the order (forward, reverse,andrandom ) to apply the animation to each child View.
➤ GridLayoutAnimationController Is a derived class that lets you assign the animation
sequence of the child Views using grid row and column references.


Creating Layout Animations
To create a new Layout Animation, start by defining the Animation to apply to each child View. Then
create a new LayoutAnimation, either in code or as an external animation resource, that references the
animation to apply and defines the order and timing in which to apply it.
Listing 15-16 show the definition of a simple Animation stored as popin.xml in the res/anim folder, and
a Layout Animation definition stored as popinlayout.xml.
The Layout Animation applies a simple ‘‘pop-in’’ animation randomly to each child View of any View
Group it’s assigned to.
>>>>>>>>>>>>>>>>>code


Using Layout Animations
Once you’ve defined a Layout Animation, you can apply it to a View Group either in code or in the
layout XML resource. In XML this is done using the android:layoutAnimation tag in the layout
definition:
android:layoutAnimation="@anim/popinlayout"
To set a Layout Animation in code, call setLayoutAnimation on the View Group, passing in a reference
to the LayoutAnimationobject you want to apply.
In each case, the Layout Animation will execute once, when the View Group is first laid out. You can
force it to execute again by calling scheduleLayoutAnimation on the ViewGroup object. The animation
will then be executed the next time the View Group is laid out.
Layout Animations also support Animation Listeners.

>>>>>>>>>>>>>>>code

Creating and Using Frame-by-Frame Animations
Frame-by-frame animations are akin to traditional cel-based cartoons in which an image is chosen for
each frame. Where tweened animations use the ta rget View to supply the content of the animation,
frame-by-frame animations let you specify a series of Drawable objects that are used as the background
to a View.
The AnimationDrawable class is used to create a new frame-by-frame animation presented as a
Drawable resource. You can define your Animation Drawa ble resource as an external resource in your
project’s res/drawable folder using XML.
Use the<animation-list>tagtogroupacollectionof <item>nodes, each of which uses a drawable
attribute to define an image to display, and a duration attribute to specify the time (in milliseconds) to
display it.
Listing 15-18 shows how to create a simple animation that displays a rocket taking off (rocket images
not included). The file is stored as res/drawable/animated_rocket.xml.
>>>>>>>>>>>>>Code

To display your animation, set it as the background to a View using the setBackgroundResource
method.
ImageView image = (ImageView)findViewById(R.id.my_animation_frame);
image.setBackgroundResource(R.drawable.animated_rocket);
Alternatively, use thesetBackgroundDrawable to use a Drawable instance instead of a resource refer-
ence. Run the animation calling its startmethod.





Không có nhận xét nào:

Đăng nhận xét