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

16

我想用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();

谢谢

3个回答

18

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

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

更新:在安卓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);
    }
}
在你的代码中。
animator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            animation.removeListener(this);
            animation.setDuration(0);
            animation.setInterpolator(new ReverseInterpolator());
            animation.start();
        }
});

为什么需要 animation.removeListener(this); - David Chen
@DavidChen 因为在 .reverse() 之后,onAnimationEnd() 方法会再次被调用。 - princeparadoxes
1
在我的测试中,这个解决方案产生了一个无休止的震动,而不是停止并返回到原始大小。可能有一个错误吗? - KJaeg

3

根据文档(属性动画):
属性动画系统可以通过更改View对象中的实际属性来对屏幕上的视图进行动画处理。此外,每当其属性被更改时,视图还会自动调用invalidate()方法以刷新屏幕。
因此,您可以使用AnimatorListener来监听动画事件,然后重置您所动画处理的视图属性,比如取消(cancel)事件和缩放(scaleX)属性。
scaleAnimator.setListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationCancel(Animator animation) {
        scaleView.setScaleX(0)
    }
});

希望这能有所帮助。

-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) {

            }
        });

是的,我知道,但是...我该如何恢复视图的原始状态? - NullPointerException
你尝试过使用 yourView.clearAnimation() 来清除动画吗? - Álvaro Pérez Soria
好的,我看到您正在使用ObjectAnimator。这是一种属性动画。这里有一个很好的解释,关于属性动画和视图动画之间的区别:http://stackoverflow.com/questions/11855570/reset-animation-effects-in-android。您应该尝试创建一个视图动画,因为它不会影响原始视图。 - Álvaro Pérez Soria

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接