Android custom animation interpolator - slow animation speed by half after half of it's duration has passed

952 views Asked by At

I've applied animation on a ProgressBar such that it animates from max progress to 0 in a constant pace (by default)

I need it to behave like this:

  • Animate from 100% progress to 50% progress in 5 seconds.
  • Animate from 50% progress to 0% progress in 10 seconds

Basically I want the animation to slow it's speed by half once it reaches 50% progress but I want this transition to be smooth.

I tried to run 2 different animations one after another, once the first animation ended in the animaton listener on end callback I was starting the next one with doubled duration, but there is a small annoying pause and it doesn't look continuous.

Can I define an interpolator that would handle this and what would the formula be?

This is my current code

private 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);
            points = (int)(value/100);
            txtProgress.setText(String.valueOf(points));

            setTextPosition(value);
        }

    }

And I am calling it like this:

anim = new ProgressBarAnimation(progressBar, progressBar.getMax(), 0);
        anim.setInterpolator(new ProgressInterpolator());
        anim.setDuration(10000);
        anim.setStartOffset(0);

progressBar.startAnimation(anim);
0

There are 0 answers