Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Custom Animations in Android Development

Introduction

Custom animations can greatly enhance the user experience in your Android applications by making interactions more vivid and engaging. This tutorial will guide you through the process of creating custom animations from start to finish with detailed explanations and examples.

Prerequisites

Before you start, ensure you have the following:

  • Basic knowledge of Android development
  • Android Studio installed
  • A basic understanding of XML and Java/Kotlin

Creating Simple Animations

We will start by creating a simple fade-in animation using XML. Android provides a set of built-in animations that can be used for basic transformations.

Step 1: Define the Animation

Create a new XML file in the res/anim directory. Name it fade_in.xml.

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"/>
                

Step 2: Apply the Animation

Open your activity layout XML file and add a view element, such as a TextView.

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, World!"
    android:textSize="24sp"
    android:layout_gravity="center"/>
                

In your activity Java/Kotlin file, load and start the animation as follows:

Java:
TextView textView = findViewById(R.id.textView);
Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
textView.startAnimation(fadeIn);

Kotlin:
val textView: TextView = findViewById(R.id.textView)
val fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in)
textView.startAnimation(fadeIn)
                

Advanced Animations

For more complex animations, you can define multiple transformations and combine them. Let's create a more advanced animation that combines a fade and a scale transformation.

Step 1: Define the Animation Set

Create a new XML file in the res/anim directory. Name it fade_scale.xml.

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>
    <scale
        android:duration="1000"
        android:fromXScale="0.5"
        android:fromYScale="0.5"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"/>
</set>
                

Step 2: Apply the Animation

In your activity Java/Kotlin file, load and start the animation as follows:

Java:
TextView textView = findViewById(R.id.textView);
Animation fadeScale = AnimationUtils.loadAnimation(this, R.anim.fade_scale);
textView.startAnimation(fadeScale);

Kotlin:
val textView: TextView = findViewById(R.id.textView)
val fadeScale = AnimationUtils.loadAnimation(this, R.anim.fade_scale)
textView.startAnimation(fadeScale)
                

Animating Property Values

Property animations allow you to animate almost any property of an object. Android provides the ObjectAnimator class for this purpose.

Example: Rotating a View

We will create an animation that rotates a view 360 degrees.

Step 1: Add the View

Open your activity layout XML file and add a view element, such as an ImageView.

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher_foreground"
    android:layout_gravity="center"/>
                

Step 2: Apply the ObjectAnimator

In your activity Java/Kotlin file, create and start the property animation as follows:

Java:
ImageView imageView = findViewById(R.id.imageView);
ObjectAnimator rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
rotate.setDuration(1000);
rotate.start();

Kotlin:
val imageView: ImageView = findViewById(R.id.imageView)
val rotate = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f)
rotate.duration = 1000
rotate.start()
                

Conclusion

We have covered the basics of creating custom animations in Android using XML and the ObjectAnimator class. By mastering these techniques, you can create more engaging and interactive user experiences in your applications.