Moh*_*mad 10 android canvas rounded-corners kotlin android-jetpack-compose
我正在尝试在 Jetpack Compose 中使用创建圆角三角形Canvas。
我尝试用这段代码来绘制三角形:
@Composable
fun RoundedTriangle() {
Canvas(modifier = Modifier.size(500.dp)) {
val trianglePath = Path().apply {
val height = size.height
val width = size.width
moveTo(width / 2.0f, 0f)
lineTo(width, height)
lineTo(0f, height)
}
drawPath(trianglePath, color = Color.Blue)
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何圆化三角形的角。我也尝试过使用arcTo,但无法得到合适的结果。
我怎样才能画出像下图这样的东西?
Phi*_*hov 15
您可以Stroke像这样指定舍入:
drawPath(
...
style = Stroke(
width = 2.dp.toPx(),
pathEffect = PathEffect.cornerPathEffect(4.dp.toPx())
)
)
Run Code Online (Sandbox Code Playgroud)
但似乎Fill缺乏支持四舍五入。我创建了一个功能请求,请为其加注星标。
但 Canvas 有drawOutline函数,它接受两个Outline,它可以包装Path, 和Paint,您可以为其指定pathEffect:
Canvas(modifier = Modifier.fillMaxWidth().aspectRatio(1f)) {
val rect = Rect(Offset.Zero, size)
val trianglePath = Path().apply {
moveTo(rect.topCenter)
lineTo(rect.bottomRight)
lineTo(rect.bottomLeft)
close()
}
drawIntoCanvas { canvas ->
canvas.drawOutline(
outline = Outline.Generic(trianglePath),
paint = Paint().apply {
color = Color.Black
pathEffect = PathEffect.cornerPathEffect(rect.maxDimension / 3)
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
Path帮手:
fun Path.moveTo(offset: Offset) = moveTo(offset.x, offset.y)
fun Path.lineTo(offset: Offset) = lineTo(offset.x, offset.y)
Run Code Online (Sandbox Code Playgroud)
结果:

| 归档时间: |
|
| 查看次数: |
7192 次 |
| 最近记录: |