Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 1.0.0
belongs to Maven artifact androidx.leanback:leanback:1.0.0-rc01

ShadowOverlayHelper

public final class ShadowOverlayHelper
extends Object

java.lang.Object
   ↳ androidx.leanback.widget.ShadowOverlayHelper


ShadowOverlayHelper is a helper class for shadow, overlay color and rounded corner. There are many choices to implement Shadow, overlay color. Initialize it with ShadowOverlayHelper.Builder and it decides the best strategy based on options user choose and current platform version.

  • For shadow: it may use 9-patch with opticalBounds or Z-value based shadow for API >= 21. When 9-patch is used, it requires a ShadowOverlayContainer to include 9-patch views.
  • For overlay: it may use ShadowOverlayContainer which overrides draw() or it may use setForeground(new ColorDrawable()) for API>=23. The foreground support might be disabled if rounded corner is applied due to performance reason.
  • For rounded-corner: it uses a ViewOutlineProvider for API>=21. There are two different strategies: use Wrapper with a ShadowOverlayContainer; or apply rounded corner, overlay and rounded-corner to the view itself. Below is an example of how helper is used. ShadowOverlayHelper mHelper = new ShadowOverlayHelper.Builder(). .needsOverlay(true).needsRoundedCorner(true).needsShadow(true) .build(); mHelper.prepareParentForShadow(parentView); // apply optical-bounds for 9-patch shadow. mHelper.setOverlayColor(view, Color.argb(0x80, 0x80, 0x80, 0x80)); mHelper.setShadowFocusLevel(view, 1.0f); ... View initializeView(View view) { if (mHelper.needsWrapper()) { ShadowOverlayContainer wrapper = mHelper.createShadowOverlayContainer(context); wrapper.wrap(view); return wrapper; } else { mHelper.onViewCreated(view); return view; } } ...

    Summary

    Nested classes

    class ShadowOverlayHelper.Builder

    Builder for creating ShadowOverlayHelper. 

    class ShadowOverlayHelper.Options

    Option values for ShadowOverlayContainer. 

    Constants

    int SHADOW_DYNAMIC

    Shadows depend on the size, shape, and position of the view.

    int SHADOW_NONE

    No shadow.

    int SHADOW_STATIC

    Shadows are fixed.

    Public methods

    ShadowOverlayContainer createShadowOverlayContainer(Context context)

    Create ShadowOverlayContainer for this helper.

    int getShadowType()
    boolean needsOverlay()
    boolean needsRoundedCorner()
    boolean needsWrapper()

    Returns true if a "wrapper" ShadowOverlayContainer is needed.

    void onViewCreated(View view)

    Must be called when view is created for cases needsWrapper() is false.

    void prepareParentForShadow(ViewGroup parent)

    prepareParentForShadow(ViewGroup) must be called on parent of container before using shadow.

    static void setNoneWrapperOverlayColor(View view, int color)

    Set overlay color for view other than ShadowOverlayContainer.

    static void setNoneWrapperShadowFocusLevel(View view, float level)

    Set shadow focus level (0 to 1).

    void setOverlayColor(View view, int color)

    Set overlay color for view, it can be a ShadowOverlayContainer if needsWrapper() is true, or other view type.

    void setShadowFocusLevel(View view, float level)

    Set shadow focus level (0 to 1).

    static boolean supportsDynamicShadow()

    Returns true if the platform sdk supports dynamic shadows.

    static boolean supportsForeground()

    Returns true if view.setForeground() is supported.

    static boolean supportsRoundedCorner()

    Returns true if the platform sdk supports rounded corner through outline.

    static boolean supportsShadow()

    Return true if the platform sdk supports shadow.

    Inherited methods

    Constants

    SHADOW_DYNAMIC

    added in version 1.0.0
    public static final int SHADOW_DYNAMIC

    Shadows depend on the size, shape, and position of the view.

    Constant Value: 3 (0x00000003)

    SHADOW_NONE

    added in version 1.0.0
    public static final int SHADOW_NONE

    No shadow.

    Constant Value: 1 (0x00000001)

    SHADOW_STATIC

    added in version 1.0.0
    public static final int SHADOW_STATIC

    Shadows are fixed.

    Constant Value: 2 (0x00000002)

    Public methods

    createShadowOverlayContainer

    added in version 1.0.0
    public ShadowOverlayContainer createShadowOverlayContainer (Context context)

    Create ShadowOverlayContainer for this helper.

    Parameters
    context Context: Context to create view.

    Returns
    ShadowOverlayContainer ShadowOverlayContainer.

    getShadowType

    added in version 1.0.0
    public int getShadowType ()

    Returns
    int

    needsOverlay

    added in version 1.0.0
    public boolean needsOverlay ()

    Returns
    boolean

    needsRoundedCorner

    added in version 1.0.0
    public boolean needsRoundedCorner ()

    Returns
    boolean

    needsWrapper

    added in version 1.0.0
    public boolean needsWrapper ()

    Returns true if a "wrapper" ShadowOverlayContainer is needed. When needsWrapper() is true, call createShadowOverlayContainer(Context) to create the wrapper.

    Returns
    boolean

    onViewCreated

    added in version 1.0.0
    public void onViewCreated (View view)

    Must be called when view is created for cases needsWrapper() is false.

    prepareParentForShadow

    added in version 1.0.0
    public void prepareParentForShadow (ViewGroup parent)

    prepareParentForShadow(ViewGroup) must be called on parent of container before using shadow. Depending on Shadow type, optical bounds might be applied.

    Parameters
    parent ViewGroup

    setNoneWrapperOverlayColor

    added in version 1.0.0
    public static void setNoneWrapperOverlayColor (View view, 
                    int color)

    Set overlay color for view other than ShadowOverlayContainer. See also ShadowOverlayContainer.setOverlayColor(int).

    Parameters
    view View

    color int

    setNoneWrapperShadowFocusLevel

    added in version 1.0.0
    public static void setNoneWrapperShadowFocusLevel (View view, 
                    float level)

    Set shadow focus level (0 to 1). 0 for unfocused, 1 for fully focused. This is for view other than ShadowOverlayContainer. See also ShadowOverlayContainer.setShadowFocusLevel(float).

    Parameters
    view View

    level float

    setOverlayColor

    added in version 1.0.0
    public void setOverlayColor (View view, 
                    int color)

    Set overlay color for view, it can be a ShadowOverlayContainer if needsWrapper() is true, or other view type.

    Parameters
    view View

    color int

    setShadowFocusLevel

    added in version 1.0.0
    public void setShadowFocusLevel (View view, 
                    float level)

    Set shadow focus level (0 to 1). 0 for unfocused, 1 for fully focused.

    Parameters
    view View

    level float

    supportsDynamicShadow

    added in version 1.0.0
    public static boolean supportsDynamicShadow ()

    Returns true if the platform sdk supports dynamic shadows.

    Returns
    boolean

    supportsForeground

    added in version 1.0.0
    public static boolean supportsForeground ()

    Returns true if view.setForeground() is supported.

    Returns
    boolean

    supportsRoundedCorner

    added in version 1.0.0
    public static boolean supportsRoundedCorner ()

    Returns true if the platform sdk supports rounded corner through outline.

    Returns
    boolean

    supportsShadow

    added in version 1.0.0
    public static boolean supportsShadow ()

    Return true if the platform sdk supports shadow.

    Returns
    boolean