https://kotlinlang.org logo
#compose
Title
# compose
t

tylerwilson

11/09/2021, 2:23 PM
I am constructing a UI with Compose, using .dp as units. And I am also using detectTapGestures / onTap to get an offset, but it appears to be in pixels, not dp. Is there a Compose way to get the dp value from the offset value? Thank you!
c

Csaba Kozák

11/09/2021, 2:25 PM
Copy code
with(LocalDensity.current) { valueInpx.toDp() }
t

tylerwilson

11/09/2021, 2:33 PM
Perfect, works like a charm! Thank you!
g

George Mount

11/09/2021, 6:54 PM
Also,
PointerInputScope
is
Density
, so you can manipulate px<->dp within the gesture detectors.
👍 1
t

tylerwilson

11/09/2021, 6:55 PM
Well, that is certainly handy!
a

Adam Powell

11/09/2021, 7:01 PM
often you'll want to go the other direction, define in dp and convert to px for processing at the layers of layout, drawing, and input processing. It can somewhat depend on the use case but something to keep in mind
👍 1
2 Views