且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将ObjectAnimator重置为其初始状态?

更新时间:2023-01-31 20:38:43

对于ValueAnimator和ObjectAnimator可以这样尝试:

For ValueAnimator and ObjectAnimator can be like this a try:

animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        animation.removeListener(this);
        animation.setDuration(0);
        ((ValueAnimator) animation).reverse();
    }
});

更新 在Android 7上不起作用. ***的方法是使用插值器.

UPDATE On Android 7 it doesn't work. Best way use the interpolator.

public class ReverseInterpolator implements Interpolator {

    private final Interpolator delegate;

    public ReverseInterpolator(Interpolator delegate){
        this.delegate = delegate;
    }

    public ReverseInterpolator(){
        this(new LinearInterpolator());
    }

    @Override
    public float getInterpolation(float input) {
        return 1 - delegate.getInterpolation(input);
    }
}

在您的代码中

animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            animation.removeListener(this);
            animation.setDuration(0);
            animation.setInterpolator(new ReverseInterpolator());
            animation.start();
        }
});