Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

ElevationOverlayProvider

public class ElevationOverlayProvider
extends Object

java.lang.Object
   ↳ com.google.android.material.elevation.ElevationOverlayProvider


Utility for calculating elevation overlay alpha values and colors.

Summary

Public constructors

ElevationOverlayProvider(Context context)

Public methods

int calculateOverlayAlpha(float elevation)

Calculates the alpha value, between 0 and 255, that should be used with the elevation overlay color, based on the provided elevation value.

float calculateOverlayAlphaFraction(float elevation)

Calculates the alpha fraction, between 0 and 1, that should be used with the elevation overlay color, based on the provided elevation value.

int compositeOverlay(int backgroundColor, float elevation)

Blends the calculated elevation overlay color with the provided backgroundColor.

int compositeOverlay(int backgroundColor, float elevation, View overlayView)

See compositeOverlay(int, float).

int compositeOverlayIfNeeded(int backgroundColor, float elevation, View overlayView)

See compositeOverlayIfNeeded(int, float).

int compositeOverlayIfNeeded(int backgroundColor, float elevation)

Blends the calculated elevation overlay color (@see #compositeOverlay(int, float)) with the backgroundColor, only if the current theme's R.attr.elevationOverlayEnabled is true and the backgroundColor matches the theme's surface color (R.attr.colorSurface); otherwise returns the backgroundColor.

int compositeOverlayWithThemeSurfaceColorIfNeeded(float elevation)

Blends the calculated elevation overlay color (@see #compositeOverlayIfNeeded(int, float)) with the current theme's color int value for R.attr.colorSurface if needed.

int compositeOverlayWithThemeSurfaceColorIfNeeded(float elevation, View overlayView)

See compositeOverlayWithThemeSurfaceColorIfNeeded(float).

float getParentAbsoluteElevation(View overlayView)

Returns the absolute elevation of the parent of the provided overlayView, or in other words, the sum of the elevations of all ancestors of the overlayView.

int getThemeElevationOverlayColor()

Returns the current theme's color int value for R.attr.elevationOverlayColor.

int getThemeSurfaceColor()

Returns the current theme's color int value for R.attr.colorSurface.

boolean isThemeElevationOverlayEnabled()

Returns the current theme's boolean value for R.attr.elevationOverlayEnabled.

Inherited methods

From class java.lang.Object

Public constructors

ElevationOverlayProvider

ElevationOverlayProvider (Context context)

Parameters
context Context

Public methods

calculateOverlayAlpha

int calculateOverlayAlpha (float elevation)

Calculates the alpha value, between 0 and 255, that should be used with the elevation overlay color, based on the provided elevation value.

Parameters
elevation float

Returns
int

calculateOverlayAlphaFraction

float calculateOverlayAlphaFraction (float elevation)

Calculates the alpha fraction, between 0 and 1, that should be used with the elevation overlay color, based on the provided elevation value.

Parameters
elevation float

Returns
float

compositeOverlay

int compositeOverlay (int backgroundColor, 
                float elevation)

Blends the calculated elevation overlay color with the provided backgroundColor.

An alpha level is applied to the theme's R.attr.elevationOverlayColor by using a formula that is based on the provided elevation value.

Parameters
backgroundColor int

elevation float

Returns
int

compositeOverlay

int compositeOverlay (int backgroundColor, 
                float elevation, 
                View overlayView)

See compositeOverlay(int, float).

Parameters
backgroundColor int

elevation float

overlayView View

Returns
int

compositeOverlayIfNeeded

int compositeOverlayIfNeeded (int backgroundColor, 
                float elevation, 
                View overlayView)

See compositeOverlayIfNeeded(int, float).

The absolute elevation of the parent of the provided overlayView will also be factored in when determining the overlay color.

Parameters
backgroundColor int

elevation float

overlayView View

Returns
int

compositeOverlayIfNeeded

int compositeOverlayIfNeeded (int backgroundColor, 
                float elevation)

Blends the calculated elevation overlay color (@see #compositeOverlay(int, float)) with the backgroundColor, only if the current theme's R.attr.elevationOverlayEnabled is true and the backgroundColor matches the theme's surface color (R.attr.colorSurface); otherwise returns the backgroundColor.

Parameters
backgroundColor int

elevation float

Returns
int

compositeOverlayWithThemeSurfaceColorIfNeeded

int compositeOverlayWithThemeSurfaceColorIfNeeded (float elevation)

Blends the calculated elevation overlay color (@see #compositeOverlayIfNeeded(int, float)) with the current theme's color int value for R.attr.colorSurface if needed.

Parameters
elevation float

Returns
int

compositeOverlayWithThemeSurfaceColorIfNeeded

int compositeOverlayWithThemeSurfaceColorIfNeeded (float elevation, 
                View overlayView)

See compositeOverlayWithThemeSurfaceColorIfNeeded(float).

The absolute elevation of the parent of the provided overlayView will also be factored in when determining the overlay color.

Parameters
elevation float

overlayView View

Returns
int

getParentAbsoluteElevation

float getParentAbsoluteElevation (View overlayView)

Returns the absolute elevation of the parent of the provided overlayView, or in other words, the sum of the elevations of all ancestors of the overlayView.

Parameters
overlayView View

Returns
float

getThemeElevationOverlayColor

int getThemeElevationOverlayColor ()

Returns the current theme's color int value for R.attr.elevationOverlayColor.

Returns
int

getThemeSurfaceColor

int getThemeSurfaceColor ()

Returns the current theme's color int value for R.attr.colorSurface.

Returns
int

isThemeElevationOverlayEnabled

boolean isThemeElevationOverlayEnabled ()

Returns the current theme's boolean value for R.attr.elevationOverlayEnabled.

Returns
boolean