In this Blog, we will learn about using some more simple animations in your android application.
For Checking what this blog is all about, please scroll to the end of the blog, there you will see a sample gif demo of animations covered in this blog.
Well, the content in this blog is in addition to my previous blog, so if you don’t get to understand something, please read this blog first.
In this blog, I have just added two more Simple Animations to my collection.
APPROACH :
- In the”res” folder of your project, create a new folder and name it “anim”.
- You need to create an xml file in which you will write the code for your animation.
- Get the object of the view in your activity/fragment.
- Call the startAnimation() method on the view. This method call requires you to send an object of animation in params.
- You can either create a new animation object and assign it the value returned by AnimationUtils.loadAnimation()method or directly pass the AnimationUtils.loadAnimation() method in the method used in step4.
- The AnimationUtils.loadAnimation() method takes two parameters: the first one is the Context, second one is the id of your xml file in which the animation is set. You need to pass the resourceId of the file you created in step2 (something like R.anim.your_file_name)
CODE:
my_custom_animation_horizontal_loading.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromXDelta="0" android:interpolator="@android:anim/linear_interpolator" android:repeatCount="infinite" android:repeatMode="restart" android:toXDelta="20" /> <!--Change duration time in which you want to complete one animation--> |
my_custom_animation_circular_loading.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:duration="400" android:repeatCount="infinite" android:pivotX="50%" android:pivotY="50%"> </rotate> <!--Change duration time in which you want to complete one animation--> |
Using these in your code.
Get an instance of the view which you want to animate and use below shared lines to apply animation to that particular view.
1 2 3 |
//Change the views as per your use. my_horizontal_animating_view.startAnimation((Animation) AnimationUtils.loadAnimation(this,R.anim.horizontal_loading_animation)); my_circular_animating_view.startAnimation((Animation)AnimationUtils.loadAnimation(this, R.anim.circular_rotation)); |
DEMO :
“…” is animating using”my_custom_animation_horizontal_loading” animation and the image is using “my_custom_animation_circular_loading” animation in the gif below.