When using `AnimatedVisibility` , is it possible f...
# compose
t
When using
AnimatedVisibility
, is it possible for the content to provide custom `Enter/ExitTransition`s, i.e. other than slide/fade/shrink/expand?
d
Yes. You could create custom enter/exit animations using the Transition object provided by AnimatedVisibilityScope: https://developer.android.com/reference/kotlin/androidx/compose/animation/AnimatedVisibilityScope?hl=nb Curious what's the custom enter/exit transition you have in mind? 🙂
🙏🏼 1
t
Oh nice, thanks! Will look into that. I have an existing
Animatable<Offset>
& an
Animatable<Float>
that I want to simultaneously update when the component enters/exits…although I feel like the
Transition
framework is different from these singular `Animatable`s 🤔
d
although I feel like the 
Transition
 framework is different from these singular `Animatable`s
Depending on what you use the `Animatable`s for. If they are purely for defining an initial value to animate from, the Transition from AnimatedVisibilityScope would be more convenient, because it asks you for the target values for preEnter, Visible, and postExit states, and automatically kicks off the transition preEnter -> Visible as a form of enter transition, similarly Visible -> postExit for exit transition. Give it a try, I'd love to know how it works for your case. We are looking at other possibilities for expanding Enter/ExitTransition support as well. 🙂
💡 1
t
I see, that makes sense. In this use case, there is a draggable element that can be dragged away and can automatically snap back to its original position, all currently controlled by an
Animatable<Offset>
. If the element enters/exits, theoretically wanted to use that same
Animatable<Offset>
to animate a slide-in/slide-out starting at the `Animatable<Offset>`’s current X/Y, for example.
i.e. when the component is declared, it’s
offset
modifier is already being defined by that `Animatable<Offset>`:
Copy code
offset {
    IntOffset(
        x = animatableOffset.value.x.roundToInt(),
        y = animatableOffset.value.y.roundToInt()
    )
}
d
Can the Animatable and the AnimatedVisibility offset be entirely independent, so that the visual offset is a combination of the two?
💡 1
t
Thats an interesting idea. So the component’s actual Offset could be something like a sum of an
Animatable<Offset>
and a
State<Float>
from
transition.animateFloat(…)
d
Yea, having them in separate offset modifier would make it easier to manage, unless one offset depends on the other. This is a new usage pattern in Compose - in the View world, we'd have multiple sources of truth for the translationX/Y. Here having a chain of two offset modifiers is a new option in Compose. 😄
👌 1
t
this is great, I didn’t even consider to chain the offset modifiers (some part of me thought it wasn’t possible or one would overwrite the other)
d
That's probably the result of too much experience with Views. 🤣
😖 1
t
Just tried by using a built-in `slideIn`/`slideOut`
Enter/ExitTransition
and that seems to work. So the component can be controlled by two separate “offsets”…. Thanks for this insight, will run with it and see how it goes 😄
👍 2