Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 104 ยท Building a Real UI

Lottie, Shimmer and View Animations in Android

Add motion to Android apps: Lottie JSON animations, shimmer loading placeholders in Compose and Views, ObjectAnimator, interpolators, fade and rotate effects, with runnable math stand-ins.

Motion tells users what is happening: a spinner says "wait", a skeleton says "content is coming here", a fade says "this replaced that". Animation in Compose covers the built-in APIs (animate*AsState, AnimatedVisibility, transitions). This lesson adds three things people reach for next: Lottie (designer-made vector animations), shimmer (loading placeholders) and the older View animation toolkit (ObjectAnimator, interpolators, ViewPropertyAnimator) that you will find in XML-based apps and games.

Animations need a screen, so the Android snippets are read-only. The TryIts run the underlying math in plain Kotlin: interpolators, shimmer offsets and Lottie frame calculations. Knowing the math makes the APIs predictable.

Lottie: animations as JSON

Lottie plays animations that designers export from After Effects (through the Bodymovin plugin) as a small JSON file. The file describes vector shapes, colors and keyframes, so it scales to any size, usually weighs a few kilobytes and is much lighter than a GIF or a video. Typical uses: a success check mark, an empty-state illustration, onboarding screens, pull-to-refresh.

Put the file in res/raw/ (or assets/) and play it:

Kotlin
// read-only example: Lottie in Compose
// build.gradle.kts: implementation("com.airbnb.android:lottie-compose:<version>")
@Composable
fun SuccessAnimation() {
    val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.success))
    val progress by animateLottieCompositionAsState(
        composition = composition,
        iterations = 1                     // or LottieConstants.IterateForever
    )
    LottieAnimation(composition = composition, progress = { progress }, modifier = Modifier.size(160.dp))
}
XML
<!-- read-only example: Lottie in a View layout -->
<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/loading"
    android:layout_width="160dp"
    android:layout_height="160dp"
    app:lottie_rawRes="@raw/loading"
    app:lottie_autoPlay="true"
    app:lottie_loop="true" />

Useful controls: playAnimation(), pauseAnimation(), setMinAndMaxFrame(from, to) to play only a segment, speed to change pace, and addAnimatorListener to react when the animation ends. Free files are on LottieFiles, but check each license, and keep the performance in mind: huge or complex files (masks, many layers) can slow low-end devices.

A Lottie file has a frame rate and in/out frames. The player turns the progress (0 to 1) into a frame, so segment and speed logic is easy to reason about:

Kotlin Lottie progress, frames and segments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Shimmer: skeleton loading placeholders

A shimmer effect shows grey placeholder shapes with a light band sweeping across while data loads. It communicates the page layout before the content arrives and feels faster than a lone spinner. The classic View library is Facebook's ShimmerFrameLayout, which wraps placeholder views; in Compose you build it yourself with a moving gradient brush, so there is no extra dependency.

Kotlin
// read-only example: a shimmer brush in Compose
@Composable
fun shimmerBrush(): Brush {
    val transition = rememberInfiniteTransition(label = "shimmer")
    val offset by transition.animateFloat(
        initialValue = -300f,
        targetValue = 1000f,
        animationSpec = infiniteRepeatable(tween(1200, easing = LinearEasing)),
        label = "offset"
    )
    return Brush.linearGradient(
        colors = listOf(Color(0xFFE0E0E0), Color(0xFFF5F5F5), Color(0xFFE0E0E0)),
        start = Offset(offset, 0f),
        end = Offset(offset + 300f, 0f)
    )
}

@Composable
fun ArticlePlaceholder() {
    val brush = shimmerBrush()
    Column(Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
        Box(Modifier.fillMaxWidth().height(180.dp).background(brush, RoundedCornerShape(8.dp)))
        Box(Modifier.fillMaxWidth(0.8f).height(20.dp).background(brush, RoundedCornerShape(4.dp)))
        Box(Modifier.fillMaxWidth(0.5f).height(20.dp).background(brush, RoundedCornerShape(4.dp)))
    }
}

The same pattern works in Views with ShimmerFrameLayout: wrap a skeleton layout, call startShimmer() while loading, then stopShimmer() and hide it when data arrives. Tie the visibility to your UI state: Loading shows the skeleton, Success shows content.

Two practical rules: show a skeleton only if loading may take longer than a fraction of a second (flicker looks worse than nothing), and match the skeleton's shape to the real content so the layout does not jump.

The sweeping band is a position that moves linearly and repeats. Here is the math, printed as a text strip so you can see the band travel.

