preface

Tween animation is only visual changes, not real changes in position, and property animation makes up for the shortcomings of tween animation, really change the properties of View, and can customize the interpolator, to achieve a variety of effects.

Function object

No longer limited to View objects, it can be any Java object.

Inheritance relationships

The base class for View animations is Animation, and the base class for property animations is Animator

  • ValueAnimator: This animation animates the value of the property. It does not change the UI and cannot be animated directly. Something needs to be done by listening on the animation, and setting this value to the corresponding property in the listener will change the corresponding property.

  • ObjectAnimator: Animates an object directly. It will call the get/set method of the corresponding property of the object to set the value of the property of the object.

  • TimeAnimator: This also does not directly implement the animation, but simply provides a listening callback that returns the total time the animation has been executed, the time since the last animation was executed, etc. (callback method once every 16 or 17ms).

use

1. ValueAnimator

ValueAnimator only animates values. It does not make any substantial changes to the UI. Event listeners need to be added

ValueAnimatorBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1f.0f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                view.setAlpha((float) animation.getAnimatedValue()); }}); valueAnimator.setDuration(1000); valueAnimator.start(); }});Copy the code

Common methods

Method names meaning
valueAnimator.setDuration(); Set animation time
valueAnimator.setRepeatCount(); Set repetition times
valueAnimator.setInterpolator(); Set the interpolator
valueAnimator.setRepeatMode(); Set repeat mode
valueAnimator.setStartDelay; Set the delay

2. ObjectAnimator

ObjectAnimator is a subset of ValueAnimator and can operate directly on object properties. It is relatively easy to animate using ObjectAnimator.

 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha".1f.0f);
 animator.setDuration(1000);/ / time 1 s
 animator.start();
Copy the code

Common attribute values: Alpha, TranslationX, TranslationY, ScaleX, Rotation, RotationX, RotationY

ObjectAnimator.ofFloat(imageView, "alpha".1f.0f.1f);
ObjectAnimator.ofFloat(imageView, "rotation".0f.360f.0f);
ObjectAnimator.ofFloat(imageView, "translationX".0f, -300f.0f);
ObjectAnimator.ofFloat(imageView, "scaleX".1f.2f.1f);
Copy the code

Results demonstrate

code

Github demo code