Is there a way to detect the touch offset of the o...
# compose
a
Is there a way to detect the touch offset of the original bitmap not the
Image()
Composable itself?
I'm using
pointerInput
with
detectTapGestures
to get the offset for the user clicked on an
Image()
Composable as following:
Copy code
Image(
   bitmap = imageBitmap,
   contentDescription = "",
   modifier = Modifier.pointerInput(Unit){
        detectTapGestures { offset ->
           //This offset related to Image() composable          
        }
   }
)
I can get the bitmap offset only when its size of it equals the
Image()
size, but when
Image()
size becomes different than the original bitmap, I get the
Image()
offset not the bitmap offset. How I can get the offset of the original bitmap?
d
You have the bitmap size and you know how it will be used (cropped / center inside etc). The rest is just math
a
I don't set any specific scale type, however, the only thing that may change the Image size is
Modifier.weight()
, I think the full code will elaborate more:
Copy code
Column(
  verticalArrangement = Arrangement.spacedBy(12.dp),
  modifier = Modifier.fillMaxSize()
) {
   Image(
      bitmap = bitmap,
      contentDescription = "",
      modifier = Modifier
           .weight(1f)
           .pointerInput(Unit) {
                 detectTapGestures { offset -> }
            }
    )

    Divider(Modifier.background(LightGray))

    Text(text = pageNum.toString())
}
d
The default
ContentScale
is Fit. You have the size of the input area from the
PointerInputScope
the bitmap has width and height. That's all you need
a
Can you elaborate more please? Yes, I have the bitmap and Image() sizes, but my question is how to get the bitmap offset while
detectTapGestures
gives me the Image() offset
k
Let’s say your
Image
is 400x400, and your bitmap is
1000x2000
. If the scale is fit, you compute the scale factor yourself, which is 0.2 in this case, which then used to determine the position of the bitmap inside your image - the bitmap is 200x400, offset horizontally by 100. And that would give you the next step, translating coordinates of the click from image to bitmap.