Android animation property animation ValueAnimator

Property Animation 2 Property Animation

ValueAnimator (difference animation)

Simply understood literally, ObjectAnimator acts on an actual object, and ValueAnimator is the parent class of ObjectAnimator , which inherits from the abstract class Animator . It acts on a value, changing it from one value to another, and then according to the value According to certain rules, dynamically modify the properties of the View , such as the position, transparency, rotation angle, size, etc. of the View , to complete the animation effect.

 

/*
     Here, a ValueAnimator instance is constructed by the ofFloat() method. In addition, other functions ofInt() , ofObject() , ofPropertyValuesHolder() are provided.
     After 
    api 21 , ofArgb() is provided . Each function is Multiple change values ​​can be passed in.
 */
public void initAnim() {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 126512.36f);
    valueAnimator.setDuration(2000);
    valueAnimator.setInterpolator(new LinearInterpolator());
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float money = (float) animation.getAnimatedValue();
            mTextView.setText(String.format("%.2f", money));
        }
    });
    valueAnimator.start();
}

 

Interpolator

// How to customize an interpolator? Example: We implement an interpolator that first decelerates and then accelerates. The code is as follows
 public class DecelerateAccelerateInterpolator implements Interpolator {
     @Override
     public float getInterpolation ( float input) {
         float result ;
        if (input <= 0.5f) {
            result = (float) (Math.sin(Math.PI * input)) / 2.0f;
        } else {
            result = (float) (2 - Math.sin(Math.PI * input)) / 2.0f;
        }
        return result;
    }
}

 

TypeEvaluator

//TypeEvaluator (Evaluator)
 /*
TypeEvaluator is used to calculate the changed attribute value based on the percentage of the current attribute change. The system provides the following estimators: IntEvaluator for 
integer attributes 
IntArrayEvaluator for integer attribute sets 
FloatEvaluator for floating point attributes 
FloatArrayEvaluator for floating point attribute sets 
ArgbEvaluator   for Color property 
RectEvaluator for Rect property 
PointFEvaluator for PointF property 
*/

 

// The interface provides evaluate(float fraction, T startValue, T endValue);
// When the animation is running, Interpolator automatically calculates the percentage fraction of the animation running ,
 // Then TypeEvaluator calculates the attribute value of the current animation
 according to the fraction . public class FloatEvaluator implements TypeEvaluator<Number> {
     public Float evaluate ( float fraction , Number startValue , Number endValue) {
         float startFloat = startValue.floatValue() ;
        return startFloat + fraction * (endValue.floatValue() - startFloat ) ;
    }
}

 

Custom TypeEvaLuator

// How to customize TypeEvaLuator ? For example, we want to realize an animation with the increase of money and the redder the font color,
 // We all know that ofArgb() can be used now, but ofArgb() needs api 21 or above to use,
 // So we need to customize an ArgbEvaLuator , here In order to demonstrate custom
 // TypeEvaluator directly use the ArgbEvaluator source code
 provided in api 21 to use public class TextArgbEvaluator implements TypeEvaluator {

    public Object evaluate(float fraction, Object startValue, Object endValue) {
        int startInt = (Integer) startValue;
        int startA = (startInt >> 24) & 0xff;
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;

        int endInt = (Integer) endValue;
        int endA = (endInt >> 24) & 0xff;
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;

        return (int) ((startA + (int) (fraction * (endA - startA))) << 24) |
                (int) ((startR + (int) (fraction * (endR - startR))) << 16) |
                (int) ((startG + (int) (fraction * (endG - startG))) << 8) |
                (int) ((startB + (int) (fraction * (endB - startB))));
    }
}

 

调用方式

//调用方式:
public void ss() {
    AnimatorSet animatorSet = new AnimatorSet();
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 126512.36f);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float money = (float) animation.getAnimatedValue();
            Log.e("Interpolator", "money---->" + money);
            mTextView.setText(String.format("%.2f", money));
        }
    });

    int startColor = Color.parseColor("#FCA3AB");
    int endColor = Color.parseColor("#FB0435");
    ValueAnimator colorAnimator = ValueAnimator.ofObject(new TextArgbEvaluator(), startColor, endColor);
    colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int color = (int) animation.getAnimatedValue();
            Log.e("Interpolator", "color---->" + color);
            mTextView.setTextColor(color);
        }
    });

    animatorSet.playTogether(valueAnimator, colorAnimator);
    animatorSet.setDuration(5000);
    animatorSet.setInterpolator(new LinearInterpolator());
    animatorSet.start();
}

 

Guess you like

Origin blog.csdn.net/l331258747/article/details/70858290