Reputation: 5892
Android provides the following animation interpolators: AccelerateInterpolator, DecelerateInterpolator, AccelerateDecelerateInterpolator.
How can I achieve a DecelerateAccelerateInterpolator?
Upvotes: 2
Views: 428
Reputation: 13039
You want an interpolator that goes fast at the start and end but slow in the middle ?
Find the proper math function that represents your interpolator and implement the Interpolator interface with your math function.
Example from http://cogitolearning.co.uk/?p=1078
public class DecelerateAccelerateInterpolator implements Interpolator {
public DecelerateAccelerateInterpolator() {}
public float getInterpolation(float t) {
float x=2.0f*t-1.0f;
return 0.5f*(x*x*x + 1.0f);
}
}
Or combine a DecelerateInterpolator following by and AccelerateInterpolator.
public class DecelerateAccelerateInterpolator implements Interpolator {
final float factor;
public DecelerateAccelerateInterpolator() {
this(1);
}
public DecelerateAccelerateInterpolator(float factor) {
this.factor = factor;
}
public float getInterpolation(float t) {
if (t < 0.5f) {
return new DecelerateInterpolator(factor).getInterpolation(2 * t);
} else {
return new AccelerateInterpolator(factor).getInterpolation(2 * (t - 0.5f));
}
}
}
Upvotes: 4