kotlin 如何在画布上居中一个弧-喷气背包组成?

ehxuflar  于 2023-03-13  发布在  Kotlin
关注(0)|答案(2)|浏览(194)

我不知道如何计算才能使这条弧线在画布上居中。有人能给我指个方向吗?

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
        )
}
qgzx9mmu

qgzx9mmu1#

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
    )

xwbd5t1u

xwbd5t1u2#

@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)
        )
    }
}

相关问题