visit
#90A4AE
#4DB6AC
#FFFFFF
Jetpack Compose provides us with a composable function called drawArc()
. This function requires 4 parameters to be declared:
brush: Brush
Color or fill to be applied to the arcstartAngle: Float
Starting angle in degrees. 0 represents 3 o'clocksweepAngle: Float
Size of the arc in degrees that is drawn clockwise relative to startAngle
useCenter: Boolean
Flag indicating if the arc is to close the center of the boundThere are other parameters that offer you more customization, but they are not required, so I will skip over them for now.
As the startAngle's 0 represents 3'o clock, I want to start drawing somewhere close to 7'o clock. That is the reason we use 140 for the startAngle
.
A circle in its simplest form is also an arc, whose sweep angle is 360. So you can use the same drawArc API to draw a circle. However, Jetpack Compose provides us with another convenient API to draw a circle in an intuitive manner. The method is called drawCircle
. It requires only one parameter to draw and that is the color.
As you can see it added a circle at the center of your canvas and the radius is half the size of your canvas. So now you have to fix both of these things. First, go ahead and set the radius to 5f.
Great job resizing the circle! 👏
Now we need to position the circle onto the arc so that it matches your desired design. To achieve this, you have to provide the coordinates of the center of the circle as an Offset
object. Go ahead and set this as (0,0) and see what happens.
You did not expect that to happen, did you? Do you know why this happened? If you are paying attention, you'd be thinking but why does the circle not appear on the extreme top, left of your canvas? This is because you added 10 dp, padding to your canvas. Try removing the padding modifier and see what happens. Android Studio can even show this is to you, just hover over your composable's design preview and it will show you a bounding box like this
In the next session, you'll see the code to position the circle correctly on the arc.