Is there a way to get the absolute layout position...
# compose
f
Is there a way to get the absolute layout position in
Layout
? the use case is to animate a component moving from one part of the screen to another.
h
What do you mean by absolute layout position? Do you mean the position relative to window? Relative to parent?
a
I'd like to know this as well. I was thinking about constraint layout: it allows you to set barriers an guidelines so it maybe possible to animate to one of those. I'm not sure if that would work, however, because there's no a continuum of values, i.e. numbers, in constraint layout properties.
If you had a box layout, perhaps setting the offsets would give you positions to which you could animate towards.
f
What do you mean by absolute layout position? Do you mean the position relative to window? Relative to parent?
Relative to root or window, I will compare the position using a
LayoutCoordinates
which has
boundsInWindow()
and
boundsInRoot
, so window will be good.
was thinking about constraint layout:
That was my first attempt but it is too limiting. I can't reliably set the velocity of the animation.
If you had a box layout, perhaps setting the offsets would give you positions to which you could animate towards.
what do you mean?
It is also possible to do a hack à la
Copy code
@Composable
fun Hack(content: @Composable () -> Unit) {
    var coords: LayoutCoordinates? by remember{ mutableStateOf(null)}
    Layout(content = content, measurePolicy = { measureables, constraints ->
        if(coords != null){
            // Use coords
        } else layout(constraints.maxWidth, constraints.maxHeight) {}
        
    }, modifier = Modifier.onGloballyPositioned { coords = it })
}