MaterialContainerTransform

public final class MaterialContainerTransform


A shared element Transition that transforms one container to another.

MaterialContainerTransform can be used to morph between two Activities, Fragments, Views or a View to a Fragment.

This transition captures a start and end View which are used to create a Drawable which will be added to the view hierarchy. The drawable will be added to the view hierarchy as an overlay and handles drawing a mask that morphs between the shape of the start View to the shape of the end View. During the animation, the start and end View's are drawn inside the masking container and faded in and/or out over a duration of the transition. Additionally, the masking container will be translated and scaled from the position and size of the start View to the position and size of the end View.

MaterialContainerTransform supports theme-based easing, duration, and path values. In order to have the transition load these values upfront, use the MaterialContainerTransform constructor. Otherwise, use the default constructor and the transition will load theme values from the View context before it runs, and only use them if the corresponding properties weren't already set on the transition instance.

The composition of MaterialContainerTransform's animation can be customized in a number of ways. The two most prominent customizations are the way in which content inside the container is swapped via setFadeMode and path the container follows from its starting position to its ending position via setPathMotion. For other ways to customize the container transform, see:

Summary

Nested types

A class which holds a start and end value which represent a range within 0.0 - 1.0.

Constants

static final int

Indicates that this transition should cross fade the outgoing and incoming content.

static final int

Indicates that this transition should only fade in the incoming content, without changing the opacity of the outgoing content.

static final int

Indicates that this transition should only fade out the outgoing content, without changing the opacity of the incoming content.

static final int

Indicates that this transition should sequentially fade out the outgoing content and fade in the incoming content.

static final int

Indicates that this transition should automatically choose whether to use FIT_MODE_WIDTH or FIT_MODE_HEIGHT.

static final int

Indicates that this transition should fit the incoming content to the height of the outgoing content during the scale animation.

static final int

Indicates that this transition should fit the incoming content to the width of the outgoing content during the scale animation.

static final int

Indicates that this transition should use automatic detection to determine whether it is an Enter or a Return.

static final int

Indicates that this is an Enter transition, i.e., when elements are entering the scene.

static final int

Indicates that this is a Return transition, i.e., when elements are exiting the scene.

Public constructors

MaterialContainerTransform(Context context, boolean entering)

Public methods

void
captureEndValues(TransitionValues transitionValues)
void
captureStartValues(TransitionValues transitionValues)
Animator
createAnimator(
    ViewGroup sceneRoot,
    TransitionValues startValues,
    TransitionValues endValues
)
int

Get the color to be drawn beneath both the start view and end view.

int

Get the id of the View whose overlay this transitions will be added to.

int

Get the color to be drawn beneath the end view.

float

Get the elevation that will be used to render a shadow around the container at the end of the transition.

ShapeAppearanceModel

Get the ShapeAppearanceModel which will be used to determine the shape into which the container will be transformed.

View

Get the View which will be used as the end shared element container.

int

Get the id of the View which will be used as the end shared element container.

int

The fade mode to be used to swap the content of the start View with that of the end View.

MaterialContainerTransform.ProgressThresholds

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the fade animation, determined by getFadeMode will complete.

int

The fit mode to be used when scaling the incoming content of the end View.

MaterialContainerTransform.ProgressThresholds

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the start and end View's dimensions.

MaterialContainerTransform.ProgressThresholds

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the outgoing and incoming content will scale to the full dimensions of the end container.

int

Get the color to be drawn under the morphing container but within the bounds of the getDrawingViewId.

MaterialContainerTransform.ProgressThresholds

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the starting ShapeAppearanceModel and ending ShapeAppearanceModel.

int

Get the color to be drawn beneath the start view.

float

Get the elevation that will be used to render a shadow around the container at the start of the transition.

ShapeAppearanceModel

Get the ShapeAppearanceModel which will be used to determine the shape from which the container will be transformed.

View

Get the View which will be used as the start shared element container.

int

Get the id of the View which will be used as the start shared element container.

int

The direction to be used by this transform.

String[]
boolean

Whether debug drawing is enabled.

boolean

Get whether shadows should be drawn around the container to approximate native elevation shadows on the start and end views.

boolean

Whether to hold the last frame at the end of the animation.

void
setAllContainerColors(int containerColor)

