如何在 Jetpack Compose 中将弧线居中于画布?

3

我不确定如何计算以使该弧形居中于画布?有人能指引我正确的方向吗?

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
        )
}
2个回答

5

drawArc方法中使用topLeft参数。

像这样:

    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


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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接