Update ProgressBar Update

I use a progress bar (as a bar). I want the bar to grow and change smoothly with an interpolator, but it does not work. 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?

+48
java android android-layout
May 23 '11 at 13:23
source share
8 answers

The interpolator should be attached to the animation, and this will only work on cells 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 DecelerateInterpolator, but this is optional, and there are other possibilities.

+110
Apr 14 '13 at 17:37
source share

Here is a self-sufficient 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 also change the type of AnimatingProgressBar to use setAnimate() to turn off the animation (may be useful when restoring an activity state)

+16
Dec 22 '13 at 9:52
source share

If you change the value of progress each time by 1 (for example, from 45 to 46), you will not see the animation. Itโ€™s better to change the course by 100 points, for this you just need to multiply the maximum value by 100 and each progress value to 100. 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(); } 
+9
Jul 6 '16 at 9:55
source share

I developed how to do this using runnable, I was able to update the progress bar several times per second and thus give a sliding effect. Code below:

 private Runnable SmoothIncrement = new Runnable() { public void run() { final long start = mStartTime; long millis = SystemClock.uptimeMillis() - start; if(track!=increase) { if((pb.getProgress()==100)&&(count<target)) { pb.setProgress(0); } pb.incrementProgressBy(1); track++; incrementor.postAtTime(this, start + millis); } else { incrementor.removeCallbacks(this); } } }; 

Here, "track" keeps track of how many increments have been done, and the increase is the total number of steps that must be taken. I can dynamically increase the number of increments from the user interface thread to provide a smooth effect. The code only works for progress indicators that do not need to be reduced.

To run it, just use this code:

  mStartTime = System.currentTimeMillis(); incrementor.removeCallbacks(SmoothIncrement); if(track!=0) { track -= increase; } incrementor.postDelayed(SmoothIncrement, 0); 
+3
May 24 '11 at 11:58 a.m.
source share

My Link Library

Above the link is my library. Just use it if you want.

+2
Dec 16 '15 at 7:24
source share

I'm not sure, but please check it out:

 pb.setProgress(pb.getProgress() * 100); 
+1
May 23 '11 at 13:29
source share

According to the documentation, the interpolator is applied to uncertain progress. Since you are setting progress, I think you intend to use normal with values. I think it would be best for you to increase the maximum value of progress and go in smaller increments.

+1
May 23 '11 at 13:45
source share

I used animation for Android:

 public class ProgressBarAnimation extends Animation{ private ProgressBar progressBar; private float from; private float to; public ProgressBarAnimation(ProgressBar progressBar, float from, float to) { super(); this.progressBar = progressBar; this.from = from; this.to = to; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); float value = from + (to - from) * interpolatedTime; progressBar.setProgress((int) value); } } 

and name it like this:

 ProgressBarAnimation anim = new ProgressBarAnimation(progress, from, to); anim.setDuration(1000); progress.startAnimation(anim); 

Note: if the from and for values โ€‹โ€‹are too low to create a smooth animation, just multiply them by 100 or so. If you do, be sure to also multiply setMax (..).

+1
Dec 19 '16 at 6:25
source share



All Articles