首页 文章

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

提问于
浏览
207

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

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

7 回答

  • 447

    在你叫 startAnimation()View 上调用 clearAnimation() .

  • 3

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

    这是我在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);
    
  • 0

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

    v.getAnimation().cancel();
    v.clearAnimation();
    animation.setAnimationListener(null);
    
  • 4

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

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

    您可以尝试做的是在停止之前从动画中获取变换矩阵并检查Matrix内容以获取您要查找的位置值 .

    以下是你应该研究的api

    public boolean getTransformation (long currentTime, Transformation outTransformation)

    public Matrix getMatrix ()

    public void 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];
    
  • 0

    使用方法 setAnimation(null) 来停止动画,它在 View.java 中作为公共方法公开,它是所有 widgets 的基类,用于创建交互式UI组件(按钮,文本字段等) . /** * 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)

  • 35

    要停止动画,您可以设置不执行任何操作的objectAnimator,例如

    首先当手动翻转时,动画从左到右:

    flipper.setInAnimation(leftIn);
    flipper.setOutAnimation(rightOut);
    

    然后当切换到自动翻转时,没有动画

    flipper.setInAnimation(doNothing);
    flipper.setOutAnimation(doNothing);
    
    doNothing = ObjectAnimator.ofFloat(flipper, "x", 0f, 0f).setDuration(flipperSwipingDuration);
    

相关问题