Set the container color, the start container color and the end container color.

void
setContainerColor(int containerColor)

Set a color to be drawn beneath both the start and end view.

void
setDrawDebugEnabled(boolean drawDebugEnabled)

Set whether or not to draw paths which follow the shape and path of animating containers.

void
setDrawingViewId(int drawingViewId)

Set the id of the View whose overlay this transition will be added to.

void
setElevationShadowEnabled(boolean elevationShadowEnabled)

Set whether shadows should be drawn around the container to approximate native elevation shadows on the start and end views.

void
setEndContainerColor(int containerColor)

Set a color to be drawn beneath the end view.

void
setEndElevation(float endElevation)

Set the elevation that will be used to render a shadow around the container at the end of the transition.

void
setEndShapeAppearanceModel(
    ShapeAppearanceModel endShapeAppearanceModel
)

Set the ShapeAppearanceModel which will be used to determine the shape into which the container will be transformed.

void
setEndView(View endView)

Set the View to be used as the end shared element container.

void
setEndViewId(int endViewId)

Set the id of the View to be used as the end shared element container.

void
setFadeMode(int fadeMode)

Set the fade mode to be used to swap the content of the start View with that of the end View.

void

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the fade animation, determined by getFadeMode will complete.

void
setFitMode(int fitMode)

Set the fit mode to be used when scaling the incoming content of the end View.

void
setHoldAtEndEnabled(boolean holdAtEndEnabled)

If true, the last frame of the animation will be held in place, and the original outgoing and incoming views will not be re-shown.

void
setPathMotion(PathMotion pathMotion)
void

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the start and end View's dimensions.

void

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the outgoing and incoming content will scale to the full dimensions of the end container.

void
setScrimColor(int scrimColor)

Set the color to be drawn under the morphing container but within the bounds of the getDrawingViewId.

void

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the starting ShapeAppearanceModel and ending ShapeAppearanceModel.

void
setStartContainerColor(int containerColor)

Set a color to be drawn beneath the start view.

void
setStartElevation(float startElevation)

Set the elevation that will be used to render a shadow around the container at the start of the transition.

void
setStartShapeAppearanceModel(
    ShapeAppearanceModel startShapeAppearanceModel
)

Set the ShapeAppearanceModel which will be used to determine the shape from which the container will be transformed.

void
setStartView(View startView)

Set the View to be used as the start shared element container.

void
setStartViewId(int startViewId)

Set the id of the View to be used as the start shared element container.

void
setTransitionDirection(int transitionDirection)

Set the transition direction to be used by this transform.

Constants

FADE_MODE_CROSS

public static final int FADE_MODE_CROSS = 2

Indicates that this transition should cross fade the outgoing and incoming content.

FADE_MODE_IN

public static final int FADE_MODE_IN = 0

Indicates that this transition should only fade in the incoming content, without changing the opacity of the outgoing content.

FADE_MODE_OUT

public static final int FADE_MODE_OUT = 1

Indicates that this transition should only fade out the outgoing content, without changing the opacity of the incoming content.

FADE_MODE_THROUGH

public static final int FADE_MODE_THROUGH = 3

Indicates that this transition should sequentially fade out the outgoing content and fade in the incoming content.

FIT_MODE_AUTO

public static final int FIT_MODE_AUTO = 0

Indicates that this transition should automatically choose whether to use FIT_MODE_WIDTH or FIT_MODE_HEIGHT.

FIT_MODE_HEIGHT

public static final int FIT_MODE_HEIGHT = 2

Indicates that this transition should fit the incoming content to the height of the outgoing content during the scale animation.

FIT_MODE_WIDTH

public static final int FIT_MODE_WIDTH = 1

Indicates that this transition should fit the incoming content to the width of the outgoing content during the scale animation.

TRANSITION_DIRECTION_AUTO

public static final int TRANSITION_DIRECTION_AUTO = 0

Indicates that this transition should use automatic detection to determine whether it is an Enter or a Return. If the end container has a larger area than the start container then it is considered an Enter transition, otherwise it is a Return transition.

TRANSITION_DIRECTION_ENTER

public static final int TRANSITION_DIRECTION_ENTER = 1

Indicates that this is an Enter transition, i.e., when elements are entering the scene.

TRANSITION_DIRECTION_RETURN

public static final int TRANSITION_DIRECTION_RETURN = 2

Indicates that this is a Return transition, i.e., when elements are exiting the scene.

Public fields

containerColor

public int containerColor

drawingViewId

public int drawingViewId

endContainerColor

public int endContainerColor

endElevation

public float endElevation

endShapeAppearanceModel

public ShapeAppearanceModel endShapeAppearanceModel

endView

public View endView

endViewId

public int endViewId

fadeMode

public int fadeMode

fitMode

public int fitMode

scrimColor

public int scrimColor

startContainerColor

public int startContainerColor

startElevation

public float startElevation

startShapeAppearanceModel

public ShapeAppearanceModel startShapeAppearanceModel

startView

public View startView

startViewId

public int startViewId

transitionDirection

public int transitionDirection

Public constructors

MaterialContainerTransform

public MaterialContainerTransform()

MaterialContainerTransform

public MaterialContainerTransform(Context context, boolean entering)

Public methods

captureEndValues

public void captureEndValues(TransitionValues transitionValues)

captureStartValues

public void captureStartValues(TransitionValues transitionValues)

createAnimator

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

getContainerColor

public int getContainerColor()

Get the color to be drawn beneath both the start view and end view.

getDrawingViewId

public int getDrawingViewId()

Get the id of the View whose overlay this transitions will be added to.

getEndContainerColor

public int getEndContainerColor()

Get the color to be drawn beneath the end view.

getEndElevation

public float getEndElevation()

Get the elevation that will be used to render a shadow around the container at the end of the transition.

Default is -1, which means the elevation of the end view will be used.

getEndShapeAppearanceModel

public ShapeAppearanceModel getEndShapeAppearanceModel()

Get the ShapeAppearanceModel which will be used to determine the shape into which the container will be transformed.

getEndView

public View getEndView()

Get the View which will be used as the end shared element container.

getEndViewId

public int getEndViewId()

Get the id of the View which will be used as the end shared element container.

Setting an end View id can be used to manually configure MaterialContainerTransform when transitioning between two Views in a single layout when the Transition system will not automatically capture shared element start or end Views for you.

If the end view cannot be found during the initialization of the MaterialContainerTransform, then an IllegalArgumentException will be thrown.

getFadeMode

public int getFadeMode()

The fade mode to be used to swap the content of the start View with that of the end View.

getFadeProgressThresholds

public MaterialContainerTransform.ProgressThresholds getFadeProgressThresholds()

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the fade animation, determined by getFadeMode will complete.

getFitMode

public int getFitMode()

The fit mode to be used when scaling the incoming content of the end View.

getScaleMaskProgressThresholds

public MaterialContainerTransform.ProgressThresholds getScaleMaskProgressThresholds()

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the start and end View's dimensions.

getScaleProgressThresholds

public MaterialContainerTransform.ProgressThresholds getScaleProgressThresholds()

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the outgoing and incoming content will scale to the full dimensions of the end container.

getScrimColor

public int getScrimColor()

Get the color to be drawn under the morphing container but within the bounds of the getDrawingViewId.

getShapeMaskProgressThresholds

public MaterialContainerTransform.ProgressThresholds getShapeMaskProgressThresholds()

Get the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the starting ShapeAppearanceModel and ending ShapeAppearanceModel.

getStartContainerColor

public int getStartContainerColor()

Get the color to be drawn beneath the start view.

getStartElevation

public float getStartElevation()

Get the elevation that will be used to render a shadow around the container at the start of the transition.

Default is -1, which means the elevation of the start view will be used.

getStartShapeAppearanceModel

public ShapeAppearanceModel getStartShapeAppearanceModel()

Get the ShapeAppearanceModel which will be used to determine the shape from which the container will be transformed.

getStartView

public View getStartView()

Get the View which will be used as the start shared element container.

getStartViewId

public int getStartViewId()

Get the id of the View which will be used as the start shared element container.

getTransitionDirection

public int getTransitionDirection()

The direction to be used by this transform.

getTransitionProperties

public String[] getTransitionProperties()

isDrawDebugEnabled

public boolean isDrawDebugEnabled()

Whether debug drawing is enabled.

isElevationShadowEnabled

public boolean isElevationShadowEnabled()

Get whether shadows should be drawn around the container to approximate native elevation shadows on the start and end views.

isHoldAtEndEnabled

public boolean isHoldAtEndEnabled()

Whether to hold the last frame at the end of the animation.

setAllContainerColors

public void setAllContainerColors(int containerColor)

Set the container color, the start container color and the end container color.

This is a helper for the common case of transitioning between a start and end view when neither draws its own background but a common color is shared. This prevents the start or end view from being visible below one another.

setContainerColor

public void setContainerColor(int containerColor)

Set a color to be drawn beneath both the start and end view.

This color is the background color of the transforming container inside of which the start and end views are drawn. Unlike the start view, start container color, end view and end container color, this color will always be drawn as fully opaque, beneath all other content in the container. By default, this color is set to transparent (0), meaning a container color will not be drawn.

If a default container transform results in the start view being visible beneath the end view, or vica versa, this is due to one or both views not having a background. The most common way to solve this issue is by sequentially fading the contents with FADE_MODE_THROUGH and setting this color to the start and end view's desired background color.

If the start and end views have different background colors, or you would like to use a fade mode other than FADE_MODE_THROUGH, handle this by using setStartContainerColor and setEndContainerColor.

setDrawDebugEnabled

public void setDrawDebugEnabled(boolean drawDebugEnabled)

Set whether or not to draw paths which follow the shape and path of animating containers.

Parameters
boolean drawDebugEnabled

true if debugging lines and borders should be drawn during animation.

setDrawingViewId

public void setDrawingViewId(int drawingViewId)

Set the id of the View whose overlay this transition will be added to.

This can be used to limit the bounds of the animation (including the background scrim) to the bounds of the provided drawing view, and also have the animation drawn at the relative z-order of the drawing view.

By default, the drawingViewId will be android.R.id.content. Additionally, if drawingViewId is the same as the end View's id, MaterialContainerTransform will add the transition's drawable to the drawingViewId's parent instead.

If the drawing view cannot be found during the initialization of the MaterialContainerTransform, then an IllegalArgumentException will be thrown.

setElevationShadowEnabled

public void setElevationShadowEnabled(boolean elevationShadowEnabled)

Set whether shadows should be drawn around the container to approximate native elevation shadows on the start and end views.

By default, the elevation shadows are only enabled for API level 28 and above, because Paint shadows are not supported with hardware acceleration below API level 28. If enabled for below API level 28, then the shadows will be drawn using , however this may cause performance issues.

Additionally, the rendering of elevation shadows may cause performance issues if the container's shape is not a round rect or a regular rect, e.g., a rect with cut corners.

setEndContainerColor

public void setEndContainerColor(int containerColor)

Set a color to be drawn beneath the end view.

This color will be drawn directly beneath the end view, will fill the entire transforming container, and the will animate its opacity to match the end view's. By default, this color is set to transparent (0), meaning no color will be drawn.

This method can be useful when the color of the start and end view differ and the end view does not handle drawing its own background. Setting this color will prevent the start view from being visible beneath the end view while transforming.

setEndElevation

public void setEndElevation(float endElevation)

Set the elevation that will be used to render a shadow around the container at the end of the transition.

By default the elevation of the end view will be used.

setEndShapeAppearanceModel

public void setEndShapeAppearanceModel(
    ShapeAppearanceModel endShapeAppearanceModel
)

Set the ShapeAppearanceModel which will be used to determine the shape into which the container will be transformed.

setEndView

public void setEndView(View endView)

Set the View to be used as the end shared element container.

See also
setEndViewId

setEndViewId

public void setEndViewId(int endViewId)

Set the id of the View to be used as the end shared element container. The matching View will be searched for in the hierarchy when starting this transition.

Manually setting the end View id will override any View explicitly set via setEndView or any View picked up by the Transition system marked with a transitionName.

setFadeMode

public void setFadeMode(int fadeMode)

Set the fade mode to be used to swap the content of the start View with that of the end View.

By default, the fade mode is set to FADE_MODE_IN.

setFadeProgressThresholds

public void setFadeProgressThresholds(
    MaterialContainerTransform.ProgressThresholds fadeProgressThresholds
)

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the fade animation, determined by getFadeMode will complete.

See ProgressThresholds for an example of how the threshold ranges work.

setFitMode

public void setFitMode(int fitMode)

Set the fit mode to be used when scaling the incoming content of the end View.

By default, the fit mode is set to FIT_MODE_AUTO.

setHoldAtEndEnabled

public void setHoldAtEndEnabled(boolean holdAtEndEnabled)

If true, the last frame of the animation will be held in place, and the original outgoing and incoming views will not be re-shown.

Useful for Activity return transitions to make sure the screen doesn't flash at the end.

setPathMotion

public void setPathMotion(PathMotion pathMotion)

setScaleMaskProgressThresholds

public void setScaleMaskProgressThresholds(
    MaterialContainerTransform.ProgressThresholds scaleMaskProgressThresholds
)

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the start and end View's dimensions.

See ProgressThresholds for an example of how the threshold ranges work.

setScaleProgressThresholds

public void setScaleProgressThresholds(
    MaterialContainerTransform.ProgressThresholds scaleProgressThresholds
)

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 - 1.0) between which the outgoing and incoming content will scale to the full dimensions of the end container.

See ProgressThresholds for an example of how the threshold ranges work.

setScrimColor

public void setScrimColor(int scrimColor)

Set the color to be drawn under the morphing container but within the bounds of the getDrawingViewId.

By default this is set to black with 32% opacity. Drawing a scrim is primarily useful for transforming from a partial-screen View (eg. Card in a grid) to a full screen. The scrim will gradually fade in and cover the content being transformed over by the morphing container.

Changing the default scrim color can be useful when transitioning between two Views in a layout, where the ending View does not cover any outgoing content (eg. a FAB to a bottom toolbar). For scenarios such as these, set the scrim color to transparent.

setShapeMaskProgressThresholds

public void setShapeMaskProgressThresholds(
    MaterialContainerTransform.ProgressThresholds shapeMaskProgressThresholds
)

Set the ProgressThresholds which define the sub-range (any range inside the full progress range of 0.0 and 1.0) between which the container will morph between the starting ShapeAppearanceModel and ending ShapeAppearanceModel.

See ProgressThresholds for an example of how the threshold ranges work.

setStartContainerColor

public void setStartContainerColor(int containerColor)

Set a color to be drawn beneath the start view.

This color will be drawn directly beneath the start view, will fill the entire transforming container, and will animate its opacity to match the start view's. By default, this color is set to transparent (0), meaning no color will be drawn.

This method can be useful when the color of the start and end view differ and the start view does not handle drawing its own background. This can also be used if an expanding container is larger than the start view. Setting this color to match that of the start view's background will cause the start view to look like its background is expanding to fill the transforming container.

setStartElevation

public void setStartElevation(float startElevation)

Set the elevation that will be used to render a shadow around the container at the start of the transition.

By default the elevation of the start view will be used.

setStartShapeAppearanceModel

public void setStartShapeAppearanceModel(
    ShapeAppearanceModel startShapeAppearanceModel
)

Set the ShapeAppearanceModel which will be used to determine the shape from which the container will be transformed.

Manually setting a shape appearance will override both your theme's transitionShapeAppearance attribute (if set) and the shape appearance of the start View (or end View via setEndShapeAppearanceModel if the View implements the Shapeable interface. Setting this property can be useful if your start or end View does not implement Shapeable but does have a shape (eg. a rounded rect background drawable) and you would like MaterialContainerTransform to morph from or to your View's shape.

setStartView

public void setStartView(View startView)

Set the View to be used as the start shared element container.

See also
setStartViewId

setStartViewId

public void setStartViewId(int startViewId)

Set the id of the View to be used as the start shared element container. The matching View will be searched for in the hierarchy when starting this transition.

Setting a start View can be helpful when transitioning from a View to another View or if transitioning from a View to a Fragment.

Manually setting the start View id will override any View explicitly set via setStartView or any View picked up by the Transition system marked with a transitionName.

If the start view cannot be found during the initialization of the MaterialContainerTransform, then an IllegalArgumentException will be thrown.

setTransitionDirection

public void setTransitionDirection(int transitionDirection)

Set the transition direction to be used by this transform.

By default, the transition direction is determined by the change in size between the start and end Views.