Welcome to this series of articles that covers Jetpack Compose animations. Along the way, we’ll be using the most common layout. You guessed it, the list!
In this article, we’ll try to create a cool animation for when an item is added to a list. Without further ado, let’s start with a preview of what we’re going to build:

If you want to follow along or want the code of the UI, check out this repository:

Note that compose version is 1.0.0

Let’s summarize what is happening so we can split this implementation into smaller lucid pieces.

  1. On add button click event, we will randomly pick a ShoesArticle object from an already initialized array of articles.
  2. Then, a particle, which is tinted with a passed color from the chosen article, will be fired from underneath the top bar.
  3. When the particle is in position, the item’s background will expand from the particle to the slot size.
  4. Finally, we animate the content alpha to make it visible.

1. Particle

1.1. UI

So, how to draw a circle in Compose? We will use a canvas to do that. It is pretty straightforward.

Now, we will head to MainActivity to place it under the top bar. So, we need to wrap the top bar and the Particle into a Box. Then, we generate a random color to update the circle tint every time we click the AddCircle icon.

1.2. Animation

What exactly are we going to animate? That’ll be the top translation of the particle. Let’s put that in a state, and translate the circle according to its value.

We need to swing the particle between its first position and the position of the first item in our LazyColumn. So, we animate the top translation between zero and the distance between the center of the particle and the first item. We can calculate the latter with the addition of the particle radius, the top padding of the LazyColumn layout, and the height’s half of an item.
For such a use case, we would normally use animate*AsState.

animate*AsState is a fire-and-forget animation function. This Composable function is overloaded for different parameter types such as Float, Color, Dp, etc. When the provided targetValue is changed, the animation will run automatically.

We can use a different targetValue basing on a boolean state.

However, we want to reset the topTransition to 0f after the animation to make it ready for the next item adding process. So, we need another API for our animation. We will use Animateable.

Animateable is a value holder that can animate the value as it is changed via animateTo.

animateTo is a suspend function. So, we need to wrap it into a coroutine.

LaunchedEffect launches a block into the composition’s CoroutineScope. The coroutine will be canceled and re-launched when LaunchedEffect is recomposed with a different key or keys.

So, we would use isFired as a key, and execute the block only when that key is true. Then, we animate our Animateable while updating topTranslation. We can spice it up with a physics-based animation using the spring function to produce a specification of an animation. Subsequently, we would reset our top translations. And finally, we execute a callback that will be used to notify that the newly added item should be animated.

2. Item

2.1. UI

Now, we need to play with the background. So, we can use Modifier.drawBehind() to draw into a canvas behind the modified content. And replace the background color with a transparent one. We want to draw a circle with a dynamic radius that changes between the particle’s equivalent and a max radius that we have to calculate. That’s the value that we are looking for:

We can compute this with: sqrt( (width/2) ^2 + (height/2)^2). Kotlin offers us an inline function that achieves this, which is hypot().
But now we need to get the slot size after its initial composition. In Android Views, we normally use viewReference.post {}. The Compose counterpart is Modifier.onGloballyPositioned {}.
The last thing, we want to add a visibility alpha for the item’s content to animate it later after the circular expansion of the background.

Back to MainActivity now to add an item. Nothing complex here, we create a ShoesArticle basing on the color we randomly picked, and we increment the id for the next article.

Now, we handle the items states. We create a map basing on our shoesArticles list, and we change the newly added item state to true when the particle animation is complete. Also, we set isFired to false so that when it is switched to true on the next click, the LaunchedEffect block of the particle will execute.

2.2. Animation

We want to animate the radius in the background, and then the alpha of the content. So, we utilize the same approach used in particle implementation.

3. Final thoughts

Since the items will be recomposed with a true isVisible parameter, they will animate. And we will get a scroll animation:

4. External links and source code

Animation | Jetpack Compose | Android Developers

Here is the full Github repository for this article:

That’s it for this article. See you in the next one of this series.

Here We Go Again : (

if (article == helpful) {
    println("Like and subscribe to blog newsletter.")
} else {
    println("Let me know what i should blog on.")

This site uses Akismet to reduce spam. Learn how your comment data is processed.