![]() |
VOOZH | about |
The animation is a method in which a collection of images is combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seems to be alive. Android has quite a few tools to help you create animations with relative ease. So in this article, let's learn to create android animations using Java.
| XML ATTRIBUTES | DESCRIPTION |
|---|---|
| android:id | Sets unique id of the view |
| android:duration | Used to specify the duration of the animation |
| android:fromDegrees | Starting angular position (in degrees) |
| android:toDegrees | Ending angular position (in degrees) |
| android:fromXScale | Starting X size offset |
| android:toXScale | Ending of X size offset |
| android:fromYScale | Starting Y size offset |
| android:toYScale | Ending of Y size offset |
| android:fromAlpha | Starting alpha value for the animation (1.0 means fully opaque and 0.0 means fully transparent) |
| android:toAlpha | Ending alpha value |
| android:fromYDelta | Change in Y coordinate to be applied at the beginning of the animation |
| android:toYDelta | Change in Y coordinate to be applied at the end of the animation |
| android:pivotX | Represents the X-axis coordinates to zoom from the starting point |
| android:pivotY | Represents the Y-axis coordinates to zoom from the starting point |
| android:interpolator | It defines the rate of change of an animation |
| android:startOffset | Delay occurs when an animation runs (in ms), once start time is reached |
Step 1: Create a New Project
Step 2: Modify activity_main.xml file
In the XML file, we have added an ImageView, TextView, and Button inside the RelativeLayout.
Step 3: Add these XML files to the anim directory
After modifying the layout we will create XML files for animations. So we will first create a folder name anim. In this folder, we will be adding the XML files which will be used to produce the animations. For this to happen, go to app/res right-click and then select Android Resource Directory and name it as anim.
Some Common Types of Animations in Android are,
Step 4: Modify MainActivity.java
To perform animation in android, we have to call a static function loadAnimation() of the class AnimationUtils. We get the result in an instance of the Animation Object. Syntax to create animation object:
Animation object = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ANIMATIONFILE);
To apply the above animation to an object(Let say in an image), we have to call the startAnimation() method of the object. Syntax to call the method:
ImageView image = findViewById(R.id.imageID);
image.startAnimation(object);
Methods of animation class:
Method | Description |
| startAnimation(object) | Starts the animation |
| setDuration(long duration) | Sets the duration of animation |
| getDuration() | Gets the duration of animation |
| end() | Ends the animation |
| cancel() | Cancels the animation |