Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a ProgressBar update smoothly

I'm using a progress bar (in bar form). I wish to make the bar increase and decrease smoothly using an interpolator, but It's not working. This is what I have at the moment:

pb.setInterpolator(main.this, android.R.anim.bounce_interpolator);              pb.setProgress(pb.getProgress()+10); 

Am I doing something really wrong?

like image 423
Todd Davies Avatar asked May 23 '11 13:05

Todd Davies


People also ask

How do I make my progress bar smooth in C#?

Create a custom ProgressBar controlStart Microsoft Visual Studio. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual C# under Project Types, and then click Windows Forms Control Library under Templates.

What is a difference between SeekBar and ProgressBar in Android?

In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.


2 Answers

The Interpolator has to be attached to an animation and this will work only on Honeycomb or higher:

if(android.os.Build.VERSION.SDK_INT >= 11){     // will update the "progress" propriety of seekbar until it reaches progress     ObjectAnimator animation = ObjectAnimator.ofInt(seekbar, "progress", progress);      animation.setDuration(500); // 0.5 second     animation.setInterpolator(new DecelerateInterpolator());     animation.start(); } else      seekbar.setProgress(progress); // no animation on Gingerbread or lower 

If your minimum SDK is Gingerbread or lower, add:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)  // or  @SuppressLint("NewApi")  

to your function/class.

I used a DecelerateInterpolator, but this is optional and there are others possibilities.

like image 107
now Avatar answered Sep 30 '22 10:09

now


Here is a self-contained drop in solution:

import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.content.Context; import android.util.AttributeSet; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Interpolator; import android.widget.ProgressBar;  public class AnimatingProgressBar extends ProgressBar {      private static final Interpolator DEFAULT_INTERPOLATER = new AccelerateDecelerateInterpolator();      private ValueAnimator animator;     private ValueAnimator animatorSecondary;     private boolean animate = true;      public AnimatingProgressBar(Context context, AttributeSet attrs, int defStyle) {         super(context, attrs, defStyle);     }      public AnimatingProgressBar(Context context, AttributeSet attrs) {         super(context, attrs);     }      public AnimatingProgressBar(Context context) {         super(context);     }      public boolean isAnimate() {         return animate;     }      public void setAnimate(boolean animate) {         this.animate = animate;     }      @Override     public synchronized void setProgress(int progress) {         if (!animate) {             super.setProgress(progress);             return;         }         if (animator != null)             animator.cancel();         if (animator == null) {             animator = ValueAnimator.ofInt(getProgress(), progress);             animator.setInterpolator(DEFAULT_INTERPOLATER);             animator.addUpdateListener(new AnimatorUpdateListener() {                  @Override                 public void onAnimationUpdate(ValueAnimator animation) {                     AnimatingProgressBar.super.setProgress((Integer) animation.getAnimatedValue());                 }             });         } else             animator.setIntValues(getProgress(), progress);         animator.start();      }      @Override     public synchronized void setSecondaryProgress(int secondaryProgress) {         if (!animate) {             super.setSecondaryProgress(secondaryProgress);             return;         }         if (animatorSecondary != null)             animatorSecondary.cancel();         if (animatorSecondary == null) {             animatorSecondary = ValueAnimator.ofInt(getProgress(), secondaryProgress);             animatorSecondary.setInterpolator(DEFAULT_INTERPOLATER);             animatorSecondary.addUpdateListener(new AnimatorUpdateListener() {                  @Override                 public void onAnimationUpdate(ValueAnimator animation) {                     AnimatingProgressBar.super.setSecondaryProgress((Integer) animation                             .getAnimatedValue());                 }             });         } else             animatorSecondary.setIntValues(getProgress(), secondaryProgress);         animatorSecondary.start();     }      @Override     protected void onDetachedFromWindow() {         super.onDetachedFromWindow();         if (animator != null)             animator.cancel();         if (animatorSecondary != null)             animatorSecondary.cancel();     }  } 

replace ProgressBar with AnimatingProgressBar in your layout

You many also change the type to AnimatingProgressBar to utilize setAnimate() to disable animation (could be useful when restoring activity state)

like image 21
tom91136 Avatar answered Sep 30 '22 10:09

tom91136