Step 1:
res/layout/Animation.xml
res/layout/Animation.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.evgenii.sixbouncingbuttons.MainActivity">
<Button
android:id="@+id/button_animate"
android:layout_width="100dp"
android:layout_height="40dp"
android:onClick="didTapButton"
android:background="#FFA400" />
</android.support.constraint.ConstraintLayout>
Step 2:
res/anim/animation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="2000" android:fromXScale="0.3" android:toXScale="1.0" android:fromYScale="0.3" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" /> </set>
step 3:
following method use in your activity
method 1:
public void didTapButton(View view) { Button button = (Button)findViewById(R.id.button); final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce); button.startAnimation(myAnim); }
method 2:
class MyAnimation implements android.view.animation.Interpolator { private double mAmplitude = 1; private double mFrequency = 10; MyAnimation (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); } }
method 3:
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); }