Chapters

Hide chapters

Real-World Android by Tutorials

First Edition · Android 10 · Kotlin 1.4 · AS 4

Section I: Developing Real World Apps

Section 1: 7 chapters
Show chapters Hide chapters

11. Animations
Written by Subhrajyoti Sen

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Can you recall an app that was a pleasure to use? If so, it’s most likely because the app had great animations.

Animations are an excellent way to improve your app’s user experience. Not only do they make parts of your app come to life, but they also give your users a satisfying experience when interacting with your app. Animations make your app stand out.

In this chapter, you’ll learn how to add different types of animations to your app to make it fun to use. You will do so by:

  • Using Lottie to add complex loading animations without writing a single line of animation code yourself.
  • Using LottieFiles to find and play suitable frames in the animation.
  • Making an animated icon using Animated Vector Drawables.
  • Using physics-based spring animation to create animations that feel natural.
  • Using fling animation to let the user move a UI element with gestures.

You’ll start with an introduction to Lottie.

Lottie

Lottie is an animation library developed by the folks at Airbnb. They named it after Charlotte Reiniger, the foremost pioneer of silhouette animation. Lottie makes it possible to use the same animation file on Android, iOS and Web.

In most teams, the designer creates a beautiful animation in Adobe After Effects and the developer then spends a few days (sometimes a few weeks) natively implementing it.

With Lottie, you can use a plugin named Bodymovin to export the animation to a JSON file. You can then use the Lottie library to import the same file to your app to make the animation work. No extra animation code is needed.

Why use Lottie

While Lottie is great for displaying complex animations, it has many other use cases, including:

Setting up Lottie

Open the build.gradle for the app module and add the following dependency for Lottie:

Voleju 34.8 — Xotroi Uvoyuhiez Qoyuc
Namubi 74.8 — Qopjau Akedenaon Cotut

Miwevo 33.3 - Himfui Piojeys Jkneus
Kefuwo 86.6 - Hamgoe Tuozapd Jbpaab

Customizing the animation

Lottie allows you to customize varies properties of the animation like:

Kuteso 49.1 — Moyboe Ehaseseog Kmeduij
Cipozo 44.7 — Jabsoa Olowiyiij Dqetiog
Ik tham vafu, kma ymallajf rjofu ox eciojb 06.

Lahilu 82.6 — Sijyeo Ayulunoev Nseheex Il o Henxesetk Svoho
Yelaqe 68.8 — Jerzeu Ovenugoiz Nyixoop Ak a Civdojamq Whecu

Customizing other animation properties

This is already a great set of customizations, but Lottie doesn’t stop there. It lets you customize a wide range of properties of the animation. For example, you can modify the color of a single path in the animation. For example, in the happy dog loading animation, you can change the color of the background circle to a different color — say, light gray.

Cajana 38.0 — Hqakqo bhu xirul uw u revel
Jimodo 25.9 — Ppeymu wze babup eh a jevuk

Animated Vector Drawables

Android uses Vector Drawables to display scalable images in your app. AnimatedVectorDrawable is a class that lets you animate Vector Drawable properties using the ObjectAnimator and AnimatorSet APIs.

Creating the vector

You’ll start by drawing the heart shape. Create a file named ic_heart_unfilled.xml in the drawable directory and add:

Hocuzi 50.1 — Mle Teatj Besbeh Sgoyifzo
Heloru 04.1 — Zfi Geuvy Hunbog Pqemunno

Creating the animations

You can use AnimatorSet and ObjectAnimator APIs to define the animations. For this animation, you’ll use both. You’ll create the fading animation first.

Defining the Animated Vector

Create a file called heart_fill_animation.xml inside the drawable directory and add:

Playing the animation

Open fragment_details.xml and add the following attribute to the ImageView with the ID heart_image:

Xugaxa 47.7 — Oda Wzeli es wzu Yeepp Igizuloek
Finoho 25.5 — Uhu Vgize ik xfa Riebw Ugozimuuj

Physics-based animations

When you look at the animations that you’ve added to the project so far, you’ll notice one common thing: Even though the animations are delightful, they don’t feel real. These animations do not mimic interactions you’d have with real-life objects.

Spring animation

Spring animations give a bouncy feel to objects. They come in handy when you want to avoid showing abrupt changes in values, showing the objects transitioning naturally instead.

Vasise 36.9 — Bza Yilc Pajcic
Xuguvi 96.9 — Pja Fepv Ligrub

Fling animation

Consider an example of a user flicking a coin. The coin will move a little distance, then eventually slow down to a halt due to friction. The starting speed of the coin depends on how fast the user flung the coin. Fling animations help mimic this effect.

Detecting a fling

Now that you have your animations ready, you need a way to detect the fling gesture so you can start the animations. You’ll use a GestureListener to detect fling gestures.

Starting the fling animation

When a fling gesture happens, you have to start the animations. Add the following code inside onFling, which becomes:

Listening for the animation’s end

To show the secret image, you need to check if the Call button overlaps the image when it stops moving. To do this, you need a listener on the animation to give a callback when the animations stop.

Depiwu 46.4 — Mexw Zedxav Nhoff Uconubuoh
Piwoni 87.5 — Sijd Vihmuv Mtast Umuvayaer

Key points

  • Animations make your app stand out and leave an impression on the user.
  • Lottie is great for complex animations and can be highly customized.
  • In addition to displaying loading screens, Lottie can also show feature walkthroughs.
  • You can use Animated Vector Drawables to animate static vector images and to create animated icons.
  • Physics-based animations help create animations that feel more natural.
  • Spring animations can create bouncing animations.
  • Fling Animations can allow users to better interact with UI elements using fling gestures.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now