Android property animation, value animation, and android property Animation
- Android property Animation
- What is Android property animation?
- Comparison of similar technologies
- Tween Animation
- Frame Animation
- Property Animation
- Introduction to attributes animation Components
- ObjectAnimator object animation execution class
- ValueAnimator value animation execution class
- PropertyValuesHolder
- Keyframe
- AnimatorSet execution set class
- AnimatorUpdateListener animation update listener
- AnimatorListener animation execution listener
- AnimatorInflater animation Loader
- TypeEvaluator type valuation
- Several common interpolation Devices
Android property animation what is Android property Animation
The Property Animation system is a robust Animation framework system that can meet most of your Animation needs. Whether or not an animation object has been drawn to the screen, you can change any attribute value during animation execution. An Attribute animation changes the value of an attribute (a field in an object) within a certain period of time. You can use the following steps to define an animation: Specify the attributes of the animation to be executed, such as the position of the animation object (View) on the screen, and specify the execution duration, specify the change value of the expected attribute.
Comparison of similar technologies: The Property Animation (Frame Animation), a Property Animation, is a component of the Tween Animation Frame Animation. Related Classes: ObjectAnimator object Animation execution class Introduction: Example:
// Construct an ObjectAnimator object through static methods // set the target object, attribute name, and value set ObjectAnimator. ofFloat (view, "translationX", 0.0F, 200366f) // set the execution time (1000 ms ). setDuration (1000) // start the animation. start ();
2. Composite example: The View is automatically dropped and then popped up and executed once.
// Modify the view's y attribute and move it from the current position to 300.0 fObjectAnimator yBouncer = ObjectAnimator. ofFloat (view, "y", view. getY (), 300366f); yBouncer. setDuration (1500); // sets the interpolation tool (used to adjust the speed of the animation Execution Process) yBouncer. setInterpolator (new BounceInterpolator (); // you can specify the number of repetitions. The default value is 0, indicating that repeated executions are not performed. setRepeatCount (1); // set RESTART or REVERSE. The number of retries is greater than 0 or INFINITE takes effect for yBouncer. setRepeatMode (ValueAnimator. REVERSE); // sets the animation start delay (200 ms) yBouncer. setStartDelay (200); // start animation yBouncer. start ();
ValueAnimator value animation execution class Introduction: Example:
// Construct a ValueAnimator object using a static method // set the value set ValueAnimator animator = ValueAnimator. ofFloat (0f, 200366f); // sets the animation object to be used. setTarget (view); // set the execution time (1000 ms) animator. setDuration (1000); // Add an animation update listener to animator. addUpdateListener (new AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {// obtain the current value Float mValue = (Float) animation. getAnimatedValue (); // sets the horizontal offset view. setTranslationX (mValue); // sets the vertical offset view. setTranslationY (mValue) ;}}); // start animation animator. start ();
PropertyValuesHolder:
Provides multiple operation attributes and corresponding execution parameters for ValueAnimator.
Example:
// Obtain the left position of the view, int left = view. getLeft (); // get the right position of the view, int right = view. getRight (); // Add 10 pixels to the left of the view. PropertyValuesHolder pvhLeft = PropertyValuesHolder. ofInt ("left", left, left + 10); // reduce the right side of the view by 10 pixels PropertyValuesHolder pvhRight = PropertyValuesHolder. ofInt ("right", right, right-10); // scale from 1f to 0f on the X axis, and then to fPropertyValuesHolder pvhScaleX = PropertyValuesHolder. ofFloat ("scaleX", 1f, 0f, 1f); // scale in the Y axis from 1f to 0f, and then to fPropertyValuesHolder pvhScaleY = PropertyValuesHolder. ofFloat ("scaleY", 1f, 0f, 1f); // deliver PropertyValuesHolder to ObjectAnimator for building ObjectAnimator customAnim = ObjectAnimator. ofPropertyValuesHolder (view, pvhLeft, pvhRight, pvhScaleX, pvhScaleY); // set the execution time (1000 ms) customAnim. setDuration (1000); // start the customAnim animation. start ();
Keyframe:
Provides a set of key frame operation values for PropertyValuesHolder.
Example:
The following example shows the rotation animation of the PropertyValuesHolder. When the execution time is 0%, 50%, 100%, the rotation angles are 0 °, and 0 °, respectively. The animation is automatically supplemented during execution. It is displayed as spin before turning back.
// When the animation starts, the rotation angle is 0 degrees Keyframe kf0 = Keyframe. ofFloat (0f, 0f); // when the animation is executed at 50%, the rotation angle is degrees Keyframe kf1 = Keyframe. ofFloat// when the animation ends, the rotation angle is 0 degrees Keyframe kf2 = Keyframe. ofFloat (1f, 0f); // use PropertyValuesHolder to encapsulate the property name and value set PropertyValuesHolder pvhRotation = PropertyValuesHolder. ofKeyframe ("rotation", kf0, kf1, kf2); // execute ObjectAnimator through ObjectAnimator. ofPropertyValuesHolder (view, pvhRotation) // sets the execution time (1000 ms ). setDuration (1000) // start the animation. start ();
Introduction to the Collection class for executing the AnimatorSet operation: Example:
The playing sequence of the animation in the following example is
1. Play bounceAnim;
2. Play squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 simultaneously;
3. Play bounceBackAnim;
4. Finally play fadeAnim;
AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f,0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();
Animation update listener: Example:
// 1. manually update the View attributes in the callback: AnimatorUpdateListener l = new AnimatorUpdateListener () {public void onAnimationUpdate (ValueAnimator animation) {// The current value range is 0.0f-> 1.0f // The percentage of animation execution. Different from AnimatedValue. Float fraction = animation. getAnimatedFraction (); // The effect below is that the View is completely transparent to opacity, view. setAlpha (fraction); // The PX distance downward in the Y direction. view. setTranslationY (fraction * 300366f) ;}; ValueAnimator mAnim = ValueAnimator. ofFloat (0f, 1.0f); mAnim. addUpdateListener (l); mAnim. setDuration (1, 1000); mAnim. start ();
2. Used to trigger re-painting within the custom View:
// 2. the custom View is used to re-paint public class MyAnimationView extends View implements ValueAnimator. animatorUpdateListener {public MyAnimationView (Context context) {super (context) ;}@ Override public void onAnimationUpdate (ValueAnimator animation) {// manually trigger the interface to re-paint invalidate ();}}
Introduction to listening to animation execution using AnimatorListener: Example:
// Update the view transparency from the current 1.0f to 0.5f. After the animation ends, remove the ViewObjectAnimator anim = ObjectAnimator. ofFloat (view, "alpha", 0.5f); anim. setDuration (1, 1000); anim. addListener (new AnimatorListener () {@ Override public void onAnimationStart (Animator animation) {// call at animation start} @ Override public void onAnimationRepeat (Animator animation) {// call when the animation is repeated} @ Override public void onAnimationEnd (Animator animation) {// call ViewGroup p when the animation ends Arent = (ViewGroup) view. getParent (); if (parent! = Null) parent. removeView (view) ;}@ Override public void onAnimationCancel (Animator animation) {// call when the animation is canceled}); anim. start ();
Introduction to the AnimatorInflater animation Loader:
<setandroid:ordering=["together" ¦ "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float¦ int ¦ color" android:valueTo="float¦ int ¦ color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat"¦ "reverse"] android:valueType=["intType"¦ "floatType"]/> <animator android:duration="int" android:valueFrom="float¦ int ¦ color" android:valueTo="float¦ int ¦ color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat"¦ "reverse"] android:valueType=["intType"¦ "floatType"]/> <set> ... </set></set>
Example
// Load the xml property animation Animator anim = AnimatorInflater. loadAnimator (this, R. anim. animator_set); anim. setTarget (view); anim. start ();
Example
<?xml version="1.0" encoding="utf-8"?><set> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueTo="200" android:valueType="floatType" android:propertyName="x" android:repeatCount="1" android:repeatMode="reverse"/> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:valueTo="400" android:valueType="floatType" android:propertyName="y" android:repeatCount="1" android:repeatMode="reverse"/></set>
Description of TypeEvaluator type valuation: Example:
// Type valuation-parabolic example TypeEvaluator <PointF> typeEvaluator = new TypeEvaluator <PointF> () {@ Override public PointF evaluate (float fraction, PointF startValue, PointF endValue) {float time = fraction * 3; Log. e (TAG, time + ""); // 200px/s in the x direction, 0.5*200 * t PointF point = new PointF (); point. x = 120 * time; point. y = 0.5f * 200 * time; return point ;}}; ValueAnimator valueAnimator = ValueAnimator. ofObject (typeEvaluator, new PointF (0, 0); valueAnimator. setInterpolator (new LinearInterpolator (); valueAnimator. setDuration (3000); valueAnimator. start (); valueAnimator. addUpdateListener (new AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {PointF point = (PointF) animation. getAnimatedValue (); view. setX (point. x); view. setY (point. y );}});
Several common interpolation devices:
Custom Interpolation
A. Implement the Interpolator (TimeInterpolator) interface;
B. Rewrite the interface function float getInterpolation (floatinput ).