可以在android上制作圆角六边形形状

问题描述 投票:0回答:1

我想做圆角,但我不知道该怎么做。我尝试使用cubicTo、arcTo 等。

我现在的身材

我的代码来绘制这个

fun Path.customHexagon(radius: Float, size: Size) {
    val triangleHeight = (sqrt(3.0) * radius / 2)
    val centerX = size.width / 2
    val centerY = size.height / 2

    moveTo(centerX, centerY + radius)
    lineTo((centerX - triangleHeight).toFloat(), centerY + radius/2)
    lineTo((centerX - triangleHeight).toFloat(), centerY - radius/2)
    lineTo(centerX, centerY - radius)
    lineTo((centerX + triangleHeight).toFloat(), centerY - radius/2)
    lineTo((centerX + triangleHeight).toFloat(), centerY + radius/2)

    close()
}

也许你知道任何具有六边形形状的 android/kotlin 库?

帮助编写代码或链接到带有 Android 形状的库

android kotlin android-jetpack-compose android-canvas
1个回答
0
投票

this类似,您可以在这里重复:

@Composable
fun Hexagon(
    modifier: Modifier = Modifier
) {
    Canvas(
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(1f)
    ) {
        val size = 300f
        val trianglePath = Path().apply {
            val triangleHeight = (sqrt(3.0) * size / 2)
            val centerX = [email protected] / 2
            val centerY = [email protected] / 2

            moveTo(centerX, centerY + size)
            lineTo((centerX - triangleHeight).toFloat(), centerY + size / 2)
            lineTo((centerX - triangleHeight).toFloat(), centerY - size / 2)
            lineTo(centerX, centerY - size)
            lineTo((centerX + triangleHeight).toFloat(), centerY - size / 2)
            lineTo((centerX + triangleHeight).toFloat(), centerY + size / 2)
            close()
        }

        drawIntoCanvas { canvas ->
            canvas.drawOutline(
                outline = Outline.Generic(trianglePath),
                paint = Paint().apply {
                    color = Color.Black
                    pathEffect = PathEffect.cornerPathEffect(50f)
                }
            )
        }
    }
}

hexagon

© www.soinside.com 2019 - 2024. All rights reserved.