Make a bounce animation

I would like to make a layer bounce animation.

I made this layer, starting from the right to the center, now I would like to move it back a bit and then return to the center. This will create a bounce effect.

I thought I could do this with a translation like this:

<translate
    android:duration="900"
    android:fromXDelta="100%p"
    android:toXDelta="0%p" />

<translate
    android:duration="900"
    android:fromXDelta="0%p"
    android:toXDelta="100%p" />

<translate
    android:duration="900"
    android:fromXDelta="70%p"
    android:toXDelta="0%p" />

Well, this code doesn't work, the only thing I can achieve is that Layer happens to the left of the center and then the animation stops.

I can not use this code: because it does not achieve what I want

setInterpolator(AnimationUtils.loadInterpolator(this,
                        android.R.anim.bounce_interpolator));

Any help would be appreciated.

+6
source share
3 answers

BounceInterpolator, . docs , XML. xml :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/bounce_interpolator">

    <!-- Use your working translate animation here-->
    <translate
        android:duration="900"
        android:fromXDelta="100%p"
        android:toXDelta="0%p" />
</set>
+9

XML

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/bounce_interpolator">

    <scale
        android:duration="600"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

, , ( ), ..

+3

Add code to button or image click

    final Animation myAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.bounce);
    // Use bounce interpolator with amplitude 0.1 and frequency 15
    MyBounceInterpolator interpolator = new MyBounceInterpolator(0.1, 15);
    myAnim.setInterpolator(interpolator);
    imgVoiceSearch.startAnimation(myAnim);

Add this class

public class MyBounceInterpolator implements android.view.animation.Interpolator {
private double mAmplitude = 1;
private double mFrequency = 10;

public 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);
}
}
0
source

Source: https://habr.com/ru/post/1542512/


All Articles