I've attempted to create a hexagon shape in jetpack compose but could only achieve pointed ends, tried several methods with quadraticBezierTo and arcs but couldn't achieve a result with curved ends.
I've tried to look on the web but not quite what i wanted, If you have an idea how to achieve it, thanks for answering to this post.
Here's the current code without curved ends:
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)
)
}
}
You can use
pathEffect = PathEffect.cornerPathEffect(cornerRadius)and polygon function
You can get
ImageBitmapfrom Coil and you can draw it withBlendModeas like this