1) Implement the bounce interpolator
Next, we write the code that adds the bounce effect to the scale animation.
- Create a new Java Class file in your app module and name it MyBounceInterpolator.
- Open the Java file that was created and replace the class code with the following.
class MyBounceInterpolator implements android.view.animation.Interpolator {
double mAmplitude = 1;
double mFrequency = 10;
MyBounceInterpolator(double amplitude, double frequency) { mAmplitude = amplitude; mFrequency = frequency; }
public float getInterpolation(float time) { return (float) (-1 * Math.pow(Math.E, -time/ mAmplitude) * Math.cos(mFrequency * time) + 1); } }
I will explain how this code works in a moment.
2) Use the bounce interpolator
Finally, open your activity Java file again and replace the entire didTapButton
method with the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[box] public void didTapButton(View view) { Button button = (Button)findViewById(R.id.button); final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce); // Use bounce interpolator with amplitude 0.2 and frequency 20 MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20); myAnim.setInterpolator(interpolator); button.startAnimation(myAnim); } [/box] |
Recent Comments