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

Nikhil

09/16/2021, 5:58 PM
How can I get the center of any composable element? I was trying using
onGloballyPositioned{..}
(I also have some other code and I want to avoid adding it this modifier since it’s called a lot.) Is there any method/modifier where I can get any alternative of `Rect`/
bounds
?
d

Doris Liu

09/16/2021, 6:14 PM
What do you need the Rect/Bounds for? Maybe there are other ways to achieve what you are trying to do
n

Nikhil

09/17/2021, 7:53 AM
I need center position/coordinates of the composable element. How can I get that?
z

Zach Klippenstein (he/him) [MOD]

09/17/2021, 4:13 PM
Why do you need them?
n

Nikhil

09/18/2021, 5:33 AM
I’m trying to draw some animation from the center of the composable element.
w

Waqas Tahir

09/18/2021, 9:18 AM
this can't be done without size which you would get from onSizeChanged Modifier and then you can calculate center from onGloballyPositioned coordinates , and everytime onSizeChanged is called recalculate the center !
n

Nikhil

09/18/2021, 10:36 AM
Right. That’s what I’m trying right now with
onGloballyPositioned{..}
. I was asking if there’s any other way.
z

Zach Klippenstein (he/him) [MOD]

09/19/2021, 7:50 PM
If your animation is just drawing, you can use a drawBehind modifier (or other draw modifier) and then you have access to the size from the DrawScope
1
n

Nikhil

09/20/2021, 8:05 AM
Yeah, Thanks.