【问题标题】:How to set animation to progress bar in Android如何在Android中将动画设置为进度条
【发布时间】:2019-04-03 07:40:27
【问题描述】:

我想创建 1 分钟的计时器进度条。我使用了以下代码,但它是从 60 到 0 开始的,而使用相同的程序我必须从 0 到 60 开始。

public static void animateTimerProgress(int currentTimeDuration, final int maxTimeDuration, boolean withStartDelay, final DonutProgress timeOutProgressView, Runnable endAction) {
    final int duration = currentTimeDuration < 0 ? 1 : currentTimeDuration;
    timeOutProgressView.setMax(maxTimeDuration);
    timeOutProgressView.setProgress(duration);
    timeOutProgressView.animate()
            .setDuration(duration * SECOND_IN_MILLIS)
            .setInterpolator(new LinearInterpolator())
            .setStartDelay(withStartDelay ? TIMEOUT_START_DELAY : 0)
            .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    int progress = 1 + (int) ((1f - valueAnimator.getAnimatedFraction()) * (duration - 1f));

                    timeOutProgressView.setProgress(progress);
                }
            })
            .withEndAction(endAction)
            .start();
}

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-animation android-progressbar


    【解决方案1】:

    你可以像这样使用ValueAnimator

    ValueAnimator animator = ValueAnimator.ofInt(0, 60);
    animator.setDuration(duration * SECOND_IN_MILLIS);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            timeOutProgressView.setProgress((int) animation.getAnimatedValue());
        }
    });
    animator.start();
    

    【讨论】:

      【解决方案2】:

      在 Kotlin 上,您可以使用 2 个扩展函数来帮助解决这个问题,在进度上加 1 加 1。这样,你可以有一个更流畅的动画:

      /**
       * ProgressBar Extensions
       */
      fun ProgressBar.setBigMax(max: Int) {
          this.max = max * 1000
      }
      
      fun ProgressBar.animateTo(progressTo: Int, startDelay: Long) {
          val animation = ObjectAnimator.ofInt(
              this,
              "progress",
              this.progress,
              progressTo * 1000
          )
          animation.duration = 500
          animation.interpolator = DecelerateInterpolator()
          animation.startDelay = startDelay
          animation.start()
      }
      

      使用方法:

       progress.setBigMax(10)
       progress.animateTo(10, 100)
      

      【讨论】:

        【解决方案3】:

        使用以下代码获取您的进度

        int progress = 1 + (int) (valueAnimator.getAnimatedFraction() * (duration - 1f));
        

        valueAnimator.getAnimatedFraction() 给出动画的分数值。您需要乘以最大进度/持续时间才能获得当前进度值。

        【讨论】:

        • 谢谢@nitinkumarp
        • 如果它帮助您解决问题,请标记为已接受 :)
        【解决方案4】:

        创建一个类

         public class Anim {
        
            public static void fadeOutProgressLayout(Activity act, final ViewGroup progressLayout) {
                act.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Animation fadeOut = new AlphaAnimation(1, 0);
                        fadeOut.setInterpolator(new AccelerateInterpolator());
                        fadeOut.setDuration(300);
        
                        fadeOut.setAnimationListener(new Animation.AnimationListener()
                        {
                            public void onAnimationEnd(Animation animation)
                            {
                                progressLayout.setVisibility(View.GONE);
                            }
                            public void onAnimationRepeat(Animation animation) {}
                            public void onAnimationStart(Animation animation) {}
                        });
        
                        progressLayout.startAnimation(fadeOut);
                    }
                });
            }
        }
        

        把这行写在你想要进度的地方

        Anim.fadeOutProgressLayout(GamblingVideosActivity.this, progressLayout);  
        

        【讨论】:

          猜你喜欢
          • 2015-03-23
          • 2016-02-24
          • 1970-01-01
          • 1970-01-01
          • 2021-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-30
          相关资源
          最近更新 更多