VOOZH about

URL: https://www.geeksforgeeks.org/android/android-animations-using-java/

⇱ Android Animations using Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Android Animations using Java

Last Updated : 15 Jul, 2025

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. 

Table of Attributes

XML ATTRIBUTESDESCRIPTION
android:idSets unique id of the view
android:durationUsed to specify the duration of the animation
android:fromDegreesStarting angular position (in degrees)
android:toDegreesEnding angular position (in degrees)
android:fromXScaleStarting X size offset
android:toXScaleEnding of X size offset
android:fromYScaleStarting Y size offset
android:toYScaleEnding of Y size offset
android:fromAlphaStarting alpha value for the animation
(1.0 means fully opaque and 0.0 means fully transparent)
android:toAlphaEnding alpha value
android:fromYDeltaChange in Y coordinate to be applied at the beginning of the animation
android:toYDeltaChange in Y coordinate to be applied at the end of the animation
android:pivotXRepresents the X-axis coordinates to zoom from the starting point
android:pivotYRepresents the Y-axis coordinates to zoom from the starting point
android:interpolatorIt defines the rate of change of an animation
android:startOffsetDelay occurs when an animation runs (in ms), once start time is reached

How to add animation in Android using Java

Step 1: Create a New Project

  • Start Android Studio (version > 2.2)
  • Go to File -> New -> New Project.
  • Select Empty Activity and click on next
  • Select minimum SDK as 21
  • Choose the language as Java and click on the finish button.
  • Modify the following XML and java files.

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,

  1. Blink - Hides the object for 0.6 to 1 second.
  2. Slide - Move the object either vertically or horizontally to its axis.
  3. Rotate - Rotate the object either clockwise or anti-clockwise.
  4. Zoom - Zoom in or out the object in the X and Y-axis.

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

Output: Run on Emulator
 

Comment
Article Tags:

Explore