Ahaisting
10/05/2022, 7:36 PM.animateContentSize()
works when expanding content, but not when contracting/shrinking it?
details and example in thread 🧵👇Ahaisting
10/05/2022, 7:37 PM@ExperimentalAnimationApi
@Composable
private fun AnimateTest() {
var expanded by remember { mutableStateOf(true) }
Column(
Modifier
.background(Color.Red)
.fillMaxSize()
) {
Box(
Modifier
.animateContentSize()
.height(if (expanded) 200.dp else 100.dp)
.background(Color.Blue)
.fillMaxWidth()
.clickable { expanded = !expanded }
)
}
}
Casey Brooks
10/05/2022, 7:52 PMheight
to 200dp when expanded, the Composable is given up to 200dp to measure itself, and animateContentSize()
can then change the actual measurement over the animation duration until it hits the value height()
.
But when going the other way, the height
is constrained to a max of 100, and so even though the animation wants to back the height down slowly, every animated value will always be higher the the max allotted by height()
.
Maybe use animateDpAsState()
instead for animating between 2 specific values, or change the height of the child of Box()
instead of the parent box with animateContentSize()
?Ahaisting
10/05/2022, 7:58 PManimateDpAsState()
and using a child Box
) work as solutions!Ahaisting
10/05/2022, 7:59 PMCasey Brooks
10/05/2022, 8:07 PMrequiredSize
modifier” https://developer.android.com/jetpack/compose/modifiers#padding-and-size
So Modifier.size()
works by setting constraints on the children, but doesn’t set specific measured values (which is why the animation can override the measured size as long as it is less than the constraint). I suspect if you changed size
to requiredSize
in the original example, you’d get the same “jump” behavior going in both directionsDoris Liu
10/05/2022, 8:43 PManimateContentSize
is animating the clip bounds around the child layout (i.e. Modifer.height(...) in your example). When the child snaps to a larger size, you notice the clip bounds growing to reveal more and more of the child. However, when the child snaps to a smaller size, the clip bounds animation may or may not be visually noticeable depending on your setup. If you have a sibling in this Column under the Box, you'll see the effect of the clip bounds change slowly pushing down or bringing up the sibling.
In this case since you have the initial/target size predefined, I'd go with Casey's suggestion on using animateDpAsState
Ahaisting
10/07/2022, 1:13 AM