jvm 在Jetpack Compose Koltin中绘制椭圆形

x3naxklr  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(166)

我想创建或绘制椭圆形。
我已经做了如下

class CustomOvalShape : Shape {
    override fun createOutline(
        size: androidx.compose.ui.geometry.Size,
        layoutDirection: LayoutDirection,
        density: androidx.compose.ui.unit.Density
    ): Outline {
        val path = Path().apply {
            val rect = Rect(0f, 0f, size.width, size.height)
            addOval(rect)
        }
        return Outline.Generic(path)
    }
}

我正在使用jetpack composeKotlin,当我使用above class时,我得到了如下错误:

Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option

我该怎么解决这个问题?

dly7yett

dly7yett1#

使用KotlinCompose绘制椭圆形,如下所示-

@Composable
fun OvalShape() {
    Canvas(modifier = Modifier.fillMaxSize()) {
        val centerX = size.width / 2
        val centerY = size.height / 2
        val radiusX = size.width / 2
        val radiusY = size.height / 2

        drawOval(
            color = Color.Blue,
            topLeft = Offset(centerX - radiusX, centerY - radiusY),
            size = Size(radiusX * 2, radiusY * 2),
            style = Stroke(width = 2.dp.toPx())
        )
    }
}

相关问题