3

I'm not sure how can I do the calculation in order to center this arc on the canvas? Can someone point me in the proper direction?

Canvas(modifier = Modifier
    .background(Color.LightGray)
    .fillMaxWidth()
    .height(300.dp)
) {
        drawArc(
            color = Color.Blue,
            startAngle = 30f,
            sweepAngle = 300f,
            useCenter = false,
            style = Stroke(width = 50f, cap = StrokeCap.Round),
            size = size/2.25F
        )
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Stefan
  • 2,829
  • 5
  • 20
  • 44

2 Answers2

5

Use the topLeft parameter in the drawArc method.

Something like:

    val sizeArc = size/2.25F
    drawArc(
        color = Color.Blue,
        startAngle = 30f,
        sweepAngle = 300f,
        topLeft = Offset((size.width - sizeArc.width)/2f,(size.height - sizeArc.height)/2f),
        useCenter = false,
        style = Stroke(width = 50f, cap = StrokeCap.Round),
        size = sizeArc
    )

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1
@Composable
fun CustomArc() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        val arcRadius = 200f
        val canvasWidth = size.width
        val canvasHeight = size.height

        drawArc(
            color = Color.Red,
            startAngle = -90f, //start angle is always in clockwise direction
            sweepAngle = 270f, // angle formed between the start angle
            useCenter = false,
            size = Size(arcRadius, arcRadius),
            topLeft = Offset(
                (canvasWidth / 2) - (arcRadius / 2),
                canvasHeight / 2 - (arcRadius / 2)
            ),
            style = Stroke(width = 10f, cap = StrokeCap.Round)
        )
    }
}

enter image description here

Chinmay
  • 354
  • 4
  • 10