Skip to content

Instantly share code, notes, and snippets.

@westonal
Created February 26, 2017 01:06
Show Gist options
  • Select an option

  • Save westonal/03c81ea0718c8060b35a6d146d67da36 to your computer and use it in GitHub Desktop.

Select an option

Save westonal/03c81ea0718c8060b35a6d146d67da36 to your computer and use it in GitHub Desktop.
Auto reverse interpolator
import android.animation.TimeInterpolator;
import android.view.animation.LinearInterpolator;
/**
* Maps time into another interpolator so that the animation plays, and then reverses.
* i.e. time maps like so:
* [0..0.5, 0.5..1] maps to [0..1, 1..0]
*/
public final class AutoReverseInterpolator implements TimeInterpolator {
private final TimeInterpolator inner;
/**
* Decorator constructor, use if you don't want a linear interpolation.
* @param inner
*/
public AutoReverseInterpolator(TimeInterpolator inner) {
this.inner = inner;
}
/**
* Linear interpolation constructor.
*/
public AutoReverseInterpolator() {
this(new LinearInterpolator());
}
@Override
public float getInterpolation(float input) {
input *= 2;
if (input > 1) {
input = 2 - input;
}
return inner.getInterpolation(input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment