Most visited

Recently visited

Added in API level 19

Visibility

public abstract class Visibility
extends Transition

java.lang.Object
   ↳ android.transition.Transition
     ↳ android.transition.Visibility
Known Direct Subclasses


This transition tracks changes to the visibility of target views in the start and end scenes. Visibility is determined not just by the setVisibility(int) state of views, but also whether views exist in the current view hierarchy. The class is intended to be a utility for subclasses such as Fade, which use this visibility information to determine the specific animations to run when visibility changes occur. Subclasses should implement one or both of the methods onAppear(ViewGroup, TransitionValues, int, TransitionValues, int), onDisappear(ViewGroup, TransitionValues, int, TransitionValues, int) or onAppear(ViewGroup, View, TransitionValues, TransitionValues), onDisappear(ViewGroup, View, TransitionValues, TransitionValues).

Summary

XML attributes

android:transitionVisibilityMode Changes whether the transition supports appearing and/or disappearing Views. 

Inherited XML attributes

From class android.transition.Transition

Constants

int MODE_IN

Mode used in setMode(int) to make the transition operate on targets that are appearing.

int MODE_OUT

Mode used in setMode(int) to make the transition operate on targets that are disappearing.

Inherited constants

From class android.transition.Transition

Public constructors

Visibility()
Visibility(Context context, AttributeSet attrs)

Public methods

void captureEndValues(TransitionValues transitionValues)

Captures the values in the end scene for the properties that this transition monitors.

void captureStartValues(TransitionValues transitionValues)

Captures the values in the start scene for the properties that this transition monitors.

Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues)

This method creates an animation that will be run for this transition given the information in the startValues and endValues structures captured earlier for the start and end scenes.

int getMode()

Returns whether appearing and/or disappearing Views are supported.

String[] getTransitionProperties()

Returns the set of property names used stored in the TransitionValues object passed into captureStartValues(TransitionValues) that this transition cares about for the purposes of canceling overlapping animations.

boolean isTransitionRequired(TransitionValues startValues, TransitionValues newValues)

Returns whether or not the transition should create an Animator, based on the values captured during captureStartValues(TransitionValues) and captureEndValues(TransitionValues).

boolean isVisible(TransitionValues values)

Returns whether the view is 'visible' according to the given values object.

Animator onAppear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues)

The default implementation of this method returns a null Animator.

Animator onAppear(ViewGroup sceneRoot, TransitionValues startValues, int startVisibility, TransitionValues endValues, int endVisibility)

The default implementation of this method calls onAppear(ViewGroup, View, TransitionValues, TransitionValues).

Animator onDisappear(ViewGroup sceneRoot, View view, TransitionValues startValues, TransitionValues endValues)

The default implementation of this method returns a null Animator.

Animator onDisappear(ViewGroup sceneRoot, TransitionValues startValues, int startVisibility, TransitionValues endValues, int endVisibility)

Subclasses should override this method or onDisappear(ViewGroup, View, TransitionValues, TransitionValues) if they need to create an Animator when targets disappear.

void setMode(int mode)

Changes the transition to support appearing and/or disappearing Views, depending on mode.

Inherited methods

From class android.transition.Transition
From class java.lang.Object

XML attributes

android:transitionVisibilityMode

Changes whether the transition supports appearing and/or disappearing Views. Corresponds to setMode(int).

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
mode_in1 Only appearing Views will be supported.
mode_out2 Only disappearing Views will be supported.

This corresponds to the global attribute resource symbol transitionVisibilityMode.

Related methods:

Constants

MODE_IN

Added in API level 21
int MODE_IN

Mode used in setMode(int) to make the transition operate on targets that are appearing. Maybe be combined with MODE_OUT to target Visibility changes both in and out.

Constant Value: 1 (0x00000001)

MODE_OUT

Added in API level 21
int MODE_OUT

Mode used in setMode(int) to make the transition operate on targets that are disappearing. Maybe be combined with MODE_IN to target Visibility changes both in and out.

Constant Value: 2 (0x00000002)

Public constructors

Visibility

Added in API level 19
Visibility ()

Visibility

Added in API level 21
Visibility (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

Public methods

captureEndValues

Added in API level 19
void captureEndValues (TransitionValues transitionValues)

Captures the values in the end scene for the properties that this transition monitors. These values are then passed as the endValues structure in a later call to createAnimator(ViewGroup, TransitionValues, TransitionValues). The main concern for an implementation is what the properties are that the transition cares about and what the values are for all of those properties. The start and end values will be compared later during the createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues) method to determine what, if any, animations, should be run.

Subclasses must implement this method. The method should only be called by the transition system; it is not intended to be called from external classes.

Parameters
transitionValues TransitionValues: The holder for any values that the Transition wishes to store. Values are stored in the values field of this TransitionValues object and are keyed from a String value. For example, to store a view's rotation value, a transition might call transitionValues.values.put("appname:transitionname:rotation", view.getRotation()). The target view will already be stored in the transitionValues structure when this method is called.

captureStartValues

Added in API level 19
void captureStartValues (TransitionValues transitionValues)

Captures the values in the start scene for the properties that this transition monitors. These values are then passed as the startValues structure in a later call to createAnimator(ViewGroup, TransitionValues, TransitionValues). The main concern for an implementation is what the properties are that the transition cares about and what the values are for all of those properties. The start and end values will be compared later during the createAnimator(android.view.ViewGroup, TransitionValues, TransitionValues) method to determine what, if any, animations, should be run.

Subclasses must implement this method. The method should only be called by the transition system; it is not intended to be called from external classes.

Parameters
transitionValues TransitionValues: The holder for any values that the Transition wishes to store. Values are stored in the values field of this TransitionValues object and are keyed from a String value. For example, to store a view's rotation value, a transition might call transitionValues.values.put("appname:transitionname:rotation", view.getRotation()). The target view will already be stored in the transitionValues structure when this method is called.

createAnimator

Added in API level 19
Animator createAnimator (ViewGroup sceneRoot, 
                TransitionValues startValues, 
                TransitionValues endValues)

This method creates an animation that will be run for this transition given the information in the startValues and endValues structures captured earlier for the start and end scenes. Subclasses of Transition should override this method. The method should only be called by the transition system; it is not intended to be called from external classes.

This method is called by the transition's parent (all the way up to the topmost Transition in the hierarchy) with the sceneRoot and start/end values that the transition may need to set up initial target values and construct an appropriate animation. For example, if an overall Transition is a TransitionSet consisting of several child transitions in sequence, then some of the child transitions may want to set initial values on target views prior to the overall Transition commencing, to put them in an appropriate state for the delay between that start and the child Transition start time. For example, a transition that fades an item in may wish to set the starting alpha value to 0, to avoid it blinking in prior to the transition actually starting the animation. This is necessary because the scene change that triggers the Transition will automatically set the end-scene on all target views, so a Transition that wants to animate from a different value should set that value prior to returning from this method.

Additionally, a Transition can perform logic to determine whether the transition needs to run on the given target and start/end values. For example, a transition that resizes objects on the screen may wish to avoid running for views which are not present in either the start or end scenes.

If there is an animator created and returned from this method, the transition mechanism will apply any applicable duration, startDelay, and interpolator to that animation and start it. A return value of null indicates that no animation should run. The default implementation returns null.

The method is called for every applicable target object, which is stored in the view field.

Parameters
sceneRoot ViewGroup: The root of the transition hierarchy.
startValues TransitionValues: The values for a specific target in the start scene.
endValues TransitionValues: The values for the target in the end scene.
Returns
Animator A Animator to be started at the appropriate time in the overall transition for this scene change. A null value means no animation should be run.

getMode

Added in API level 21
int getMode ()

Returns whether appearing and/or disappearing Views are supported. Returns whether appearing and/or disappearing Views are supported. A combination of MODE_IN and MODE_OUT.

Related XML Attributes:

Returns
int

getTransitionProperties

Added in API level 19
String[] getTransitionProperties ()

Returns the set of property names used stored in the TransitionValues object passed into captureStartValues(TransitionValues) that this transition cares about for the purposes of canceling overlapping animations. When any transition is started on a given scene root, all transitions currently running on that same scene root are checked to see whether the properties on which they based their animations agree with the end values of the same properties in the new transition. If the end values are not equal, then the old animation is canceled since the new transition will start a new animation to these new values. If the values are equal, the old animation is allowed to continue and no new animation is started for that transition.

A transition does not need to override this method. However, not doing so will mean that the cancellation logic outlined in the previous paragraph will be skipped for that transition, possibly leading to artifacts as old transitions and new transitions on the same targets run in parallel, animating views toward potentially different end values.

Returns
String[] An array of property names as described in the class documentation for TransitionValues. The default implementation returns null.

isTransitionRequired

Added in API level 23
boolean isTransitionRequired (TransitionValues startValues, 
                TransitionValues newValues)

Returns whether or not the transition should create an Animator, based on the values captured during captureStartValues(TransitionValues) and captureEndValues(TransitionValues). The default implementation compares the property values returned from getTransitionProperties(), or all property values if getTransitionProperties() returns null. Subclasses may override this method to provide logic more specific to the transition implementation.

Parameters
startValues TransitionValues: the values from captureStartValues, This may be null if the View did not exist in the start state.
newValues TransitionValues: the values from captureEndValues. This may be null if the View did not exist in the end state.
Returns
boolean

isVisible

Added in API level 19
boolean isVisible (TransitionValues values)

Returns whether the view is 'visible' according to the given values object. This is determined by testing the same properties in the values object that are used to determine whether the object is appearing or disappearing in the createAnimator(ViewGroup, TransitionValues, TransitionValues) method. This method can be called by, for example, subclasses that want to know whether the object is visible in the same way that Visibility determines it for the actual animation.

Parameters
values TransitionValues: The TransitionValues object that holds the information by which visibility is determined.
Returns
boolean True if the view reference by values is visible, false otherwise.

onAppear

Added in API level 21
Animator onAppear (ViewGroup sceneRoot, 
                View view, 
                TransitionValues startValues, 
                TransitionValues endValues)

The default implementation of this method returns a null Animator. Subclasses should override this method to make targets appear with the desired transition. The method should only be called from onAppear(ViewGroup, TransitionValues, int, TransitionValues, int).

Parameters
sceneRoot ViewGroup: The root of the transition hierarchy
view View: The View to make appear. This will be in the target scene's View hierarchy and will be VISIBLE.
startValues TransitionValues: The target values in the start scene
endValues TransitionValues: The target values in the end scene
Returns
Animator An Animator to be started at the appropriate time in the overall transition for this scene change. A null value means no animation should be run.

onAppear

Added in API level 19
Animator onAppear (ViewGroup sceneRoot, 
                TransitionValues startValues, 
                int startVisibility, 
                TransitionValues endValues, 
                int endVisibility)

The default implementation of this method calls onAppear(ViewGroup, View, TransitionValues, TransitionValues). Subclasses should override this method or onAppear(ViewGroup, View, TransitionValues, TransitionValues). if they need to create an Animator when targets appear. The method should only be called by the Visibility class; it is not intended to be called from external classes.

Parameters
sceneRoot ViewGroup: The root of the transition hierarchy
startValues TransitionValues: The target values in the start scene
startVisibility int: The target visibility in the start scene
endValues TransitionValues: The target values in the end scene
endVisibility int: The target visibility in the end scene
Returns
Animator An Animator to be started at the appropriate time in the overall transition for this scene change. A null value means no animation should be run.

onDisappear

Added in API level 21
Animator onDisappear (ViewGroup sceneRoot, 
                View view, 
                TransitionValues startValues, 
                TransitionValues endValues)

The default implementation of this method returns a null Animator. Subclasses should override this method to make targets disappear with the desired transition. The method should only be called from onDisappear(ViewGroup, TransitionValues, int, TransitionValues, int).

Parameters
sceneRoot ViewGroup: The root of the transition hierarchy
view View: The View to make disappear. This will be in the target scene's View hierarchy or in an ViewGroupOverlay and will be VISIBLE.
startValues TransitionValues: The target values in the start scene
endValues TransitionValues: The target values in the end scene
Returns
Animator An Animator to be started at the appropriate time in the overall transition for this scene change. A null value means no animation should be run.

onDisappear

Added in API level 19
Animator onDisappear (ViewGroup sceneRoot, 
                TransitionValues startValues, 
                int startVisibility, 
                TransitionValues endValues, 
                int endVisibility)

Subclasses should override this method or onDisappear(ViewGroup, View, TransitionValues, TransitionValues) if they need to create an Animator when targets disappear. The method should only be called by the Visibility class; it is not intended to be called from external classes.

The default implementation of this method attempts to find a View to use to call onDisappear(ViewGroup, View, TransitionValues, TransitionValues), based on the situation of the View in the View hierarchy. For example, if a View was simply removed from its parent, then the View will be added into a ViewGroupOverlay and passed as the view parameter in onDisappear(ViewGroup, View, TransitionValues, TransitionValues). If a visible View is changed to be GONE or INVISIBLE, then it can be used as the view and the visibility will be changed to VISIBLE for the duration of the animation. However, if a View is in a hierarchy which is also altering its visibility, the situation can be more complicated. In general, if a view that is no longer in the hierarchy in the end scene still has a parent (so its parent hierarchy was removed, but it was not removed from its parent), then it will be left alone to avoid side-effects from improperly removing it from its parent. The only exception to this is if the previous Scene was created from a layout resource file, then it is considered safe to un-parent the starting scene view in order to make it disappear.

Parameters
sceneRoot ViewGroup: The root of the transition hierarchy
startValues TransitionValues: The target values in the start scene
startVisibility int: The target visibility in the start scene
endValues TransitionValues: The target values in the end scene
endVisibility int: The target visibility in the end scene
Returns
Animator An Animator to be started at the appropriate time in the overall transition for this scene change. A null value means no animation should be run.

setMode

Added in API level 21
void setMode (int mode)

Changes the transition to support appearing and/or disappearing Views, depending on mode.

Related XML Attributes:

Parameters
mode int: The behavior supported by this transition, a combination of MODE_IN and MODE_OUT.

Hooray!