简体   繁体   English

Android Studio:需要帮助动画进度条

[英]Android Studio: Need help animating a progressbar

I have a round progress bar, made with a drawable, with values from 0-100 (included).我有一个圆形进度条,用可绘制的,值从 0-100(包括在内)。 When I load my activity, I want to animate the progressbar being "filled" with values from 0 to my desidered value.当我加载我的活动时,我想用从 0 到我想要的值的值来“填充”进度条的动画。 I'm using a CountdownTimer for that.我为此使用了CountdownTimer The issue is, even though I set the value to 100, the max (or any other value), the progressbar doesn't get filled all the way.问题是,即使我将值设置为 100、最大值(或任何其他值),进度条也不会一直填满。 Here's my code:这是我的代码:

  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();
}

This is what's happening: Open me!这就是正在发生的事情:打开我!

As i understood you want set progress value based on your requirement.据我了解,您希望根据您的要求设置进度值。

You can use ValueAnimator something like this:你可以像这样使用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();

For more Detail About ValueAnimator有关ValueAnimator的更多详细信息

I think just a small modification to your code will fix this.我认为只需对您的代码稍作修改即可解决此问题。 Just change your onTick code like the following.只需更改您的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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM