![](/img/trans.png)
[英]How to draw a multicolored bar with Canvas in Jetpack Compose?
[英]Draw lines at an angle in jetpack compose without a canvas
所以我想在jetpack compose中從A點到B點畫一條線。
有沒有辦法在沒有畫布的情況下做到這一點,因為畫布不會真正適用於我想做的事情
如果你想在其他可組合元素后面繪制,你可以使用drawBehind
修飾符。
如果你想在后面和前面都畫,你可以使用drawWithContent
修飾符。
如果您還想盡可能多地緩存結果,可以使用drawWithCache
修飾符。
使用drawWithCache
修飾符的內容后面的行和內容前面的行的示例
@Composable
fun DrawWithCacheExample() {
val width = 400.dp
val height = 200.dp
var offsetX by remember { mutableStateOf(0f) }
Box(
modifier = Modifier
.size(width, height)
.border(1f.dp, Color.Black, RectangleShape)
.drawWithCache {
onDrawWithContent {
// draw behind the content
drawLine(Color.Black, Offset.Zero, Offset(width.toPx(), height.toPx()), 1f)
// draw the content
drawContent()
// draw in front of the content
drawLine(Color.Black, Offset(0f, height.toPx()), Offset(width.toPx(), 0f), 1f)
}
}
) {
Box(modifier = Modifier
.size(width / 2, height / 2)
.offset { IntOffset(offsetX.roundToInt(), (height / 4).roundToPx()) }
.background(Color.Yellow)
.draggable(
orientation = Orientation.Horizontal,
state = rememberDraggableState { delta ->
offsetX += delta
}
)
)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.