ShadowOverlayHelper


class 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 types

Builder for creating ShadowOverlayHelper.

Option values for ShadowOverlayContainer.

Constants

const Int

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

const Int

No shadow.

const Int

Shadows are fixed.

Public functions

ShadowOverlayContainer!

Create ShadowOverlayContainer for this helper.

Int
Boolean
Boolean
Boolean

Returns true if a "wrapper" ShadowOverlayContainer is needed.

Unit

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

Unit

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

java-static Unit

Set overlay color for view other than ShadowOverlayContainer.

java-static Unit

Set shadow focus level (0 to 1).

Unit
setOverlayColor(view: View!, color: Int)

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

Unit
setShadowFocusLevel(view: View!, level: Float)

Set shadow focus level (0 to 1).

java-static Boolean

Returns true if the platform sdk supports dynamic shadows.

java-static Boolean

Returns true if view.setForeground() is supported.

java-static Boolean

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

java-static Boolean

Return true if the platform sdk supports shadow.

Constants

SHADOW_DYNAMIC

Added in 1.1.0
const val SHADOW_DYNAMIC = 3: Int

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

SHADOW_NONE

Added in 1.1.0
const val SHADOW_NONE = 1: Int

No shadow.

SHADOW_STATIC

Added in 1.1.0
const val SHADOW_STATIC = 2: Int

Shadows are fixed.

Public functions

createShadowOverlayContainer

Added in 1.1.0
fun createShadowOverlayContainer(context: Context!): ShadowOverlayContainer!

Create ShadowOverlayContainer for this helper.

Parameters
context: Context!

Context to create view.

Returns
ShadowOverlayContainer!

ShadowOverlayContainer.

getShadowType

Added in 1.1.0
fun getShadowType(): Int

needsOverlay

Added in 1.1.0
fun needsOverlay(): Boolean

needsRoundedCorner

Added in 1.1.0
fun needsRoundedCorner(): Boolean

needsWrapper

Added in 1.1.0
fun needsWrapper(): Boolean

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

onViewCreated

Added in 1.1.0
fun onViewCreated(view: View!): Unit

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

Parameters
view: View!

prepareParentForShadow

Added in 1.1.0
fun prepareParentForShadow(parent: ViewGroup!): Unit

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

setNoneWrapperOverlayColor

Added in 1.1.0
java-static fun setNoneWrapperOverlayColor(view: View!, color: Int): Unit

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

setNoneWrapperShadowFocusLevel

Added in 1.1.0
java-static fun setNoneWrapperShadowFocusLevel(view: View!, level: Float): Unit

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

setOverlayColor

Added in 1.1.0
fun setOverlayColor(view: View!, color: Int): Unit

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

setShadowFocusLevel

Added in 1.1.0
fun setShadowFocusLevel(view: View!, level: Float): Unit

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

supportsDynamicShadow

Added in 1.1.0
java-static fun supportsDynamicShadow(): Boolean

Returns true if the platform sdk supports dynamic shadows.

supportsForeground

Added in 1.1.0
java-static fun supportsForeground(): Boolean

Returns true if view.setForeground() is supported.

supportsRoundedCorner

Added in 1.1.0
java-static fun supportsRoundedCorner(): Boolean

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

supportsShadow

Added in 1.1.0
java-static fun supportsShadow(): Boolean

Return true if the platform sdk supports shadow.