【问题标题】:How to Center an Arc on the Canvas - Jetpack Compose?如何使画布上的圆弧居中 - Jetpack Compose?
【发布时间】:2021-09-07 11:51:35
【问题描述】:

我不确定如何进行计算才能使这条弧线在画布上居中?有人能指出我正确的方向吗?

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

【问题讨论】:

    标签: android kotlin android-canvas android-jetpack-compose


    【解决方案1】:

    在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
        )
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:
    @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)
            )
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多