简体   繁体   English

在 Jetpack Compose Koltin 中绘制一个椭圆形

[英]Draw an oval shape in Jetpack Compose Koltin

I want to create or draw the oval shape.我想创建或绘制椭圆形。

I have done as below我做了如下

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

I am using jetpack compose kotlin and when I am using above class I am getting an error as below:我正在使用 jetpack compose kotlin,当我使用上面的 class 时,出现如下错误:

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

How can I fix this?我怎样才能解决这个问题?

draw an oval shape using Kotlin Compose as below-使用 Kotlin 绘制一个椭圆形 Compose 如下 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM