Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the size of the Image composable without triggering recomposition

I have an animateDpAsState(..), whenever this animation is triggered it changes the Modifier.size(value) of an Image(...) thus causing recomposition.

Is there a way to skip composition phase for this specific scenario? Allowing an image to change its size?

like image 779
Barrufet Avatar asked Oct 20 '25 02:10

Barrufet


1 Answers

I found the solution! To skip recomposition but still affect things around the layout, you can do it do it in layout phase, otherwise move it to Draw phase!

Apply this Modifier to the Image(...) modifier parameter.

Modifier.layout { measurable, constraints ->
    val size = animationSize.toPx() // getting the size of the current animation
    val placeable = measurable.measure(Constraints.fixed(size.toInt(), size.toInt())) // setting the actual constraints of the image

    // Set the layout with the same width and height of the image.
    // Inside the layout we will place the image. This layout function is like a "box"
    layout(placeable.width,placeable.height) {
        // And then we will place the image inside the "box"
        placeable.placeRelative(0, 0) 
    }
}
like image 118
Barrufet Avatar answered Oct 22 '25 05:10

Barrufet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!