如何在不调用 Listener.onAnimationEnd 的情况下停止 ValueAnimator

How to stop an ValueAnimator without Listener.onAnimationEnd called

我将 AnimatorListenerAdapter 添加到 ValueAnimator 中,但有时我需要在 运行 时停止 ValueAnimator

我试过 ValueAnimator.cancel()ValueAnimator.stop(),它们都可以停止动画师,但我不想调用 AnimatorListenerAdapter.onAnimationEnd()

ValueAnimator.pause() 有效,但它只在 api 19 以上,所以我不能使用它;

我还有其他办法吗?

使用 cancel()stop(),以及某处的 boolean 字段。 boolean 作为一个标志告诉 onAnimationEnd() 它是否应该做它的常规工作。默认为 booleantrue;当您想阻止正常的动画结束处理时,将其翻转为 false

在某些情况下,使用标志可能会导致问题。

您应该在调用取消方法之前使用 removeListener:

animator.removeAllListeners(); // Or animator.removeListener(your listener);
animator.cancel();