【问题标题】:How to stop an animation (cancel() does not work)如何停止动画(cancel() 不起作用)
【发布时间】:2011-05-05 23:37:43
【问题描述】:

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

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

【问题讨论】:

    标签: android animation


    【解决方案1】:

    拨打clearAnimation() 拨打您拨打startAnimation() 的任何View

    【讨论】:

    • 但实际上我需要一些更复杂的东西:当动画停止时,它必须保持在当前位置,即我有滑动图像并且在触摸事件时它必须冻结在这个位置。 clearAnimation() 不是这种情况,因为它根据 setFillAfter() 将状态重置为开始/结束位置
    • @user349871: setFillAfter() 可能不会像您认为的那样做,无论如何。当触摸事件发生时,清除动画,然后调整布局以影响您寻求的任何永久更改。
    • 好的,好的,我将尝试通过调用 clearAnimation() 来停止动画。下一步是在我停止动画之前找到动画的位置。什么 API 用于此?
    • @Mix:没有 API。
    • 您好!当动画被取消时,我找到了一种方法来获取点(根据插值时间)。您需要创建 Animation 子类并从 android 代码动画中放入代码(例如 TranslateAnimation)。在您的课程中,您将能够在 applyTransformation() 函数中保存和跟踪位置。
    【解决方案2】:

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

    这里是你应该研究的 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];
    

    【讨论】:

    • 我们从哪里得到 currentTime?
    • 哦,文档说它对应于“挂钟”,在其他地方他们将挂钟定义为 System.currentTimeMillis(),它对应于日历时间,只要时间改变就会跳转用户或网络。一个奇怪的措施正在使用。
    【解决方案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);
    

    【讨论】:

    • .setListener(null); 这对我有帮助。
    【解决方案4】:

    如果您使用动画侦听器,请设置v.setAnimationListener(null)。将以下代码与所有选项一起使用。

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

    【讨论】:

    • 不需要调用v.setAnimation(null);,因为这将在v.clearAnimation(); 中完成。
    • 正如@Christian 所说,v.setAnimation(null) 不是必需的
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      使用方法 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)

      【讨论】:

        【解决方案7】:

        要停止动画,你可以设置这样的 objectAnimator 什么都不做,例如

        首先手动翻转时有从左到右的动画:

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

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

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

        【讨论】:

          【解决方案8】:

          首先,删除所有与动画师或动画师集相关的侦听器。然后取消动画师或动画师集。

           inwardAnimationSet.removeAllListeners()
                  inwardAnimationSet.cancel()
          

          【讨论】:

            【解决方案9】:

            这样使用:

            // start animation
            TranslateAnimation anim = new TranslateAnimation( 0, 100 , 0, 100);
            anim.setDuration(1000);
            anim.setFillAfter( true );
            view.startAnimation(anim);
            
            // end animation or cancel that
            view.getAnimation().cancel();
            view.clearAnimation();
            

            取消()

            取消动画。取消动画会调用动画 如果设置了监听器,则通知动画结束。 如果手动取消动画,则必须调用 reset() 在重新开始动画之前。


            clearAnimation()

            取消此视图的所有动画。


            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-10-16
              • 1970-01-01
              • 2017-03-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-01-13
              相关资源
              最近更新 更多