4

Progress Animations in Jetpack Compose with examples

 2 years ago
source link: https://blog.canopas.com/progress-animations-in-jetpack-compose-with-examples-eb76cc28fbb6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Progress Animations in Jetpack Compose with examples

Complex Animations in Jetpack Compose with examples

Animations are essential in a modern mobile applications to have a smooth user experience.

Jetpack Compose provides powerful and extensible APIs that make it easy to implement various animations in our app’s UI.

Today we are going to implement slightly complex animations in Jetpack Compose.

We are going to implement four different animations in this blog post:

  1. Twin circle animation : This animation contains two white circles that will be having zoom-in effect simultaneously, and they will ultimately hide parent element and then they will follow same animation in reverse order(zoom out).
  2. Circle offset animation: This animation again contains two circles and this time they will move from left to right and vice versa(offset animation) While offsetting from left to right or right to left when two circles interacts with each other the offset animation speed will slightly decrease providing the magnet like effect.
  3. Pacman animation: We all played Pacman game during our childhood. This animation is exactly the same as Pacman. Here, in this animation Pacman will open and close his mouth in repeated order(not to eat food).
  4. Arc rotation animation: In this animation we will have two arcs that will rotate continuously in clockwise direction. At the center of the Arc we will have two circles one circle will stay steady while other will zoom in and zoom out repeatedly.

At the end of this article we are going to learn animations like:

Cool Progress animations

Let’s start with the first animation:

1. Twin circle animation

As the name suggests we will have two circles(twins obviously) inside parent circle(with red background) as a part of UI.

Here, we have taken row for a parent circle and inside that we have used two Boxes for our twin circles, we have used spacer two provide space between two twin circles.It is all about UI part of our first animation.

Now, let's talk about twinCircleAnimation which will basically provide animation to our two tiny circles.

Here, we have used infiniteTransition and we are remembering it as we need to repeat our animation for an infinite amount of time. Then animateFloat will create the animation of type float that runs infinitely as a part of infiniteTransition.

Once the animation is created it will run from the initial value (1f) to the target value (7f) and repeat it.

Here, we have used tween as a duration-based AnimationSpec and it uses easing to adjust the animation’s fraction.

Easing allows the animating value to speed up and slow down, rather than moving at a constant speed.

Jetpack compose provides several built-in Easing functions that cover almost all the use cases. Here we have used FastOutSlowInEasing. Another Jetpack composes built-in easing are:

  • FastOutSlowInEasing
  • LinearOutSlowInEasing
  • FastOutLinearInEasing
  • LinearEasing
  • CubicBezierEasing

Tween also has another parameter called durationMills which is the waiting period after each iteration of our animation(we have used 1500 in the Twin animation example).

The complete composable for our Twin circle animation is available on Github.

2. Circle offset animation

Here, we have two circles that will move from left to right and vice versa.

Here, we need to observe three things:

When circles move towards offset values, they change their colors, size and when two circles interact with each other they slow down (with the help of LinearOutSlowInEasing).

Here, we have used canvas to draw our custom UI, inside canvas we have three circles so the first circle with LightGray color is a steady circle and the other two circles are going to move from left to right and vice versa.

So first we need to have an animation for color as we are changing it while moving with offset values.

It is just the same as our Twin circle animation, where we used animateFloat here as we are animating color, we are using animateColor. The rest of the things are already covered inside our twin circle animation.

Now, to move two circles on given offset values we have something like:

Here, our circles will move on offsetX in the range of (0f,100f,-100f,0f) means 0f to 100f, then 100f to -100f and lastly from -100f to 0f.

Another circle will have the same path but in opposite direction, animateValues which is composable that will help us change the values based on target positions.

A typical animateValues composable will look something like this:

And lastly, to change the size of the circles when they move on given offset values we have animation like:

Here, also we have used animateValues composable for the same purpose to change the scale values.

The complete composable for circle offset animation is available on Github:

3. Pacman animation

For Pacman animation also we are using canvas to draw Pacman UI.

Here, as we can see inside canvas we have one arc for Pacman and we have one circle that is for the eye of our Pacman.

Inside drawArc we have used two animations called mouth animation and antimouthAnimation, so when both these animations are used in a single arc there will be animation effects like a mouth opening and closing.

Mouth animation will rotate our Pacman arc from 360f to 280f(upper side of mouth) and anti-mouth animation will rotate our Pacman arc from 0f to 40f(lower side of mouth).

The complete composable for Pacman animation is available on Github.

4. Arc rotation animation

As seen in the snippet below Arc rotation animation also contains a canvas to draw Arc rotation UI.

As we can see we have two arcs that will rotate continuously towards our circle(Light gray circle). We have one steady circle(Light gray) and one circle with animation (Green circle).

We have used the above animation in our first arc, which will draw the arc at a start angle 0f initially and then after continuously it will change its start angle until 180f so we will have effect like arc rotation.

The above animation will be used in our second arc, which will draw the arc at a start angle 180f initially, and then after continuously it will change its start angle until 360f so we will have an effect like arc rotation.

We have used LinearEasing for both the arcs as we want to have a constant rotation speed for both arcs.

When we observe both the arc simultaneously we will have an effect like both arcs rotating in synchronization in a clockwise direction.

And lastly, for our circle with animation, we have used the above animation with FastOutLinearEasing.

The complete composable for Arc rotation animation is available on Github.

That’s it for all the Animations…

Don’t worry if you didn’t get all the animations.

You can find the complete source code of all the above animations on Github.

If you love these animations, here are few more for you written by my colleague, these animations are also available in above mentioned Github repository.

If you like what you read, be sure to 👏👏👏 below — as a writer it means the world!

Follow Canopasto get updates on interesting tech articles!

Happy animating!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK