Skip to content

Instantly share code, notes, and snippets.

@Kraiden
Last active December 30, 2020 03:31
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Kraiden/1390f1ac52a04e08b83e2d8b5a408aca to your computer and use it in GitHub Desktop.
Save Kraiden/1390f1ac52a04e08b83e2d8b5a408aca to your computer and use it in GitHub Desktop.
A more configurable bounce interpolator for Android animations
import android.view.animation.Interpolator;
import static java.lang.Math.*;
public class BetterBounceInterpolator implements Interpolator {
private int mBounces;
private double mEnergy;
/** Have more control over how to bounce your values.
*
*/
public BetterBounceInterpolator(){
this(3);
}
/** Have more control over how to bounce your values.
*
* @param bounces number of times to bounce before coming to a rest
*/
public BetterBounceInterpolator(int bounces){
this(bounces, 0.3f);
}
/** Have more control over how to bounce your values.
*
* @param bounces number of times to bounce before coming to a rest
* @param energyFactor control how the bounce loses momentum. 0 is lose energy linearly. 1 energy loss slows down over time, -1 energy loss speeds up over time. Values greater than 1 and less than -1 cause over/under bounce
*/
public BetterBounceInterpolator(int bounces, double energyFactor){
mBounces = bounces;
mEnergy = energyFactor + 0.5;
}
@Override public float getInterpolation(float x) {
return (float) (1d + (-abs(cos(x * 10 * mBounces/PI)) * getCurveAdjustment(x)));
}
private double getCurveAdjustment(double x){
return -(2 * (1 - x) * x * mEnergy + x * x) + 1;
}
}
@Cherobin
Copy link

Cherobin commented Apr 3, 2019

nice!

@Sanders248
Copy link

Thanks, work perfecly!

in kotlin if somone need

 inner class BetterBounceInterpolator(val bounces: Int, val energy: Double) : Interpolator {
       override fun getInterpolation(x: Float): Float = (1.0 + (-abs(cos(x * 10 * bounces / Math.PI)) * getCurveAdjustment(x))).toFloat()

       private fun getCurveAdjustment(x: Float) : Double = -(2 * (1 - x) * x * energy + x * x) + 1
   }

@nininea1
Copy link

nininea1 commented Jul 8, 2019

I have friction - 10 and tension - 400 in principle << how to transform those parameters into those ones? :(

@kuwapa
Copy link

kuwapa commented Jul 23, 2019

Awesome. I have an idea. How about you add an option to start from the bottom. This animation assumes that the view object is being dropped from the top. An option for the view object to start from rest, start bouncing and then go back to rest would be awesome.

@Kraiden
Copy link
Author

Kraiden commented Jul 25, 2019

@nininea1 I don't think you can really do that. The parameters basically just tweak a sine wave. I'm not sure you can convert real world values

@abhiank Here you go. Disclaimer: I haven't actually tried this for an animation. interpolators are supposed to be from 0 - 1. This goes from 0 - 1 - 0, meaning you'll need to play with the value you're animating a bit. I suspect you'll need to set the animated value yourself at the end of the animation. Eg: if you're bouncing x from 0 - 5, you'll need to manually set x back to 0 at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment