Skip to content

Instantly share code, notes, and snippets.

@s1rius
Created July 9, 2014 10:38
Show Gist options
  • Save s1rius/54e36d86db7d3e4ca8a6 to your computer and use it in GitHub Desktop.
Save s1rius/54e36d86db7d3e4ca8a6 to your computer and use it in GitHub Desktop.

Revisions

  1. s1rius created this gist Jul 9, 2014.
    42 changes: 42 additions & 0 deletions ProgressBarAnimation
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    import android.view.animation.Animation;
    import android.view.animation.Transformation;
    import android.widget.ProgressBar;

    public class ProgressBarAnimation extends Animation {
    private ProgressBar mProgressBar;
    private int mTo;
    private int mFrom;
    private long mStepDuration;

    /**
    * @param fullDuration - time required to fill progress from 0% to 100%
    */
    public ProgressBarAnimation(ProgressBar progressBar, long fullDuration) {
    super();
    mProgressBar = progressBar;
    mStepDuration = fullDuration / progressBar.getMax();
    }


    public void setProgress(int progress) {
    if (progress < 0) {
    progress = 0;
    }

    if (progress > mProgressBar.getMax()) {
    progress = mProgressBar.getMax();
    }

    mTo = progress;

    mFrom = mProgressBar.getProgress();
    setDuration(Math.abs(mTo - mFrom) * mStepDuration);
    mProgressBar.startAnimation(this);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
    float value = mFrom + (mTo - mFrom) * interpolatedTime;
    mProgressBar.setProgress((int) value);
    }
    }