1) Add a button view
We begin by placing a button in the activity layout file res/layout/activity_main.xml.
<Button android:id=”@+id/button” android:layout_width=”92dp” android:layout_height=”92dp” android:onClick=”didTapButton” android:layout_centerHorizontal=”true” android:background=”#FFA400″ />
</RelativeLayout>
2) Create a scale animation
Next, we create an animation file res/anim/bounce.xml for scaling views.
- Right click on the res folder.
- Select New / Android resource file.
- Write bounce as the name of the file
- Choose the Animation resource type. The directory name field will change to anim.
Next, open the res/anim/bounce.xml file that was created for you and replace its content with the following code.
<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>
This code creates an animation that changes the size of a view from 30% to 100% during two seconds.
3) React to button tap
Now we add the code that animates the button on tap. Add the following method to your activity Java file.
1 2 3 4 5 6 7 8 |
[box] public void didTapButton(View view) { Button button = (Button)findViewById(R.id.button); final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce); button.startAnimation(myAnim); } [/box] |
If you run the app and tap the button it will animate smoothly from smaller to bigger size.
Thanks For Animation