Kotlin A shimmer band sweeping across a strip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The View animation toolkit

XML-based apps use property animation. An animator changes a real property of an object over time, so the view's actual position or alpha changes (unlike the old Animation classes, which only changed how a view was drawn).

Kotlin
// read-only example: three ways to animate a view
// 1. ViewPropertyAnimator: short and readable, for common properties
binding.card.animate()
    .alpha(1f)
    .translationY(0f)
    .setDuration(300)
    .setInterpolator(DecelerateInterpolator())
    .start()

// 2. ObjectAnimator: any property with a setter, such as "rotation"
ObjectAnimator.ofFloat(binding.bottle, "rotation", 0f, 1440f).apply {
    duration = 2000
    interpolator = DecelerateInterpolator()   // spin fast, slow down to a stop
    start()
}

// 3. AnimatorSet: run several animators together or in sequence
AnimatorSet().apply {
    playTogether(
        ObjectAnimator.ofFloat(binding.logo, "scaleX", 0.8f, 1f),
        ObjectAnimator.ofFloat(binding.logo, "scaleY", 0.8f, 1f),
        ObjectAnimator.ofFloat(binding.logo, "alpha", 0f, 1f)
    )
    duration = 400
    start()
}

A fade is just alpha from 0 to 1 (or 1 to 0 and then visibility = GONE in an end action). For text that fades out and in with a new string, fade out, change the text in the end callback and fade back in. XML versions of the same ideas live in res/anim/ (<alpha>, <scale>, <translate>, <rotate>) or res/animator/ (<objectAnimator>), loaded through AnimationUtils.loadAnimation or AnimatorInflater. For list-item and screen changes, TransitionManager and the Navigation component's enter/exit animations (see Navigation Compose) are higher level.

Interpolators are just functions

An interpolator maps the elapsed fraction of time (0 to 1) to the fraction of the animation (0 to 1). That one idea explains every named curve: LinearInterpolator returns the input; AccelerateInterpolator starts slowly (t * t); DecelerateInterpolator ends slowly (1 - (1 - t)^2); AccelerateDecelerateInterpolator does both; OvershootInterpolator goes past the end and returns; BounceInterpolator bounces at the end.

Kotlin Interpolators and a spin that slows down

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The bottle covers 1,080 of its 1,440 degrees in the first second and only 360 in the second: exactly the "fast, then settle" feel of DecelerateInterpolator.

Choosing a tool

Need Use
Change one value smoothly in Compose animate*AsState (Animation)
Designer-made illustration or icon Lottie
Loading skeleton Shimmer brush or ShimmerFrameLayout
Fade, slide, scale, rotate a View ViewPropertyAnimator or ObjectAnimator
Screen layout morphs (collapsing headers) MotionLayout (XML layouts)
Shared element between screens Shared element transitions in Navigation

Respect the user's "remove animations" setting: when Settings.Global.ANIMATOR_DURATION_SCALE is 0, property animators finish instantly, and decorative infinite animations should stop. See Accessibility.

Common mistakes

  • Looping a heavy animation forever, which keeps the GPU and battery busy even when the screen is not visible. Pause in onPause or when Lifecycle is not started.
  • Animating layoutParams width or height every frame, which triggers layout each time. Animate transforms (translation, scale, alpha) instead.
  • Forgetting that an ObjectAnimator needs a getter/setter pair with the exact property name.
  • Skeletons that do not match the final layout.
  • Using Lottie files without checking license or size.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a Lottie animation file?

    Lottie JSON is vector based, so it scales without blur and is usually very small compared with GIF or video.
  2. 2. What does an interpolator do?

    An interpolator reshapes the progress curve: linear, accelerating, decelerating and so on.
  3. 3. Which is best for a smooth, cheap fade-in of a view?

    alpha, translation, scale and rotation are transform properties and do not require a new layout pass.
  4. 4. When should a shimmer skeleton be shown?

    A skeleton that matches the final layout makes waiting feel shorter; for instant loads it would only flicker.

Summary

  • Lottie plays small vector JSON animations in Compose or Views and supports segments, speed and listeners.
  • Shimmer placeholders are a moving gradient over skeleton shapes; show them only while loading and match the layout.
  • ViewPropertyAnimator, ObjectAnimator and AnimatorSet animate real properties of Views.
  • An interpolator is a function from time fraction to progress fraction.
  • Prefer transform properties, pause infinite animations when hidden and honor the system animation settings.

Next: Timers, clocks, sensors and speech.