Tech Info

  1. Main page
  2. Android
  3. Main content

Using Compose to make amazing loading animations

2023-10-14 449hotness 0likes 0comments

Spinning dots

I've seen elsewhere online where someone used CSS to create an effect with several dots spinning around the center, each dot sequentially changing in size and transparency. Since CSS can do this, can we create a similar effect using Compose? Let's give it a try. First, since the dots spin around the center, we need to determine the coordinates of the center point and the radius.

image.png

In the code, centerX and centerY are the coordinates of the center point, radius is the radius of the circle, and mSize gets the canvas size to dynamically update the center point coordinates and radius. We also need a set of angles - these angles will be used to draw the dots around the circumference.

image.png

angleList stores the angles for drawing each dot. With these angles, we can use the sine and cosine formulas to calculate the center point coordinates for each dot:

image.png

pointX and pointY are the functions to calculate the center point coordinates for each dot. Using this, we can first draw a circle of dots around the circumference.

image.png image.png

Now that we've drawn the dots, how do we make them spin and change size and transparency? We can think of this process as continuously changing the size and transparency of each dot as it is drawn. So we can create two more arrays to store the changing radius and transparency values.

image.png

When drawing each dot, we can use an animation loop to pull values for each dot from the radius and alphaList arrays. This will create the change in size and transparency.

image.png 0620aa2.gif

One more thing - we want each dot's size and transparency change to be different. We can do this by incrementing the listIndex each time we get it, and resetting it to 0 when it exceeds the array size. This makes each dot unique:

image.png

And we now have the completed effect:

0620aa3.gif

Colorful rings

This animation mainly uses the Modifier.graphicsLayer operator. Let's look at the parameters it provides:

image.png

We can see many graphics related parameters like size, translation, transparency, rotation etc. Here we will use the three rotation parameters: rotationX, rotationY, rotationZ.

For example, we have a Ring function that draws a ring:

image.png

We can create three rings in our UI and rotate them around the x, y and z axes:

image.png

The rotation uses an animation from 0 to 360 degrees. The effect looks like:

0625aa1.gif

It seems like only the first two rings moved - the third one didn't. But the third one is also moving, rotating around the z axis like a wheel. Let's try combining the three rings into one, rotating around all three axes. What will happen?

0625aa2.gif

The ring rotation immediately becomes more 3D. But one ring is a bit dull, so let's add a few more rings. And to prevent overlap during rotation, we'll use different rotation angles for each one. How? We can create two more animations, one from 60 to 420 degrees, the other from 90 to 450 degrees:

image.png

With three animations and three axes, we can have six rings with unique rotation directions by permutation:

image.png 0625aa3.gif

Now let's color the rings - after all, it's called colorful rings. We can define a color array in the Ring function, and use a timer to periodically pull colors from the array and set them on the ring:

image.png

We have an index variable initially pointing to the first color. Every 500ms we switch colors, looping back to the first when reaching the end. Let's see:

0625aa4.gif

It's a bit monotonous with all rings starting the same color. We can improve by setting a different starting color for each ring. Add a startIndex parameter to the Ring function, and initialize index to startIndex instead of 0.

image.png image.png

Now each ring has its own starting color. The final effect:

0625aa5.gif

Colorful tail

Another colorful animation - this time a tail. How? Let's start by drawing an arc:

image.png

An arc is a sector that doesn't connect to the center point. We use the drawArc function with some arbitrary values to get a 150 degree arc starting at 0 degrees.

image.png

Let's spin it by changing startAngle in an animation loop:

image.png 0625aa6.gif

A spinning arc! Now to turn it into a colorful tail. Since there's no tail property, we'll draw one using multiple stacked arcs. Increase startAngle and decrease sweepAngle for each arc, also increasing thickness, to look like a tail. We need three arrays for startAngle, sweepAngle and thickness:

image.png

Loop through strokeList, using corresponding array values to draw each arc:

image.png

We now have a spinning red tail!

0625aa7.gif

For color changing, instead of the previous approach, we'll use animateColorAsState. Use a timer to periodically get colors, set to animateColorAsState's targetValue, and apply the animated State<Color> to the arc's color property:

image.png image.png

And we have a colorful tail!

0625aa8.gif

Windmill

There are many ways to draw a windmill - two intersecting thick lines, four sectors, etc. Here we'll use Bezier curves and the quadraticBezierTo function for drawing a quadratic curve. Let's start with one blade:

image.png

We see the control points are the canvas center, top left corner, and center of the top edge. This gives us one windmill blade.

image.png

We can draw the other three blades the same way:

image.png image.png

That was quick to draw a windmill. Now to animate it. We can use graphicsLayer and rotationZ for rotation. Or since we only need z-axis rotation, we can just use rotate which calls graphicsLayer internally:

image.png

Call the Windcar function and rotate:

image.png 0625aa9.gif

A quick improvement is to add a stick by wrapping Windcar and a Spacer in a Box:

image.png

And our windmill is complete:

0625aa10.gif

Summary

With Compose we can easily create animations that previously required asking designers for image assets or Lottie files. Try recreating some of your app's loading animations with Compose!

Related

This article is licensed with Creative Commons Attribution 4.0 International License
Tag: Android Compose
Last updated:2023-10-14

jimmychen

This person is a lazy dog and has left nothing

Like
< Last article
Next article >

Comments

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
Cancel

Archives
  • October 2023
  • September 2023
Categories
  • Algorithm
  • Android
  • Backend
  • Embedded
  • Security
Ads

COPYRIGHT © 2023 Tech Info. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang