我曾try 在Jetpack Compose中创建一个六边形形状,但只能达到尖端,try 了几种方法,使用Square BezierTo和圆弧,但无法实现弯曲端的效果.
我试着在网上看看,但不完全是我想要的,如果你知道如何实现它,谢谢你回复这个帖子.
以下是当前代码中没有弧形结尾的代码:
class HexagonShape : Shape {
override fun createOutline(
size: Size,
layoutDirection: LayoutDirection,
density: Density
): Outline {
return Outline.Generic(
path = drawCustomHexagonPath(size)
)
}
}
private fun drawCustomHexagonPath(size: Size): Path {
return Path().apply {
val radius = min(size.width / 2f, size.height / 2f)
val triangleHeight = (sqrt(3.0) * radius / 2)
val centerX = size.width / 2
val centerY = size.height / 2
moveTo(x = centerX, y = centerY + radius)
lineTo(x = (centerX - triangleHeight).toFloat(), y = centerY + radius / 2)
lineTo(x = (centerX - triangleHeight).toFloat(), y = centerY - radius / 2)
lineTo(x = centerX, y = centerY - radius)
lineTo(x = (centerX + triangleHeight).toFloat(), y = centerY - radius / 2)
lineTo(x = (centerX + triangleHeight).toFloat(), y = centerY + radius / 2)
close()
}
}
@Composable
fun Hexagon(modifier: Modifier = Modifier) {
Box(
modifier = modifier
.clip(HexagonShape())
.background(md_theme_light_inversePrimary)
.width(500.dp)
.height(500.dp),
contentAlignment = Alignment.Center
) {
Image(
painter = painterResource(id = R.drawable.app_bg),
contentDescription = "My Hexagon Image",
contentScale = ContentScale.Crop,
modifier = Modifier
.wrapContentSize()
.background(color = Color.Cyan)
)
}
}