【问题标题】:Android Studio: Need help animating a progressbarAndroid Studio:需要帮助动画进度条
【发布时间】:2021-08-26 07:45:24
【问题描述】:

我有一个圆形进度条,由可绘制对象制成,值从 0 到 100(包括在内)。当我加载我的活动时,我想用从 0 到我想要的值的值来“填充”进度条的动画。我为此使用了CountdownTimer。 问题是,即使我将值设置为 100、最大值(或任何其他值),进度条也不会一直填满。这是我的代码:

  seekbar = (ProgressBar) findViewById(R.id.progressBar);
    seekbar.setMax(100);
    final int desiredvalue=100;


     CountDownTimer countDownTimer =
            new CountDownTimer(1500, 20) {
             int startingvalue=0;
                public void onTick(long millisUntilFinished) {
                    float progressPercentage = (desiredvalue*20)/1500;
                    seekbar.setProgress(startingvalue+=progressPercentage);

                }

                @Override
                public void onFinish() {

                }
            };
     countDownTimer.start();
}

这是正在发生的事情:Open me!

【问题讨论】:

    标签: java android android-studio user-interface progress-bar


    【解决方案1】:

    据我了解,您希望根据您的要求设置进度值。

    你可以使用ValueAnimator 类似这样的东西:

    progressBar = findViewById(R.id.my_pb);
    
            ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
            animation.setDuration(1000);
            animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator updatedAnimation) {
                    
                    Integer animatedValue = Math.round( (float)updatedAnimation.getAnimatedValue());
    
                    progressBar.setProgress(animatedValue);
                }
            });
            animation.start();
    

    更多关于ValueAnimator的详情

    【讨论】:

      【解决方案2】:

      我认为只需对您的代码稍作修改即可解决此问题。只需更改您的 onTick 代码,如下所示。

      seekbar = (ProgressBar) findViewById(R.id.progressBar);
      seekbar.setMax(100);
      final int desiredvalue=100;
      CountDownTimer countDownTimer = new CountDownTimer(1500, 20) {
          public void onTick(long millisUntilFinished) {
              float progressPercentage = (desiredvalue*(1500L-millisUntilFinished))/1500;
              seekbar.setProgress(progressPercentage);
          }
      
          @Override
          public void onFinish() {
              
          }
      };
      countDownTimer.start();
      

      【讨论】:

      • 谢谢!它有效,但我选择了其他解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多