Android Rotate animations in Kotlin
Rotate animation is a special kind of animation in Android which controls the Rotation of an object. These type of animations are usually used by developers to give a feel to the user about the changes happening in the application like loading content, processing data, etc. By using the rotate animation effect, an object can be rotated in the X-Y plane of activity and it allows the rotation in both Clockwise and Anticlockwise direction.
The class hierarchy of RotateAnimation class in Kotlin
XML attributes which define the rotation of an object
|android:pivotX||To define the X coordinate of the point about which the object is being rotated|
|android:pivotY||To define the Y coordinate of the point about which the object is being rotated|
|android:fromDegrees||Rotation of the object starts from this geomertical degree|
|android:toDegrees||Rotation of the object ends at this geomertical degree|
|android:duration||Used to define the duration of the animation in millisecond|
|android:startOffset||Used to delay the animation time in millisecond|
This example demonstrates the steps involved in implementing the clockwise and anticlockwise rotation animation to an image file. An image file will be added in the activity using ImageView.
Note: The steps are performed on Android Studio version 4.0
Step 1: Create a New Project
- Click on File, then New => New Project.
- Select language as Kotlin.
- Select the minimum SDK as per your need.
Step 2: Modify activity_main.xml file
Below is the code for
activity_main.xml file to add a TextView, ImageView and 2 buttons in an activty.
Step 3: Define XML file for clockwise and anticlockwise rotation of the image
Create a new directory in the res folder of the application and name it anim. In this directory create 2 Animation Resource File namely
rotate_anticlockwise. These 2 files are the XML file which holds the details of the animation. Below is the code for both the file.
Step 4: Modify MainActivity.kt File
Below is the code for
MainActivity.kt file to load and start animation on the ImageView widget according to the button clicked by the user.
Step 5: Modify strings.xml File
All the strings which are used in the activity are listed in this file.