rememberZoomState
为我用画布绘制的矩形添加缩放功能。然而,当我放大其中一个时,它会放大所有它们,而我们的想法是单独缩放它们。
我知道我可以使用
pointerInput
做类似的事情,但想法是使用该库。
这可能吗?
我的代码:
@Composable
internal fun ScreenShotDrawSpeech(
bubbleDomain: BubbleDomain,
modifier: Modifier = Modifier,
) {
val textMeasurer = rememberTextMeasurer()
val zoom = rememberZoomableState()
Canvas(
modifier = modifier
.fillMaxSize()
.background(background_overlay)
.zoomable(zoom),
onDraw = {
bubbleDomain.predictions.forEach { prediction ->
val left = prediction.x - prediction.width / 2
val top = prediction.y - prediction.height / 2
val right = left + prediction.width
val bottom = top + prediction.height
val rect = Rect(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
drawSpeechBoundingBox(text = prediction.confidence.toString(), boundingBox = rect, textMeasurer = textMeasurer)
}
},
)
}
我认为使用
rememberZoomState()
不可能实现这一点。当您将 zoomable()
修改器应用于画布时,整个画布将发生变化,而不是其中绘制的各个内容。 zoomable()
修改器,但是这是不可能的,因为我们在 Canvas 的 onDraw
函数中没有可组合项,也没有可组合范围。
如果可以用可组合项替换您在 Canvas 中绘制的元素,例如
RectangleComposable
、SpeechBubbleComposable
,那么您可以将这些可组合项绘制到 Box 中,并将 zoomable
修饰符添加到每个单独的可组合项中。
但如果你需要一个画布,我想正如你已经说过的,你将不得不使用
pointerInput
,如这个stackoverflow答案中所述。