This article was translated from Japanese by Claude Code.
A note about something I investigated a bit in my job.
One way to animate Views in Android is using Transition.
Transition#
For an overview of Transition, refer to the official documentation, or the slides by yaraki below.
Transition is a convenient API that saves and compares View states before and after animation, and generates and executes Animators accordingly.
For example, executing code like the following will fade the display/hide of binding.target View:
TransitionManager.beginDelayedTransition(binding.root) // Specify the root ViewGroup
binding.target.isVisible = !binding.target.isVisible // Update the rendering state of the View you want to animateThe beginDelayedTransition function is somewhat magical—after executing it, when you toggle View visibility, it automatically animates.
In the example above, animation is achieved using the Transition class set as the default internally. Of course, you can also define your own custom Transition class and pass it to achieve different animations.
val myTransition = MyTransition()
TransitionManager.beginDelayedTransition(binding.root, myTransition) // Pass Transition as the second argumentAdding a Listener#
A common pattern is wanting to execute some process after animation finishes.
If you want to perform processing when a Transition ends, use the addListener function of the Transition class to add a Listener.
Below is the internal implementation of the Transition class’s addListener function:
/**
* Adds a listener to the set of listeners that are sent events through the
* life of an animation, such as start, repeat, and end.
*
* @param listener the listener to be added to the current set of listeners
* for this animation.
* @return This transition object.
*/
@NonNull
public Transition addListener(@NonNull TransitionListener listener) {
if (mListeners == null) {
mListeners = new ArrayList<>();
}
mListeners.add(listener);
return this;
}If you define a custom Transition, you can add a Listener by calling the addListener function during Transition initialization:
val myTransition = MyTransition().addListener(object : Transition.TransitionListener() {
... // Override functions
})
TransitionManager.beginDelayedTransition(binding.root, myTransition)But what if you haven’t defined a custom Transition (i.e., you don’t pass a Transition class to the beginDelayedTransition function)?
In such cases, you can initialize the default Transition class that the Transition class holds internally and add a Listener to it.
The default Transition class is called AutoTransition. Reading the internal implementation, you can see it’s defined as a private static field in the TransitionManager class:
public class TransitionManager {
...
private static Transition sDefaultTransition = new AutoTransition();So initializing and using the AutoTransition class works fine:
val transition = AutoTransition().addListener(...)
TransitionManager.beginDelayedTransition(binding.root, transition)Tips#
While you can directly pass the TransitionListener class to the addListener function, you can also pass the TransitionListenerAdapter class which implements this Listener. Adapter classes are just classes that override the interface class (with no custom processing). Similar definitions exist for Listeners other than TransitionListener.
Since Adapter classes implement all functions defined in the interface with empty bodies, using the Adapter class instead allows you to override only the functions you want to override from those defined in the Listener through the Adapter class.
The final code looks like this:
val transition = AutoTransition()
.addListener(object : TransitionListenerAdapter() {
override fun onTransitionEnd(transition: Transition) {
// do something
}
})
TransitionManager.beginDelayedTransition(binding.root, transition)
binding.target.isVisible = !binding.target.isVisibleSummary#
When animating Views in Android, the Transition API is one option
When you want to execute processing at specific timings with Transition API animations, use the addListener function of the Transition class
The default Transition class provided is AutoTransition
The TransitionListenerAdapter class is provided for the TransitionListener interface