I need to animate scale up image (and then scale d...
# compose
l
I need to animate scale up image (and then scale down) and after this animation finishes, i need to change image drawable and change its tint color. Getting lost in so many options https://developer.android.com/jetpack/compose/animation#animatable. Please help
j
hey Luka, I’ve done exactly this using
updateTransition()
and an enum to represent the animation state in your situation you could do
Copy code
enum class MyAnimState {
    Initial,
    ScaledUp,
    ScaledDown,
    Finished;

    fun next() = when (this) {
        Initial -> ScaledUp
        ScaledUp -> ScaledDown
        ScaledDown -> Finished
        Finished -> Initial
    }
}
using this and a when statement for each of your animatable values you can define values for each state, and by having something like:
Copy code
if (transition.targetState == transition.currentState) {
    myAnimState = myAnimState.next()
}
you can automatically progress to the “next” state once each one has finished
l
Thx for help. Now i can scale image with val scale by transition.animateFloat( …) and change tint with
Copy code
val color by transition.animateColor(...)
How can i animate change of painterResource based on MyAnimState (image swap)?
j
you can do a simple image swap by checking your
targetState
in an AnimatedContent that is a parent of your image