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

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


当前回答

在经历了所有的事情之后,没有任何效果。当我在视图中应用多个动画时。 下面是对我有用的代码。 要启动连续淡入和淡出的动画

val mAnimationSet = AnimatorSet()
private fun performFadeAnimation() {
    val fadeOut: ObjectAnimator = ObjectAnimator.ofFloat(clScanPage, "alpha", 1f, 0f)
    fadeOut.duration = 1000
    val fadeIn: ObjectAnimator = ObjectAnimator.ofFloat(clScanPage, "alpha", 0f, 1f)
    fadeIn.duration = 1000
    mAnimationSet.play(fadeIn).after(fadeOut)
    mAnimationSet.addListener(animationListener)
    mAnimationSet.start()
}

连续循环的动画监听器

 private val animationListener=object : AnimatorListenerAdapter() {
    override fun onAnimationEnd(animation: Animator?) {
        super.onAnimationEnd(animation)
        mAnimationSet.start()
    }
}

停止循环中的动画。我做了以下事情。

private fun stopAnimation() {
    mAnimationSet.removeAllListeners()
}

其他回答

在Android 4.4.4,似乎唯一的方法我可以停止一个alpha淡出动画的视图是调用View.animate().cancel()(即,调用.cancel()在视图的ViewPropertyAnimator)。

以下是我在ICS之前和之后使用的兼容性代码:

public void stopAnimation(View v) {
    v.clearAnimation();
    if (canCancelAnimation()) {
        v.animate().cancel();
    }
}

…计算方法:

/**
 * 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;
}

这是我正在停止的动画:

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

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)

你可以尝试做的是在停止动画之前从动画中获取变换矩阵,并检查矩阵内容以获得你正在寻找的位置值。

下面是您应该研究的api

gettransform (long currentTime, transform outTransformation)

getMatrix ()

getValues (float[] values)

例如(一些伪代码。我还没有测试这个):

Transformation outTransformation = new Transformation();
myAnimation.getTransformation(currentTime, outTransformation);
Matrix transformationMatrix = outTransformation.getMatrix();
float[] matrixValues = new float[9];
transformationMatrix.getValues(matrixValues);
float transX = matrixValues[Matrix.MTRANS_X];
float transY = matrixValues[Matrix.MTRANS_Y];

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

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

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

 inwardAnimationSet.removeAllListeners()
        inwardAnimationSet.cancel()