Vous êtes sur la page 1sur 18

Animation

There are two ways to create animations via XML declaration or in a Java class. Were going to focus on XML declaration if youre interested in java based declaration take a look at the Android JavaDocs and the subclasses ofandroid.view.animation.Animation.

Android supports two types of animations: Tweened Animations and Frame-by-frame animations. Tweened animations are applied to Views and transform them in size, position or opacity. For example you can fade in a view or rotate him. Frame-by-frame animations shows different drawables in a View. Both animations are limited to the original size of the view.

Tweened Animations
Tweened animations are instances of the Animation abstract class. You have predefined Animations via the Java classes AlphaAnimation, RotateAnimation, ScaleAnimation TranslateAnimation.

Animations can be defined via XMl resource files in the directory "res/anim" or via code. You can load the resource file via AnimationUtils.loadAnimiation(this,R.anim.An imation).

Per default the animation will have the same speed in getting applied. You can change this via an "Interpolator" class. This class can control the speed of the animation. You have several default classes, e.g. AccelerateInterpolator, DecelerateInterpolator, LinearInterpolator, BounceInterpolator, OvershootInterpolator and CycleInterpolator. For example the BounceInterpolator gives an bouncing effect once the animation is almost over. Please check the Javadoc for the rest of the Interpolator classes.

Alpha Animation
Alpha animations allows us to create fadein/fade-out effects All animations are stored in res/anim so we create a new xml file named alpha.xml there

<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android=http://schemas.android.com/ apk/res/android android:fromAlpha="0.0" android:toAlpha="0.9" android:duration="4000" />

fromAlphaStarting alpha value for the animation, where 1.0 means fully opaque and 0.0 means fully transparent. toAlphaEnding alpha value for the animation.

Rotate Animation
An animation that controls the rotation of an object. This rotation takes place int the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top left point. If not specified, (0,0) is the default rotation point.

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/ apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:toYScale="0.0" android:pivotX="40%" android:pivotY="30%" android:duration="4000" />

fromDegreesRotation offset to apply at the start of the animation. toDegreesRotation offset to apply at the end of the animation. pivotX The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. pivotYThe Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge.

Translate Animation
TranslateAnimation Lets you move the selected View around the screen (although it will only be drawn within its original bounds).

Translate
<translate xmlns:android="http://schemas.android.com/ apk/res/android" android:fromXDelta="200%" android:toXDelta="0%" android:fromYDelta="200%" android:toYDelta="0%" android:duration="3000" android:zAdjustment="top" />

fromXDelta Change in X coordinate to apply at the start of the animation toXDelta Change in X coordinate to apply at the end of the animation fromYDelta Change in Y coordinate to apply at the start of the animation to Ydelta Change in Y coordinate to apply at the end of the animation

Scale Animation
ScaleAnimation Allows you to zoom in to or out from the selected View.

Scale Animation
<scale xmlns:android="http://schemas.android.com/ apk/res/android" android:fromXScale="4" android:toXScale="1" android:fromYScale="3" android:toYScale="1" android:pivotX="50%" android:pivotY="50%" android:duration="4000" />

fromX Horizontal scaling factor to apply at the start of the animation toX Horizontal scaling factor to apply at the end of the animation fromY Vertical scaling factor to apply at the start of the animation toY Vertical scaling factor to apply at the end of the animation

Vous aimerez peut-être aussi