Skip to content

Instantly share code, notes, and snippets.

@baleen37
Last active January 26, 2017 04:50
Show Gist options
  • Save baleen37/21e095675e1e4c3c149d3b7db0036036 to your computer and use it in GitHub Desktop.
Save baleen37/21e095675e1e4c3c149d3b7db0036036 to your computer and use it in GitHub Desktop.
public class AnimatableProgressBar extends ProgressBar {
private ProgressBarAnimation mAnimation;
public AnimatableProgressBar(Context context) {
super(context);
init(context, null);
}
public AnimatableProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public AnimatableProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public void setProgress(int progress, boolean animate) {
if (animate) {
mAnimation.setProgress(progress);
} else {
super.setProgress(progress);
}
}
private void init(Context context, AttributeSet attrs) {
mAnimation = new ProgressBarAnimation(this, 2000);
}
public class ProgressBarAnimation extends Animation {
private ProgressBar mProgressBar;
private int mTo;
private int mFrom;
private final long mStepDuration;
public ProgressBarAnimation(ProgressBar progressbar, long fullDuration) {
super();
Utils.checkNull(progressbar);
mProgressBar = progressbar;
mStepDuration = fullDuration / progressbar.getMax();
}
public void setProgress(int progress) {
if (progress < 0) {
progress = 0;
} else 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment