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

alexsullivan114

03/08/2021, 7:22 PM
More questions! I'm struggling to get my brain to understand animations in declarative land. I've been looking at the animation codelab which is helpful, but I'm still struggling. Let's say I have some immutable composable and I want to animate changes to it whenever its recomposed with a new value. Is that possible? Is that something that you'd do at the immutable composable layer or at some wrapper layer?
l

louiscad

03/08/2021, 7:25 PM
You can do it right in the composable. This video should help before reading the doc on it:

https://youtu.be/7yY2OocGiQU

a

alexsullivan114

03/08/2021, 7:30 PM
Thanks!
d

Doris Liu

03/08/2021, 7:36 PM
The video above is a great resource! And this tutorial too if you haven't already seen it: https://developer.android.com/jetpack/compose/animation
Let's say I have some immutable composable and I want to animate changes to it whenever its recomposed with a new value. Is that possible?
Yes
Is that something that you'd do at the immutable composable layer or at some wrapper layer?
Both are valid. Let me find you some examples.
In the sample code here, you can see a simple example where the composable itself contains the logic for how to interpret the incoming parameter to the composable (i.e.
ColorAnimation
composable) and convert that to a target color value, and animate that change. From the `Box`'s perspective in this example, the animation is done somewhere else, and it's just consuming the animation value.
a

alexsullivan114

03/08/2021, 7:50 PM
@Doris Liu Oh wow that kind of blows my mind! I would've thought that like since a new parameter is coming in we wouldn't be able to animate based off that because we're rebuilding the composable, state and all. But this is perfect, thank you!
d

Doris Liu

03/08/2021, 7:58 PM
Haha, it is a new parameter, but the animation composable is stateful - it creates an animation object and positionally memoizes the animation object. So all the incoming target value changes (through the animation composable) are applied to the same animation object that already tracks the value and velocity change. 🙂
a

alexsullivan114

03/08/2021, 7:59 PM
mind blown Amazing.
Thank you!
👍 1
2 Views