![](/style/images/good.png)
![](/style/images/bad.png)
Creating a Circular Progress Bar with a Gradient using Jetpack Compose
source link: https://www.bam.tech/article/creating-a-circular-progress-bar-with-a-gradient-using-jetpack-compose
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.
Inscrivez-vous à notre newsletter
Are you ready to add some colorful flair to your Jetpack Compose project? In the following lines, you will be walked through implementing a circular progress indicator with a gradient.
With each step, we will discover a bit of Jetpack Compose implementation, Canvas
API and SweepGradient
.
Let's take a look at the design that our designer has provided for implementation:
![641da56f84263a6e426a5d52_Creating%20a%20Circular%20Progress%20Bar%20with%20a%20Gradient%20using%20Jetpack%20Compose%201.png](https://uploads-ssl.webflow.com/62c6fbddb12bb54622241c3d/641da56f84263a6e426a5d52_Creating%20a%20Circular%20Progress%20Bar%20with%20a%20Gradient%20using%20Jetpack%20Compose%201.png)
it provides the following challenges:
- a custom component (circular shape)
- a circular gradient color
Forking an existing component!
Having to implement such component from scratch can be intimidating. If you lack experience with Canvas
and graphics, your implementation could lead to:
- performance issues
- sub optimal maintenance
- lots of time spent to implement it!
The idea here is to stand on the shoulders of giants and derive a Jetpack Compose Component: CircularProgressBar
.
Let's try to implement it!
Step 1: Using Compose CircularProgressIndicator
This is the result using the standard CircularProgressIndicator
:
![641da627dacecd26d0416a9d_Creating%20a%20Circular%20Progress%20Bar%20with%20a%20Gradient%20using%20Jetpack%20Compose%202.png](https://uploads-ssl.webflow.com/62c6fbddb12bb54622241c3d/641da627dacecd26d0416a9d_Creating%20a%20Circular%20Progress%20Bar%20with%20a%20Gradient%20using%20Jetpack%20Compose%202.png)
limitations:
- We can’t set a gradient for the indicator color
- We can’t set a background circle (”track”) under the indicator
Step 2: Study Compose CircularProgressIndicator source code
Let's take a closer look at the source code of CircularProgressIndicator
. You can easily navigate to it, or browse the Android X source base on GitHub.
For information, the version of the file used is right here. (1.0.0-alpha11
)
CircularProgressIndicator
has slightly evolved since then, but the modification proposed should be applicable without issue.
What's really striking is how surprisingly easy the code is to follow! This is a massive improvement over the View system (for instance, the 8k+ lines of TextView).
It seems to have been written by humans who have actual problems. Like any codebase, it contains clarifying comments and even TODOs!
Now, let's duplicate the CircularProgressIndicator()
composable function and its sub-functions. Only three functions and a few modifications are needed to isolate the component and make it compile. Once again, this is good news; great job on decoupling, Compose developers!
Step 3: Add track
Adding the track can be done by using drawDeterminateCircularIndicator()
as it’s done for the Indicator.
Step 4: Discover Gradient and brush
Let’s now discover the Gradient API. We don’t try to match our design yet, just draw the circles with some gradient.
Just create a new drawCircularIndicator()
taking 2 colors instead of one, and using the brush param of drawArc()
.
The result is starting to look like our design!
Step 5: Using SweepGradient
Brush.sweepGradient()
allows to draw what is sometime called circular gradient. This is what is used in the reference design.
So far, the out of the box result is near to what is expected, but needs minor tweaks.
Step 6: Fix gradient angle
First, the gradient is supposed to be drawn from the top of the circle, not the right. Fortunately, Canvas
API provides a convient api to rotate what’s drawn:
This gives us the expected result:
Except for a small caveat: it only works when progress is 100%. Otherwise, it’s broken:
Let’s fix this!
Step 7: Fix progress angle
Here, we just need a one line change: add 90 degrees to the start angle:
Now, the progress is drawing as intended.
Step 8: Make gradient proportional to progress
Here, we want to draw the full gradient (from start color to end color) whatever the progress is. we can replace the color list with an array of Pair<Float, Color>
which gives key points to draw the gradient. We want to sync the gradient with the progress angle, so we use the sweep
value to get the end point.
Note: We could have more than two key points which would allow to use more than two colors.
And here is the final result:
Conclusions
- The Jetpack Compose components appear to have been written by actual human Android developers, and that's great!
- It's easy to fork and find inspiration in the Jetpack Compose implementation, which is open source. This is a luxury that your iOS colleagues don't have :-)
- The Canvas API is very powerful, and leveraging it should allow for the implementation of almost any custom design!
You can find the source code (with step by step commits 🤓) on which this article is based here : compose-gradient-progress-indicator.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK