Smooth Progress Bar Animation

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

I'm trying to implement a smooth animation for my ProgressBar, but when I increase the time (30 seconds), the animation is no longer smooth.

Example with 5 seconds:

Example with 30 seconds:

My progress background:

                                                                                                                                                                                                                                                                                     

My progress layout:

My animation method:

private void startAnimation(){     ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.pb_loading);     ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 100, 0);     progressAnimator.setDuration(30000);     progressAnimator.setInterpolator(new LinearInterpolator());     progressAnimator.start(); }

回答1:

Because you are using ofInt you can only move at full integers. In other words, if you have a progress bar with a width of 1000 and a progress of 0 to 100 since you are moving at an integer pace you count 1, 2, 3, 4 which translates to 10px, 20px, 30px and 40px. Which explains the jaggedness you are seeing.

To correct this you have a few options. The first is to up your integers from 0 to someBigInt This will give the animator more numbers to work with.

ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 10000, 0);

The other option is to use ofFloat which does the same thing as ofInt but uses floating points instead of integers.

ObjectAnimator progressAnimator = ObjectAnimator.ofFloat(mProgressBar, "progress", 100.0, 0.0);


回答2:

If you change progress value each time by 1 (for example from 45 to 46) you won't see the animation. You'd better change progress by 100 points (or maybe other), for this you just need to multiply your max value by 100 and each progress value to 100 too. For example:

    private void setProgressMax(ProgressBar pb, int max) {         pb.setMax(max * 100);     }      private void setProgressAnimate(ProgressBar pb, int progressTo)      {         ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", pb.getProgress(), progressTo * 100);         animation.setDuration(500);         animation.setInterpolator(new DecelerateInterpolator());         animation.start();     }


回答3:

Just set android:max="1000" and do ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 1000, 0);

in this case you will animate on 1/1000 by each step which in 10 time smoothly when default 100 percent scale. and it looks much better



回答4:

You are using an integer when you're animating. This means that it will jump between whole values. When it moves fast, that's fine but when you move slower it looks like it jumps to much.

try changing ObjectAnimator.ofInt to ObjectAnimator.ofFloat

hope that helps :)



回答5:

You can not use the ofFloat because the ProgressBar's progress attribute doesn't accept float values, only integer ones. That is why your ProgressBar stopped progressing after going with that solution.

As the others have said it, the correct way to do what you want is to set android:max to some big integer.

Sorry for reviving the thread but I feel like this had to be said.



回答6:

Use the library may its help you give the time in which you want to fill the progress bar its very smooth with no lag but you little bit customize to use this.

Just add the dependency to your build.gradle:

compile 'com.carlosmuvi.segmentedprogressbar:library:0.2'

Next, add it to your layout

  

Finally, customize it programatically and play it!

segmentedProgressBar = (SegmentedProgressBar) findViewById(R.id.segmented_progressbar);  // number of segments in your bar segmentedProgressBar.setSegmentCount(7);   //empty segment color segmentedProgressBar.setContainerColor(Color.BLUE);  //fill segment color segmentedProgressBar.setFillColor(Color.GREEN);   //play next segment specifying its duration segmentedProgressBar.playSegment(5000);  //pause segment segmentedProgressBar.pause();  //set filled segments directly segmentedProgressBar.setCompletedSegments(3);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!