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

eygraber

02/11/2021, 9:00 PM
How would I find out the measurements for a specific node? For example, if I want to load an image from a remote service that accepts a width and height parameter, and I want to set that width and height to match the layout, how can I get that information?
k

Kirill Grouchnikov

02/11/2021, 9:01 PM
What is the "source" of truth? Do you allocate some space in your layout for the image and then hit the network to fetch the downscaled version of the image that matches that size?
Or do you wait for the image to download and then determine how much space it gets on the screen based on its aspect ratio?
e

eygraber

02/11/2021, 9:04 PM
The source of truth would be the space allocated in the layout, but my understanding of compose is that there's a constraint system that makes it dynamic/responsive so I would have to wait for the actual layout to happen to get the pixel values
a

Adam Powell

02/11/2021, 9:07 PM
Modifier.onSizeChanged {}
will be considerably less overhead than
BoxWithConstraints
if all you need is the measured size of the element.
BoxWithConstraints
is good if you would change the actual structure of the content based on the incoming layout constraints.
👍 1
i

Ian Lake

02/11/2021, 9:07 PM
You might also take a look at accompanist, which has image loading utilities for Coil, Glide, and Picasso which does this type of thing for you: https://github.com/chrisbanes/accompanist
e

eygraber

02/11/2021, 9:12 PM
I was looking at something like accompanist but I need the size in order so that I can build the url with the width and height parameters.
Modifier.onSizeChanged
looks like it will provide what I need. Thanks!
i

Ian Lake

02/11/2021, 9:20 PM
That's exactly what accompanist provides in its
requestBuilder
(for Coil in this instance) - the ability to change the
data
(i.e., the URI) based on the size you're passed: https://github.com/chrisbanes/accompanist/blob/main/coil/src/main/java/dev/chrisbanes/accompanist/coil/Coil.kt#L108
It uses
onSizeChanged
under the hood so it is good to know about that as a tool in your toolbox, but image loading isn't something that every dev should have to write from scratch 😛
e

eygraber

02/11/2021, 9:25 PM
Oh interesting, I'm not sure how I missed that. Thanks! Now I just need Coil to become multiplatform and it'll meet all of my use cases 😁
2 Views