ShadowOverlayHelper
public final 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.
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 |
|---|
public final class ShadowOverlayHelper.BuilderBuilder for creating ShadowOverlayHelper. |
public final class ShadowOverlayHelper.OptionsOption values for ShadowOverlayContainer. |
Constants |
|
|---|---|
static final int |
SHADOW_DYNAMIC = 3Shadows depend on the size, shape, and position of the view. |
static final int |
SHADOW_NONE = 1No shadow. |
static final int |
SHADOW_STATIC = 2Shadows are fixed. |
Public methods |
|
|---|---|
ShadowOverlayContainer |
createShadowOverlayContainer(Context context)Create ShadowOverlayContainer for this helper. |
int |
|
boolean |
|
boolean |
|
boolean |
Returns true if a "wrapper" ShadowOverlayContainer is needed. |
void |
onViewCreated(View view)Must be called when view is created for cases |
void |
prepareParentForShadow(ViewGroup parent)
|
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 |
Returns true if the platform sdk supports dynamic shadows. |
static boolean |
Returns true if view.setForeground() is supported. |
static boolean |
Returns true if the platform sdk supports rounded corner through outline. |
static boolean |
Return true if the platform sdk supports shadow. |
Constants
SHADOW_DYNAMIC
public static final int SHADOW_DYNAMIC = 3
Shadows depend on the size, shape, and position of the view.
Public methods
createShadowOverlayContainer
public ShadowOverlayContainer createShadowOverlayContainer(Context context)
Create ShadowOverlayContainer for this helper.
| Parameters | |
|---|---|
Context context |
Context to create view. |
| Returns | |
|---|---|
ShadowOverlayContainer |
ShadowOverlayContainer. |
needsWrapper
public boolean needsWrapper()
Returns true if a "wrapper" ShadowOverlayContainer is needed. When needsWrapper() is true, call createShadowOverlayContainer to create the wrapper.
onViewCreated
public void onViewCreated(View view)
Must be called when view is created for cases needsWrapper is false.
| Parameters | |
|---|---|
View view |
prepareParentForShadow
public void prepareParentForShadow(ViewGroup parent)
prepareParentForShadow must be called on parent of container before using shadow. Depending on Shadow type, optical bounds might be applied.
setNoneWrapperOverlayColor
public static void setNoneWrapperOverlayColor(View view, int color)
Set overlay color for view other than ShadowOverlayContainer. See also setOverlayColor.
setNoneWrapperShadowFocusLevel
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 setShadowFocusLevel.
setOverlayColor
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.
setShadowFocusLevel
public void setShadowFocusLevel(View view, float level)
Set shadow focus level (0 to 1). 0 for unfocused, 1 for fully focused.
supportsDynamicShadow
public static boolean supportsDynamicShadow()
Returns true if the platform sdk supports dynamic shadows.
supportsForeground
public static boolean supportsForeground()
Returns true if view.setForeground() is supported.
supportsRoundedCorner
public static boolean supportsRoundedCorner()
Returns true if the platform sdk supports rounded corner through outline.
supportsShadow
public static boolean supportsShadow()
Return true if the platform sdk supports shadow.