我可以重用ValueAnimator吗?

src*_*091 5 animation android reusability

我有以下代码(Scala中的Android项目):

val animator = new ValueAnimator
animator.setFloatValues(0f, 100f)
animator.setDuration(20000L)
animator.addUpdateListener(this) // prints current value to console
animator.start

override def onTouch(v: View, event: MotionEvent) = {
  animator.setFloatValues(100f, 0f)
  animator.setCurrentPlayTime(0)

  if (!animator.isRunning) animator.start
  true
}
Run Code Online (Sandbox Code Playgroud)

如果我在animator运行时触摸屏幕,则它将正确开始向后工作(因为交换了值)。但是,如果在完成后触摸屏幕,则什么也不会发生,也不会重新开始。

问题是我可以以某种方式重用此动画器并使其停止后再次工作以给定值吗?

小智 1

您无法重复使用动画。

在再次使用同一对象之前,您需要通过调用initialize()方法来reset()并重新初始化动画对象。这类似于实例化一个新的动画对象。