Smooth Progress Bar Animation

前端 未结 9 793
鱼传尺愫
鱼传尺愫 2021-01-01 10:34

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 wi

相关标签:
9条回答
  • 2021-01-01 10:38

    here is snippet for my countdown timer using smooth animation you can modify as per your need please follow below :

    private void setProgressBarValues() {
            progressBarCircle.setMax((int) (timeCountInMilliSeconds / 10));
            progressBarCircle.setProgress((int) (timeCountInMilliSeconds / 10));
            Log.e("progres", "" + (timeCountInMilliSeconds / 10));
        }
    
    private void startCountDownTimer() {
    
            smoothAnimation = ObjectAnimator.ofInt(progressBarCircle, "progress", progressBarCircle.getProgress(), progressBarCircle.getMax());
            smoothAnimation.setDuration(500);
            smoothAnimation.setInterpolator(new AccelerateInterpolator());
    
            countDownTimer = new CountDownTimer(timeCountInMilliSeconds, 10) {
                @Override
                public void onTick(long millisUntilFinished) {
    
                    Log.e("getMax", "" + progressBarCircle.getMax());
                    Log.e("getprogres", "" + progressBarCircle.getProgress());
                    textViewTime.setText(hmsTimeFormatter(millisUntilFinished));
                    progressBarCircle.setProgress((int) (timeCountInMilliSeconds / 10 - millisUntilFinished / 10));
                }
    
                @Override
                public void onFinish() {
    
                    textViewTime.setText(hmsTimeFormatter(timeCountInMilliSeconds));
                    // call to initialize the progress bar values
                    setProgressBarValues();
                    // hiding the reset icon
                    buttonReset.setEnabled(false);
                    // making edit text editable
                    editTextMinute.setEnabled(true);
                    // changing the timer status to stopped
                    status = TimerStatus.STOPPED;
                    smoothAnimation.end();
                }
    
            }.start();
            smoothAnimation.start();
            countDownTimer.start();
        }
    

    Summary:

    1. setMax and setProgress

    2. setAnimation to show from progess to max values of progressbar

    3. create a timer with call back of ~10 millisec

    4. update progress in onTick i.e total - finished

    0 讨论(0)
  • 2021-01-01 10:40

    If you have Android N and above, you can use :

    progressBar.setProgress(newProgress, true)
    

    Docs:

    Sets the current progress to the specified value, optionally animating the visual position between the current and target values.

    Animation does not affect the result of getProgress(), which will return the target value immediately after this method is called.

    https://developer.android.com/reference/android/widget/ProgressBar.html#setProgress(int,%20boolean)

    0 讨论(0)
  • 2021-01-01 10:44

    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

    0 讨论(0)
  • 2021-01-01 10:48

    You can make custom clas like this :

    public class CustomProgressBar extends ProgressBar {
    private static final long DEFAULT_DELAY = 500;
    private static final long DEFAULT_DURATION = 1000;
    
    public CustomProgressBar(Context context) {
        super(context);
    }
    
    public CustomProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    public synchronized void setProgress(float progress) {
        super.setProgress((int) progress);
    }
    
    @Override
    public synchronized void setProgress(int progress) {
        super.setProgress(progress);
    }
    
    public void startLcpProgressAnim(int progressTo) {
       startLcpProgressAnim(DEFAULT_DELAY, progressTo);
    }
    
    public void startLcpProgressAnim(long delay, int progressTo) {
       startLcpProgressAnim(DEFAULT_DURATION, delay, progressTo);
    }
    
    public void startLcpProgressAnim(long duration, long delay, float progressTo) {
        ObjectAnimator animation = ObjectAnimator.
                ofFloat(this, "progress",
                        (float)this.getProgress(), progressTo);
        animation.setDuration(duration);
        animation.setStartDelay(delay);
        animation.start();
    }
    }
    
    0 讨论(0)
  • 2021-01-01 10:51

    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

      <com.carlosmuvi.segmentedprogressbar.SegmentedProgressBar
          android:id="@+id/segmented_progressbar"
          android:layout_width="match_parent"
          android:layout_height="5dp"/>
    

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

    GoToLibrary

    0 讨论(0)
  • 2021-01-01 10:54

    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();
        }
    
    0 讨论(0)
提交回复
热议问题