如何停止动画(cancel()不起作用)

Mix*_*Mix 227 animation android

我需要停止正在运行的翻译动画.该.cancel()方法Animation无效; 无论如何,动画一直持续到最后.

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

Com*_*are 483

打电话clearAnimation()View你打电话startAnimation().

  • 你好,我们又见面了!当动画被取消时,我找到了获得点(在插值时间方面)的方法.你需要制作动画子类并从android代码动画中放置代码(例如TranslateAnimation).在您的课程中,您将能够在applyTransformation()函数中保存和跟踪位置. (6认同)

Sea*_*eau 36

在Android 4.4.4上,似乎唯一可以阻止View上的alpha淡入淡出动画的方式是调用View.animate().cancel()(即调用.cancel()View ViewPropertyAnimator).

这是我在ICS之前和之后用于兼容性的代码:

public void stopAnimation(View v) {
    v.clearAnimation();
    if (canCancelAnimation()) {
        v.animate().cancel();
    }
}
Run Code Online (Sandbox Code Playgroud)

...用方法:

/**
 * Returns true if the API level supports canceling existing animations via the
 * ViewPropertyAnimator, and false if it does not
 * @return true if the API level supports canceling existing animations via the
 * ViewPropertyAnimator, and false if it does not
 */
public static boolean canCancelAnimation() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}
Run Code Online (Sandbox Code Playgroud)

这是我要停止的动画:

v.setAlpha(0f);
v.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation listener set on the view.
v.animate()
    .alpha(1f)
    .setDuration(animationDuration)
    .setListener(null);
Run Code Online (Sandbox Code Playgroud)


DjP*_*DjP 18

如果您正在使用动画侦听器,请设置v.setAnimationListener(null).对所有选项使用以下代码.

v.getAnimation().cancel();
v.clearAnimation();
animation.setAnimationListener(null);
Run Code Online (Sandbox Code Playgroud)

  • 调用`v.setAnimation(null);'不是必要的,因为这将在`v.clearAnimation();`中完成. (2认同)

rz0*_*rz0 5

你必须使用 .clearAnimation(); UI线程中的方法:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        v.clearAnimation();
    }
});
Run Code Online (Sandbox Code Playgroud)