I am using Canvas Api to draw something. I want to draw line with rounded corner. Line is draw without any problem. But I cannot figure out which attribute for corner radius.
我在用帆布阿皮画东西。我想画圆角的线。划线没有任何问题。但我想不出拐角半径的属性是什么。
val boxSize = 30.dp
Box(modifier = Modifier
.background(Color.LightGray)
.height(height = boxSize)
) {
Canvas(
modifier = Modifier
.fillMaxWidth()
) {
val canvasWidth = size.width
drawLine(
start = Offset(x = 0f, y = (boxSize / 2).toPx()),
end = Offset(x = canvasWidth, y = (boxSize / 2).toPx()),
color = Color.Black,
strokeWidth = 8.dp.toPx()
)
}
}
My view is simple without corner radius.
我的观点很简单,没有拐角半径。
I want my Black line to be corner for each side with specific radius.
我希望我的黑线是角落的每一边与特定的半径。
更多回答
优秀答案推荐
You need to add the cap
argument to drawLine
and set it to StrokeCap.Round
.
您需要将上限参数添加到DrawLine,并将其设置为StrokeCap.round。
drawLine(
start = Offset(x = 0f, y = (boxSize / 2).toPx()),
end = Offset(x = canvasWidth, y = (boxSize / 2).toPx()),
color = Color.Black,
strokeWidth = 8.dp.toPx(),
cap = StrokeCap.Round, //add this line for rounded edges
)
Try this
尝尝这个
Box(
modifier = Modifier.padding(top = 4.dp, bottom = 4.dp).width(40.dp).height(3.dp).clip(shape = RoundedCornerShape(size = 5.dp)).background(Color.Red)
)
更多回答
How can I specific the radius of cap?
我怎样才能确定盖子的半径呢?
@VivekModi in this case you can't. It is based on the strokeWidth of the line.
@VivekModi在本例中不能。它基于行的strokeWidth。
我是一名优秀的程序员,十分优秀!