Home » Android » android – Bounce button on tap

android – Bounce button on tap

Posted by: admin June 15, 2020 Leave a comment

Questions:

does anybody know is there any xml animation that enables button to bounce for a few seconds. If you can and have share some examples…

How to&Answers:

Here is a simple way to do it using an ObjectAnimator. If you want this to work pre-honeycomb, you can either use the same syntax and do it with a view animation, or use NineOldAndroids.

ObjectAnimator animY = ObjectAnimator.ofFloat(button, "translationY", -100f, 0f);
animY.setDuration(1000);//1sec
animY.setInterpolator(new BounceInterpolator());
animY.setRepeatCount(numRepeats);
animY.start();

Answer:

Have a look here for some sample animations using a ViewPropertyAnimator. If you want to use it on APIs bellow HoneyComb you should try using NineOldAndroids which is a port of the HoneyComb animations to lower APIs.

Answer:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/bounce_interpolator">

    <scale
        android:duration="500"
        android:fromXScale="0.5"
        android:toXScale="1.0"
        android:fromYScale="0.5"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>

Above animation code put in anim folder and set it to any view using java code:

Animation animation = AnimationUtils.loadAnimation(this,R.anim.bounce);
textview.startAnimation(animation);