From 070c84d9ebf043c9b61d2b41bb8cd6dfa616e6ac Mon Sep 17 00:00:00 2001
From: Robert Ly The Android system provides a flexible animation system that allows you to animate
almost anything, either programmatically or declaratively with XML. There are two
- animation systems that you can choose from: property
+ animation systems that you can choose from: property
animation and view animation. You can use whichever
system that matches your needs, but use only one system for each object that you
are animating. Most of the property animation system's features can be found in
{@link android.animation android.animation}. Because the
- view animation system already
defines many interpolators in {@link android.view.animation android.view.animation},
you will use those to define your animation's interpolation in the property animation
system as well.
@@ -163,7 +163,7 @@ page.title=Animation
The Android system provides a set of common interpolators in
{@link android.view.animation android.view.animation}. If none of these suits your needs, you
can implement the {@link android.animation.TimeInterpolator} interface and create
- your own. See Interpolators for more information on
+ your own. See Using interpolators for more information on
how to write a custom interpolator.
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
fadeAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
balls.remove(((ObjectAnimator)animation).getTarget());
-}
-
-
+}
Instantiating an {@link android.animation.ObjectAnimator} is similar to a {@link android.animation.ValueAnimator}, but you also specify the object and that object's property (as a String) that you want to animate:
--ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f); +ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f); anim.setDuration(1000); -anim.start(); -+anim.start();
To have the {@link android.animation.ObjectAnimator} update properties correctly, you must do the following:
@@ -355,7 +352,7 @@ anim.start(); -If you want to animate a type that is unknown to the Android system, you can create your own evaluator by implementing the {@link @@ -369,15 +366,13 @@ anim.start(); This allows the animator that you are using to return an appropriate value for your animated property at the current point of the animation. The {@link android.animation.FloatEvaluator} class demonstrates how to do this:
-
-public class FloatEvaluator implements TypeEvaluator {
+ public class FloatEvaluator implements TypeEvaluator {
public Object evaluate(float fraction, Object startValue, Object endValue) {
float startFloat = ((Number) startValue).floatValue();
return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
}
-}
-
+}
Note: When {@link android.animation.ValueAnimator} (or {@link android.animation.ObjectAnimator}) runs, it calculates a current elapsed @@ -387,7 +382,7 @@ public class FloatEvaluator implements TypeEvaluator { parameter, so you do not have to take into account the interpolator when calculating animated values.
-An interpolator define how specific values in an animation are calculated as a function of time. For example, you can specify animations to happen @@ -414,12 +409,12 @@ public class FloatEvaluator implements TypeEvaluator {
AccelerateDecelerateInterpolator
public float getInterpolation(float input) {
return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
- }
+}
LinearInterpolator
public float getInterpolation(float input) {
return input;
- }
+}
The following table represents the approximate values that are calculated by these interpolators for an animation that lasts 1000ms:
@@ -488,7 +483,7 @@ public class FloatEvaluator implements TypeEvaluator { {@link android.view.animation.LinearInterpolator} between 200ms and 600ms and slower between 600ms and 1000ms. -A {@link android.animation.Keyframe} object consists of a time/value pair that lets you define a specific state at a specific time of an animation. Each keyframe can also @@ -505,19 +500,18 @@ public class FloatEvaluator implements TypeEvaluator { object, you can obtain an animator by passing in the {@link android.animation.PropertyValuesHolder} object and the object to animate. The following code snippet demonstrates how to do this:
-
- Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
- Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
- Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
- PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
- ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
- rotationAnim.setDuration(5000ms);
-
-For a more complete example on how to use keyframes, see the Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
+Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
+Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
+PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
+ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)
+rotationAnim.setDuration(5000ms);
+
+For a more complete example on how to use keyframes, see the - MultiPropertyAnimation sample in APIDemos. + MultiPropertyAnimation
sample in APIDemos. -In many cases, you want to play an animation that depends on when another animation starts or finishes. The Android system lets you bundle animations together into an @@ -559,7 +553,7 @@ animatorSet.start();
As with view animation, you can declare property animations with +
As with view animation, you can declare property animations with XML instead of doing it programmatically. The following Android classes also have XML declaration support with the following XML tags:
@@ -639,14 +633,13 @@ animatorSet.start(); android:propertyName="y" android:duration="500" android:valueTo="300" - android:valueType="int" > - </set> - <objectAnimator - android:propertyName="alpha" - android:duration="500" - android:valueTo="0f"/> - </set> - + android:valueType="int"/> + </set> + <objectAnimator + android:propertyName="alpha" + android:duration="500" + android:valueTo="0f"/> +</set>In order to run this animation, you must inflate the XML resources in your code to an {@link android.animation.AnimatorSet} object, and then set the target objects for all of @@ -698,40 +691,38 @@ animatorSet.start();
The following XML from one of the ApiDemos is used to stretch, then simultaneously spin and rotate a View object.
--<set android:shareInterpolator="false"> - <scale - android:interpolator="@android:anim/accelerate_decelerate_interpolator" - android:fromXScale="1.0" - android:toXScale="1.4" - android:fromYScale="1.0" - android:toYScale="0.6" - android:pivotX="50%" - android:pivotY="50%" - android:fillAfter="false" - android:duration="700" /> - <set android:interpolator="@android:anim/decelerate_interpolator"> - <scale - android:fromXScale="1.4" - android:toXScale="0.0" - android:fromYScale="0.6" - android:toYScale="0.0" - android:pivotX="50%" - android:pivotY="50%" - android:startOffset="700" - android:duration="400" - android:fillBefore="false" /> - <rotate - android:fromDegrees="0" - android:toDegrees="-45" - android:toYScale="0.0" - android:pivotX="50%" - android:pivotY="50%" - android:startOffset="700" - android:duration="400" /> - </set> -</set> -+
<set android:shareInterpolator="false"> + <scale + android:interpolator="@android:anim/accelerate_decelerate_interpolator" + android:fromXScale="1.0" + android:toXScale="1.4" + android:fromYScale="1.0" + android:toYScale="0.6" + android:pivotX="50%" + android:pivotY="50%" + android:fillAfter="false" + android:duration="700" /> + <set android:interpolator="@android:anim/decelerate_interpolator"> + <scale + android:fromXScale="1.4" + android:toXScale="0.0" + android:fromYScale="0.6" + android:toYScale="0.0" + android:pivotX="50%" + android:pivotY="50%" + android:startOffset="700" + android:duration="400" + android:fillBefore="false" /> + <rotate + android:fromDegrees="0" + android:toDegrees="-45" + android:toYScale="0.0" + android:pivotX="50%" + android:pivotY="50%" + android:startOffset="700" + android:duration="400" /> + </set> +</set>
Screen coordinates (not used in this example) are (0,0) at the upper left hand corner, and increase as you go down and to the right.
@@ -805,8 +796,7 @@ spaceshipImage.startAnimation(hyperspaceJumpAnimation); image to a View and then called to play. Here's an example Activity, in which the animation is added to an {@link android.widget.ImageView} and then animated when the screen is touched: -
-AnimationDrawable rocketAnimation;
+ AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -823,8 +813,7 @@ public boolean onTouchEvent(MotionEvent event) {
return true;
}
return super.onTouchEvent(event);
-}
-
+}
It's important to note that the start() method called on the
AnimationDrawable cannot be called during the onCreate() method of your
--
cgit v1.2.3-59-g8ed1b