Skip to content

Instantly share code, notes, and snippets.

@chris95x8
Created December 12, 2014 13:59
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save chris95x8/4d74591bed75fd151799 to your computer and use it in GitHub Desktop.
Save chris95x8/4d74591bed75fd151799 to your computer and use it in GitHub Desktop.
Bunch of interpolators for awesome animations in Android!
import android.graphics.PointF;
import android.view.animation.Interpolator;
/**
* From https://github.com/codesoup/android-cubic-bezier-interpolator
* Derived from: https://github.com/rdallasgray/bez
*/
public class CubicBezierInterpolator implements Interpolator {
protected PointF start;
protected PointF end;
protected PointF a = new PointF();
protected PointF b = new PointF();
protected PointF c = new PointF();
public CubicBezierInterpolator(PointF start, PointF end) throws IllegalArgumentException {
if (start.x < 0 || start.x > 1) {
throw new IllegalArgumentException("startX value must be in the range [0, 1]");
}
if (end.x < 0 || end.x > 1) {
throw new IllegalArgumentException("endX value must be in the range [0, 1]");
}
this.start = start;
this.end = end;
}
public CubicBezierInterpolator(float startX, float startY, float endX, float endY) {
this(new PointF(startX, startY), new PointF(endX, endY));
}
public CubicBezierInterpolator(double startX, double startY, double endX, double endY) {
this((float) startX, (float) startY, (float) endX, (float) endY);
}
@Override
public float getInterpolation(float time) {
return getBezierCoordinateY(getXForTime(time));
}
protected float getBezierCoordinateY(float time) {
c.y = 3 * start.y;
b.y = 3 * (end.y - start.y) - c.y;
a.y = 1 - c.y - b.y;
return time * (c.y + time * (b.y + time * a.y));
}
protected float getXForTime(float time) {
float x = time;
float z;
for (int i = 1; i < 14; i++) {
z = getBezierCoordinateX(x) - time;
if (Math.abs(z) < 1e-3) {
break;
}
x -= z / getXDerivate(x);
}
return x;
}
private float getXDerivate(float t) {
return c.x + t * (2 * b.x + 3 * a.x * t);
}
private float getBezierCoordinateX(float time) {
c.x = 3 * start.x;
b.x = 3 * (end.x - start.x) - c.x;
a.x = 1 - c.x - b.x;
return time * (c.x + time * (b.x + time * a.x));
}
}
public static Interpolator getFastInSlowOutInterpolator(){
//Ease In Out Interpolator - For elements that change position while staying in the screen
return new CubicBezierInterpolator(0.4, 0, 0.2, 1);
}
public static Interpolator getLinearOutSlowInInterpolator(){
//Decelerate Interpolator - For elements that enter the screen
return new CubicBezierInterpolator(0, 0, 0.2, 1);
}
public static Interpolator getFastOutLinearInInterpolator(){
//Accelerate Interpolator - For elements that exit the screen
return new CubicBezierInterpolator(0.4, 0, 1, 1);
}
//Usage
ObjectAnimator anim = ObjectAnimator.ofFloat(ball1, View.TRANSLATION_X, 0, 800);
anim.setDuration(750);
anim.setInterpolator(getFastInSlowOutInterpolator());
anim.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment