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

robercoding

12/16/2022, 2:32 PM
Is there a way to change the size of an
Image(...)
outside of composition phase? 🤔
Currently I have an animation changing the size of an
Image
and I kinda dislike that it recomposes a hundred of times, do you know if that's skippable or?
I tried
drawBehind { }
and
drawWithContent
, but sadly this didn't work. I'm guessing there must be another way Edit: I don't want to go through recomposition just because I'm animating something
Is this
size
determined on
Composition
phase? I'm thinking that there should be a way to do this in
Layout
phase right? Since it determines the size & placement of the layout
z

Zach Klippenstein (he/him) [MOD]

12/16/2022, 11:40 PM
You can do it in layout or draw phases, depending on what you actually want to resize. If you want the layout node to resize, ie affect other things around the image, you’ll want to do it in layout. If you don’t need layout, you could do it with a scale transformation in draw with content.
r

robercoding

12/17/2022, 10:03 AM
Yes, that's the answer I needed! Not sure why I couldn't figure it out previously, probably I need more experience 😅 The code I've used, not sure if it can be improved but it's working!
Copy code
.layout { measurable, constraints ->
    val size = sizeAnimation.toPx()
    val placeable = measurable.measure(Constraints.fixed(size.toInt(), size.toInt()))
    layout(placeable.width,placeable.height) {
        placeable.placeRelative(0, 0)
    }
}
Thank you Zach! 🫶
108 Views