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

Nul*_*ion 10 android android-animation android-view objectanimator

我想使用scaleX和scaleY振动视图,我正在使用此代码进行操作,但问题是有时视图未正确重置,并且显示应用了比例...

我希望在动画结束时,必须始终以原始状态查看视图

这是代码:

                ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.9f);
                scaleX.setDuration(50);
                scaleX.setRepeatCount(5);
                scaleX.setRepeatMode(Animation.REVERSE);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.9f);
                scaleY.setDuration(50);     
                scaleY.setRepeatCount(5);
                scaleY.setRepeatMode(Animation.REVERSE);
                set.play(scaleX).with(scaleY);
                set.start();
Run Code Online (Sandbox Code Playgroud)

谢谢

pri*_*xes 13

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

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

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的代码中

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


Álv*_*ria -4

您可以添加一个 AnimatorListener,以便在动画结束时收到通知:

scaleY.addListener(new AnimatorListener() {

            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Restore view

            }
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }



            @Override
            public void onAnimationCancel(Animator animation) {

            }
        });
Run Code Online (Sandbox Code Playgroud)