Android Jetpack Compose-点击坐标don';不匹配矩形坐标
所以我尝试在选择一个矩形时检测触摸事件。不幸的是,当我单击矩形时,我的x、y坐标与矩形坐标不匹配,因此它从未被选中 这是我的代码:Android Jetpack Compose-点击坐标don';不匹配矩形坐标,android,kotlin,android-canvas,android-jetpack-compose,Android,Kotlin,Android Canvas,Android Jetpack Compose,所以我尝试在选择一个矩形时检测触摸事件。不幸的是,当我单击矩形时,我的x、y坐标与矩形坐标不匹配,因此它从未被选中 这是我的代码: Image(bitmap = imageBitmap, "New Image", Modifier .pointerInput(Unit) { detectTapGestures( onPress = { it ->
Image(bitmap = imageBitmap, "New Image", Modifier
.pointerInput(Unit) {
detectTapGestures(
onPress = { it ->
val x = it.x
val y = it.y
println("x = $x and y = $y")
for ((index, value) in updatedList.value?.withIndex()!!) {
val rect = RectF(
value.offset.x,
value.offset.y,
value.offset.x + value.size.width,
value.offset.y + value.size.height
)
println("left = ${rect.left}")
println("top = ${rect.top}")
println("right = ${rect.right}")
println("bottom = ${rect.bottom}")
for (i in index until updatedList.value!!.size) {
if (x > rect.left
&& x < rect.right
&& y > rect.top
&& y < rect.bottom
) {
println("Touched at index $index")
}
}
}
},
)
}
知道我可能做错了什么吗?什么是updatedList?它是一个正在观察的liveData值。基本上,只要添加一个新的矩形,列表就会更新。列表如下所示:``rectList.add(RectData(size=size(word.length.toFloat(),textBounds.height().toFloat()),offset=offset(xCoordinate.toFloat(),yccoordinate.toFloat()))viewModel.onListChanged(rectList)``好的,你的问题给我指出了正确的方向。基本上:word.length不是像素值。谢谢。
I/System.out: x = 339.96582 and y = 192.96289
I/System.out: left = 215.0
I/System.out: top = 210.0
I/System.out: right = 220.0
I/System.out: bottom = 280.0