I've found the solution.
Modifier.clipToBounds
won't work, but Modifier.clip(shape)
does.
@Composable
fun Line() {
var top by remember { mutableStateOf(false) }
val shape = if (top) {
LineShapeTop
} else {
LineShapeBottom
}
Box(
Modifier
.background(color = Color.Blue, shape = shape)
.clip(shape)
.fillMaxSize()
.clickable { top = !top }
)
}
object LineShapeTop : Shape {
const val strokeWidth = 16.0f
override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density): Outline {
val path = Path().apply {
moveTo(0f, 0f)
lineTo(size.width/2 + strokeWidth, 0f)
lineTo(size.width/2 + strokeWidth, size.height- strokeWidth)
lineTo(size.width, size.height- strokeWidth)
lineTo(size.width, size.height)
lineTo(size.width/2, size.height)
lineTo(size.width/2, strokeWidth)
lineTo(0f, strokeWidth)
close()
}
return Outline.Generic(path)
}
}
object LineShapeBottom : Shape {
const val strokeWidth = 16.0f
override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density): Outline {
val path = Path().apply {
moveTo(0f, size.height)
lineTo(size.width/2 + strokeWidth, size.height)
lineTo(size.width/2 + strokeWidth, strokeWidth)
lineTo(size.width, strokeWidth)
lineTo(size.width, 0f)
lineTo(size.width/2, 0f)
lineTo(size.width/2, size.height- strokeWidth)
lineTo(0f, size.height- strokeWidth)
close()
}
return Outline.Generic(path)
}
}

