Jhaman Das
Jhaman Das

Reputation: 1114

Bouncing Animation in Android

i would like to create animation like below;

ANimation Bounce

I have used following code:

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

<scale
    android:duration="10000"
    android:fromXScale="1"
    android:fromYScale="0.5"
  />


</set>

But no able to work out! Any help could be appreciated.

Thanks

Upvotes: 2

Views: 4113

Answers (2)

Hardik Talaviya
Hardik Talaviya

Reputation: 1496

Try below code

No need any xml file in this code for the bounce animation as you want.

- Up Bouncing Animation

doBounceAnimation(yourView);


private void doBounceAnimation(View targetView) {
        Interpolator interpolator = new Interpolator() {
            @Override
            public float getInterpolation(float v) {
                return getPowOut(v,2);//Add getPowOut(v,3); for more up animation
            }
        };
        ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 25, 0);
        animator.setInterpolator(interpolator);
        animator.setStartDelay(200);
        animator.setDuration(800);
        animator.setRepeatCount(5);
        animator.start();
}

private float getPowOut(float elapsedTimeRate, double pow) {
        return (float) ((float) 1 - Math.pow(1 - elapsedTimeRate, pow));
}

- Down Bouncing Animation

doBounceAnimation(yourView);


private void doBounceAnimation(View targetView) {
        Interpolator interpolator = new Interpolator() {
            @Override
            public float getInterpolation(float v) {
                return getPowIn(v,2);//Add getPowIn(v,3); for more down animation
            }
        };
        ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 25, 0);
        animator.setInterpolator(interpolator);
        animator.setStartDelay(200);
        animator.setDuration(800);
        animator.setRepeatCount(5);
        animator.start();
}

private float getPowIn(float elapsedTimeRate, double pow) {
        return (float) Math.pow(elapsedTimeRate, pow);
}

I hope this can help you!

Thank You.

Upvotes: 7

Mona Baharlou
Mona Baharlou

Reputation: 1521

In Res/anim create this bounce_animation.xml file

<?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:fromXScale="1.0"
    android:fromYScale="0.0"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:duration="600" />
</set>

Then In your Activity class, set the animation to the element you want to tie it to. For example, when the user clicks in the TextView, it will bounce

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val bounceAnimation = AnimationUtils.loadAnimation(this, R.anim.bounce_animation)

    val textViewToBounce = findViewById(R.id.textViewToBounce)
    textViewToBounce.setOnClickListener {
        textViewToBounce.startAnimation(bounceAnimation)
    }
}

Upvotes: 0

Related Questions