0

I am using a horizontal progress bar and I am trying to animate the bar filling up ( I load the amount of progress before the bar shows up). This is what I'm doing so far but its not working:

progressBar = (ProgressBar)v.findViewById(R.id.rewards_progress);
Animation animation = new Animation() {
    @Override
    public void start() {
        progressBar.setInterpolator(new AccelerateDecelerateInterpolator());
        progressBar.setProgress(currentProgress);
    }
};
animation.setDuration(1000);
animation.start();

Any suggestions?

Allenktv
  • 93
  • 3
  • 6
  • You need to set the progress inside another thread that will keep runing and calling setProgress(currentProgress) with the updated value, if you don't how to do that I can post some code but I think all you need is that suggestion – GhostDerfel Mar 17 '14 at 20:40
  • In the same documentation of the ProgressBar class you have the answer for that. http://developer.android.com/reference/android/widget/ProgressBar.html – GoRoS Mar 17 '14 at 20:43
  • @GoRoS I've looked at the documentation but I'm not understanding which one I should implement. I've tried setting the interpolator and calling .start() but it still isn't working. – Allenktv Mar 17 '14 at 21:12

1 Answers1

0

If you just want to update the ProgressBar and as we said in previous comments, you need to do it in another thread, look at this dummy example based in the official Android doc:

package com.example.progressbar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private ProgressBar mProgress;
    private static int mProgressStatus = 0;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mProgress = (ProgressBar) findViewById(R.id.rewards_progress);

         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     doWork();
                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();
    }

    private void doWork(){
        try {
            Thread.sleep(100);
            mProgressStatus+=2;
        } catch (InterruptedException e) {
        }
    }

}

However, if you want to animate the ProgressBar update effect, there are several ways to achieve it. One particular way described in this post looks and works pretty good could be easily done by:

package com.example.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgress = (ProgressBar) findViewById(R.id.rewards_progress);

        ProgressBarAnimation anim = new ProgressBarAnimation(mProgress, 0, 100);
        anim.setDuration(5000);
        mProgress.startAnimation(anim);     
    }   

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

}

Note that you should customize ProgressBarAnimation's parameters from and to in order to apply it in a realistic approach.

Community
  • 1
  • 1
GoRoS
  • 5,183
  • 2
  • 43
  • 66