Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Android Rotate animations in Kotlin
Next article icon

Android Rotate animations in Kotlin

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 directions. 


The class hierarchy of the RotateAnimation class in Kotlin

AndroidRotate class heirarchy in kotlin

XML attributes which define the rotation of an object

XML AttributesDescription
android:pivotXTo define the X coordinate of the point about which the object is being rotated
android:pivotYTo define the Y coordinate of the point about which the object is being rotated
android:fromDegreesRotation of the object starts from this geometrical degree
android:toDegreesRotation of the object ends at this geometrical degree
android:durationUsed to define the duration of the animation in millisecond
android:startOffsetUsed to delay the animation time in millisecond

Approach:

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 

  1. Click on File, then New => New Project.
  2. Select language as Kotlin.
  3. 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 activity.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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"     android:background="#168BC34A"     tools:context=".MainActivity" >      <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:fontFamily="@font/roboto"         android:text="@string/heading"         android:textAlignment="center"         android:textColor="@android:color/holo_green_dark"         android:textSize="36sp"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.050000012" />      <ImageView         android:id="@+id/imageView"         android:layout_width="243dp"         android:layout_height="241dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/textView"         app:layout_constraintVertical_bias="0.19999999"         app:srcCompat="@drawable/logo" />      <Button         android:id="@+id/clk_rotate_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="#AB4CAF50"         android:fontFamily="@font/roboto"         android:text="@string/clk_rotate_button_text"         android:textSize="14sp"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.12"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/imageView"         app:layout_constraintVertical_bias="0.7" />      <Button         android:id="@+id/anticlk_rotate_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="#AB4CAF50"         android:fontFamily="@font/roboto"         android:text="@string/anticlk_rotate_button_text"         android:textStyle="bold"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.78"         app:layout_constraintStart_toEndOf="@+id/clk_rotate_button"         app:layout_constraintTop_toBottomOf="@+id/imageView"         app:layout_constraintVertical_bias="0.7" /> </androidx.constraintlayout.widget.ConstraintLayout> 


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_clockwise and rotate_anticlockwise. These 2 files are the XML file which holds the details of the animation. Below is the code for both the file. 

Filename: rotate_clockwise.xml

rotate_clockwise.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">      <rotate         android:pivotX = "50%"         android:pivotY = "50%"         android:fromDegrees = "0"         android:toDegrees = "360"         android:duration = "2500"/>  </set> 


Filename: rotate_anticlockwise.xml

rotate_anticlockwise.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">      <rotate         android:pivotX = "50%"         android:pivotY = "50%"         android:fromDegrees = "360"         android:toDegrees = "0"         android:duration = "2500"/>  </set> 


Step 4: Modify MainActivity.kt File 

Below is the code for MainActivity.kt file to load and start the animation on the ImageView widget according to the button clicked by the user.

MainActivity.kt
package com.example.androidrotateanimation  import android.os.Bundle import android.view.animation.AnimationUtils import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          // assigning id of button which rotates         // the image in clockwise direction         val clk_button: Button = findViewById(R.id.clk_rotate_button)          // assigning id of button which rotates         // the image in anti-clockwise direction         val anticlk_button: Button = findViewById(R.id.anticlk_rotate_button)          // assigning id of imageview         // which is to be rotated         val image: ImageView = findViewById(R.id.imageView)          // actions to be performed when         // "rotate clockwise" button is clicked         clk_button.setOnClickListener()         {              // loading the animation of             // rotate_clockwise.xml file into a variable             val clk_rotate = AnimationUtils.loadAnimation(                 this,                 R.anim.rotate_clockwise             )              // assigning that animation to             // the image and start animation             image.startAnimation(clk_rotate)         }          // actions to be performed when         // "rotate anticlockwise" button is clicked         anticlk_button.setOnClickListener()         {              // loading the animation of             // rotate_anticlockwise.xml file into a variable             val anticlk_rotate = AnimationUtils.loadAnimation(                 this,                 R.anim.rotate_anticlockwise             )              // assigning that animation to             // the image and start animation             image.startAnimation(anticlk_rotate)         }     } } 

Run as Emulator


Next Article
Android Rotate animations in Kotlin

R

RISHU_MISHRA
Improve
Article Tags :
  • Kotlin
  • Android
  • Android-Animation

Similar Reads

    Android Animations in Kotlin
    Animation is a method in which a collection of images are combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seem to be alive. Android has quite a few tools to help you create animations with relative ease. so in this article we wil
    5 min read
    Slide Animation in Android with Kotlin
    Animations in Android allow UI elements to perform aesthetic moves across the screen. Animations can be implemented from a third party as well as developed from scratch. Despite the source, animations can be applied to any kind of view or UI element. Typically, we have demonstrated a slide animation
    3 min read
    Wave Animation in Android
    Wave Animation is one of the most commonly used features in Android app. You can see this animation in most of the shopping apps, music player apps, and many more. Using this Wave Animation makes the User Experience attractive. In this article, we are going to see how to implement Wave Animation in
    2 min read
    Android Fade In/Out in Kotlin
    In Android Animations are the visuals that are added to make the user interface more interactive, clear and good looking. Fade In and Fade out animations are used to modify the appearance of any view over a set interval of time so that user can be notified about the changes that are occurring in our
    3 min read
    First Android Application in Kotlin
    We can build an Android application using Kotlin and Java. In the Android Studio Kotlin Tutorial, we are using Kotlin language to build the application. In the previous tutorial, we learned how to create a project for Kotlin language but here, we will learn how to run an application using the AVD (A
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences