Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate ProgressBar update in Android

I am using a ProgressBar in my application which I update in onProgressUpdate of an AsyncTask. So far so good.

What I want to do is to animate the progress update, so that it does not just "jump" to the value but smoothly moves to it.

I tried doing so running the following code:

this.runOnUiThread(new Runnable() {          @Override         public void run() {             while (progressBar.getProgress() < progress) {                 progressBar.incrementProgressBy(1);                 progressBar.invalidate();                 try {                     Thread.sleep(10);                 } catch (InterruptedException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();                 }             }         }      }); 

The problem is that the progress bar does not update its state until it finished its final value (progress variable). All states in between are not displayed on the screen. Calling progressBar.invalidate() didn't help either.

Any ideas? Thanks!

like image 239
user1033552 Avatar asked Nov 07 '11 11:11

user1033552


People also ask

What is ProgressBar in Android?

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.

How do I make my ProgressBar horizontal Android?

In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal. It mainly use the “android. widget. ProgressBar” class.

How do I stop indeterminate ProgressBar?

How do I stop indeterminate progress bar? An indeterminate progress bar animates constantly. You can stop the animation and clear the progress bar by making the progress bar determinate and setting the current value to the minimum.


2 Answers

I used android Animation for this:

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 call it like so:

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

Note: if from and to value are too low to produce smooth animation just multiply them by a 100 or so. If you do so, don't forget to multiply setMax(..) as well.

like image 123
Eli Konky Avatar answered Sep 24 '22 08:09

Eli Konky


I use an ObjectAnimator

private ProgressBar progreso; private ObjectAnimator progressAnimator; progreso = (ProgressBar)findViewById(R.id.progressbar1); progressAnimator = ObjectAnimator.ofInt(progreso, "progress", 0,1); progressAnimator.setDuration(7000); progressAnimator.start(); 
like image 41
kenshinsoto Avatar answered Sep 23 '22 08:09

kenshinsoto