Jetpack Compose 路径将绘制在 canvas 边界之外
Jetpack Compose Path will draw outside canvas bounds
我有一个签名框,在你签名的那一刻,可以在canvas外面画画。我需要路径保持在 canvas 的范围内。我可以在捕获路径时手动执行此操作,但我认为可能有一种自动方法。
Canvas(modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.border(1.dp, MaterialTheme.colors.primaryVariant, shape = RoundedCornerShape(4.dp))
.pointerInput(Unit) {
detectDragGestures(onDragStart = {
touchMove(path, it.x, it.y, -1f, -1f, true)
}) { change, _ ->
change.consumeAllChanges()
touchMove(
path,
change.position.x,
change.position.y,
change.previousPosition.x,
change.previousPosition.y,
false
)
}
}) {
canvasWidth = size.width
canvasHeight = size.height
drawPath(path, color = Color.Blue, style = Stroke(width = 4f))
}
亲爱的我。写完后想通了。
Modifier.clipToBounds()
奇怪的是,在任何示例中都没有提到这一点。我认为这与正常 android 剪辑的行为相反。
我有一个签名框,在你签名的那一刻,可以在canvas外面画画。我需要路径保持在 canvas 的范围内。我可以在捕获路径时手动执行此操作,但我认为可能有一种自动方法。
Canvas(modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.border(1.dp, MaterialTheme.colors.primaryVariant, shape = RoundedCornerShape(4.dp))
.pointerInput(Unit) {
detectDragGestures(onDragStart = {
touchMove(path, it.x, it.y, -1f, -1f, true)
}) { change, _ ->
change.consumeAllChanges()
touchMove(
path,
change.position.x,
change.position.y,
change.previousPosition.x,
change.previousPosition.y,
false
)
}
}) {
canvasWidth = size.width
canvasHeight = size.height
drawPath(path, color = Color.Blue, style = Stroke(width = 4f))
}
亲爱的我。写完后想通了。
Modifier.clipToBounds()
奇怪的是,在任何示例中都没有提到这一点。我认为这与正常 android 剪辑的行为相反。