WindowInsetsAnimationCompat.Callback

Added in 1.5.0

public abstract class WindowInsetsAnimationCompat.Callback


Interface that allows the application to listen to animation events for windows that cause insets.

Summary

Constants

static final int

Return value for getDispatchMode: Dispatching of animation events should continue in the view hierarchy.

static final int

Return value for getDispatchMode: Dispatching of animation events should stop at this level in the view hierarchy, and no animation events should be dispatch to the subtree of the view hierarchy.

Public constructors

Callback(int dispatchMode)

Creates a new WindowInsetsAnimationCompat callback with the given dispatch mode.

Public methods

final int

Retrieves the dispatch mode of this listener.

void

Called when an insets animation has ended.

void

Called when an insets animation is about to start and before the views have been re-laid out due to an animation.

abstract @NonNull WindowInsetsCompat
onProgress(
    @NonNull WindowInsetsCompat insets,
    @NonNull List<WindowInsetsAnimationCompat> runningAnimations
)

Called when the insets change as part of running an animation.

@NonNull WindowInsetsAnimationCompat.BoundsCompat

Called when an insets animation gets started.

Constants

DISPATCH_MODE_CONTINUE_ON_SUBTREE

Added in 1.5.0
public static final int DISPATCH_MODE_CONTINUE_ON_SUBTREE = 1

Return value for getDispatchMode: Dispatching of animation events should continue in the view hierarchy.

DISPATCH_MODE_STOP

Added in 1.5.0
public static final int DISPATCH_MODE_STOP = 0

Return value for getDispatchMode: Dispatching of animation events should stop at this level in the view hierarchy, and no animation events should be dispatch to the subtree of the view hierarchy.

Public constructors

Callback

Added in 1.5.0
public Callback(int dispatchMode)

Creates a new WindowInsetsAnimationCompat callback with the given dispatch mode.

Parameters
int dispatchMode

The dispatch mode for this callback. See getDispatchMode.

Public methods

getDispatchMode

Added in 1.5.0
public final int getDispatchMode()

Retrieves the dispatch mode of this listener. Dispatch of the all animation events is hierarchical: It will starts at the root of the view hierarchy and then traverse it and invoke the callback of the specific View that is being traversed. The method may return either DISPATCH_MODE_CONTINUE_ON_SUBTREE to indicate that animation events should be propagated to the subtree of the view hierarchy, or DISPATCH_MODE_STOP to stop dispatching. In that case, all animation callbacks related to the animation passed in will be stopped from propagating to the subtree of the hierarchy.

Also note that DISPATCH_MODE_STOP behaves the same way as returning CONSUMED during the regular insets dispatch in onApplyWindowInsets.

Returns
int

Either DISPATCH_MODE_CONTINUE_ON_SUBTREE to indicate that dispatching of animation events will continue to the subtree of the view hierarchy, or DISPATCH_MODE_STOP to indicate that animation events will stop dispatching.

onEnd

Added in 1.5.0
public void onEnd(@NonNull WindowInsetsAnimationCompat animation)

Called when an insets animation has ended.

Parameters
@NonNull WindowInsetsAnimationCompat animation

The animation that has ended. This will be the same instance as passed into onStart

onPrepare

Added in 1.5.0
public void onPrepare(@NonNull WindowInsetsAnimationCompat animation)

Called when an insets animation is about to start and before the views have been re-laid out due to an animation.

This ordering allows the application to inspect the end state after the animation has finished, and then revert to the starting state of the animation in the first onProgress callback by using post-layout view properties like setX and related methods.

The ordering of events during an insets animation is the following:

  • Application calls hide, show, controlWindowInsetsAnimation
  • onPrepare is called on the view hierarchy listeners
  • onApplyWindowInsets will be called with the end state of the animation
  • View hierarchy gets laid out according to the changes the application has requested due to the new insets being dispatched
  • onStart is called before the view hierarchy gets drawn in the new laid out state
  • onProgress is called immediately after with the animation start state
  • The frame gets drawn.

Note: If the animation is application controlled by using controlWindowInsetsAnimation, the end state of the animation is undefined as the application may decide on the end state only by passing in shown parameter when calling finish. In this situation, the system will dispatch the insets in the opposite visibility state before the animation starts. Example: When controlling the input method with controlWindowInsetsAnimation and the input method is currently showing, onApplyWindowInsets will receive a WindowInsetsCompat instance for which isVisible will return false for ime.

Parameters
@NonNull WindowInsetsAnimationCompat animation

The animation that is about to start.

onProgress

Added in 1.5.0
public abstract @NonNull WindowInsetsCompat onProgress(
    @NonNull WindowInsetsCompat insets,
    @NonNull List<WindowInsetsAnimationCompat> runningAnimations
)

Called when the insets change as part of running an animation. Note that even if multiple animations for different types are running, there will only be one progress callback per frame. The insets passed as an argument represents the overall state and will include all types, regardless of whether they are animating or not.

Note that insets dispatch is hierarchical: It will start at the root of the view hierarchy, and then traverse it and invoke the callback of the specific View being traversed. The method may return a modified instance by calling inset to indicate that a part of the insets have been used to offset or clip its children, and the children shouldn't worry about that part anymore. Furthermore, if getDispatchMode returns DISPATCH_MODE_STOP, children of this view will not receive the callback anymore.

Parameters
@NonNull WindowInsetsCompat insets

The current insets.

@NonNull List<WindowInsetsAnimationCompat> runningAnimations

The currently running animations.

Returns
@NonNull WindowInsetsCompat

The insets to dispatch to the subtree of the hierarchy.

onStart

Added in 1.5.0
public @NonNull WindowInsetsAnimationCompat.BoundsCompat onStart(
    @NonNull WindowInsetsAnimationCompat animation,
    @NonNull WindowInsetsAnimationCompat.BoundsCompat bounds
)

Called when an insets animation gets started.

This ordering allows the application to inspect the end state after the animation has finished, and then revert to the starting state of the animation in the first onProgress callback by using post-layout view properties like setX and related methods.

The ordering of events during an insets animation is the following:

  • Application calls hide, show, controlWindowInsetsAnimation
  • onPrepare is called on the view hierarchy listeners
  • onApplyWindowInsets will be called with the end state of the animation
  • View hierarchy gets laid out according to the changes the application has requested due to the new insets being dispatched
  • onStart is called before the view hierarchy gets drawn in the new laid out state
  • onProgress is called immediately after with the animation start state
  • The frame gets drawn.

Note that, like onProgress, dispatch of the animation start event is hierarchical: It will starts at the root of the view hierarchy and then traverse it and invoke the callback of the specific View that is being traversed. The method may return a modified instance of the bounds by calling inset to indicate that a part of the insets have been used to offset or clip its children, and the children shouldn't worry about that part anymore. Furthermore, if getDispatchMode returns DISPATCH_MODE_STOP, children of this view will not receive the callback anymore.

Parameters
@NonNull WindowInsetsAnimationCompat animation

The animation that is about to start.

@NonNull WindowInsetsAnimationCompat.BoundsCompat bounds

The bounds in which animation happens.

Returns
@NonNull WindowInsetsAnimationCompat.BoundsCompat

The animation bounds representing the part of the insets that should be dispatched to the subtree of the hierarchy.