【问题标题】:How do I stop an infinite animation on an ImageView, but let it complete its current cycle?如何停止 ImageView 上的无限动画,但让它完成当前循环?
【发布时间】:2017-05-13 23:38:10
【问题描述】:

我在 ImageView 上应用了无限动画来指示我的应用程序中正在运行的后台线程。当线程完成时,我可以使用 clearAnimation() 停止动画,但它会将 ImageView 捕捉回其起始位置,并且我希望当前动画周期完成(旨在优雅地结束其起始位置)。有没有办法做到这一点?

【问题讨论】:

    标签: android


    【解决方案1】:

    注册一个AnimationListener,然后等到onAnimationRepeat() 清除它。我还没有尝试过,但我认为它会起作用。

    【讨论】:

    • Drat,除了没有调用 onAnimationRepeat() 之外,这本来是完美的,而且它似乎是一个长期存在的错误:code.google.com/p/android/issues/detail?id=13397(onAnimationStart 有效,onAnimationEnd 如果我设置动画的 repeatCount 到一个有限的数字)
    • @wirbly:好吧,您可以尝试滚动您自己的“无限”,通过制作单个动画,然后在 onAnimationEnd() 上再次启动动画。不过,不确定它是否会像无限的多样性一样流畅。但是,如果这行得通,您就不会在您希望它消失的动画结尾处开始下一个动画。
    【解决方案2】:

    只需拨打setRepeatCount(0),然后收听onAnimationEnd。详情请见here

    【讨论】:

    • 简单地将重复计数设置为零对我有用。我没有听 onAnimationEnd,但它似乎工作正常。
    • @Henry 我的用例有点复杂(一个刷新时动画的刷新按钮),所以我需要 onAnimationEnd 来隐藏动画进度指示器,并用按钮替换它。此外,还需要一些变通方法以使过渡非常顺利。有关详细信息,请参阅我链接的答案。仅当您需要知道动画何时真正停止时才需要 onAnimationEnd,因为当您调用 setRepeatCount(0) 时它不会停止,它首先完成当前循环(这就是重点)
    【解决方案3】:

    您可以在动画侦听器的 onAnimationEnd() 方法中设置动画的理想位置。然后,视图将显示在您在那里设置的坐标处,而不是转到初始位置。

    animation = new TranslateAnimation(0, -length, 0, 0); //setup here your translation parameters
    animation.setInterpolator(new LinearInterpolator());
    animation.setDuration(10);
    animation.setAnimationListener(new Animation.AnimationListener() {
        public void onAnimationStart(Animation animation) {
            animationRunning = true;
        }
        public void onAnimationRepeat(Animation animation) {
        }
        public void onAnimationEnd(Animation animation) {
            animationRunning = false;
            // setAnimationPositionAfterPositionChange();
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) runningText.getLayoutParams();
            params.leftMargin  = currentPosition; //Calculate you current position here
            runningText.setLayoutParams(params);
        }
    });
    runningText.setAnimation(animation);
    

    【讨论】:

      【解决方案4】:

      如果您使用的是 animate() 你使用 setListener(null) 来停止它的动画, 对我有用。

          image.animate()
                  .translationXBy(-width* 0.5f)
                  .setDuration(300)
                  .setStartDelay(6000)
                  .setListener(new AnimatorListenerAdapter() {
                      @Override
                      public void onAnimationEnd(Animator animation) {
                          image.animate().rotationBy(90).setDuration(1000).setStartDelay(1).setListener(null);
                      }
                  });
      

      【讨论】:

        猜你喜欢
        • 2012-10-15
        • 1970-01-01
        • 2017-01-02
        • 2011-04-24
        • 2014-12-31
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        相关资源
        最近更新 更多