我需要停止一个运行翻译动画。Animation的.cancel()方法没有效果;不管怎样,动画会一直播放到最后。

如何取消正在运行的动画?


当前回答

Use the method setAnimation(null) to stop an animation, it exposed as public method in View.java, it is the base class for all widgets, which are used to create interactive UI components (buttons, text fields, etc.). /** * Sets the next animation to play for this view. * If you want the animation to play immediately, use * {@link #startAnimation(android.view.animation.Animation)} instead. * This method provides allows fine-grained * control over the start time and invalidation, but you * must make sure that 1) the animation has a start time set, and * 2) the view's parent (which controls animations on its children) * will be invalidated when the animation is supposed to * start. * * @param animation The next animation, or null. */ public void setAnimation(Animation animation)

其他回答

你必须使用.clearAnimation();方法:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        v.clearAnimation();
    }
});

Use the method setAnimation(null) to stop an animation, it exposed as public method in View.java, it is the base class for all widgets, which are used to create interactive UI components (buttons, text fields, etc.). /** * Sets the next animation to play for this view. * If you want the animation to play immediately, use * {@link #startAnimation(android.view.animation.Animation)} instead. * This method provides allows fine-grained * control over the start time and invalidation, but you * must make sure that 1) the animation has a start time set, and * 2) the view's parent (which controls animations on its children) * will be invalidated when the animation is supposed to * start. * * @param animation The next animation, or null. */ public void setAnimation(Animation animation)

在你调用startAnimation()的视图上调用clearAnimation()。

首先,移除所有与animatior或animator set相关的监听器。然后取消动画器或动画器集。

 inwardAnimationSet.removeAllListeners()
        inwardAnimationSet.cancel()

因为没有其他答案提到它,你可以很容易地使用ValueAnimator的cancel()停止动画。

ValueAnimator在制作动画方面非常强大。下面是一个使用ValueAnimator创建翻译动画的示例代码:

ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 5f);

int mDuration = 5000; //in millis
valueAnimator.setDuration(mDuration);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

   @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // Update your view's x or y coordinate
    }
});

valueAnimator.start();

然后通过调用停止动画

valueAnimator.cancel()`