Add support for shadowColor on Android (API >= 28) (#28650)

Summary:
This PR adds support for the `shadowColor` style on Android.

This is possible as of Android P using the `setOutlineAmbientShadowColor` and `setOutlineSpotShadowColor` View methods. The actual rendered color is a multiplication of the color-alpha, shadow-effect and elevation-value.

## Changelog

`[Android] [Added] - Add support for shadowColor on API level >= 28`
Pull Request resolved: https://github.com/facebook/react-native/pull/28650

Test Plan:
- Only execute code on Android P
- Added Android `BoxShadow` tests to RNTester app

![image](https://user-images.githubusercontent.com/6184593/79457137-fe627c80-7fef-11ea-8e88-3d9423a4f264.png)

Reviewed By: mdvacca

Differential Revision: D21125479

Pulled By: shergin

fbshipit-source-id: 14dcc023977d7a9d304fabcd3c90bcf34482f137
This commit is contained in:
Hein Rutjes
2020-07-07 23:30:10 -07:00
committed by Facebook GitHub Bot
parent e96f1e1d83
commit cfa4260598
8 changed files with 120 additions and 2 deletions
@@ -95,6 +95,15 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
ViewCompat.setElevation(view, PixelUtil.toPixelFromDIP(elevation));
}
@Override
@ReactProp(name = ViewProps.SHADOW_COLOR, defaultInt = Color.BLACK, customType = "Color")
public void setShadowColor(@NonNull T view, int shadowColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
view.setOutlineAmbientShadowColor(shadowColor);
view.setOutlineSpotShadowColor(shadowColor);
}
}
@Override
@ReactProp(name = ViewProps.Z_INDEX)
public void setZIndex(@NonNull T view, float zIndex) {
@@ -54,6 +54,9 @@ public abstract class BaseViewManagerAdapter<T extends View>
@Override
public void setElevation(@NonNull T view, float elevation) {}
@Override
public void setShadowColor(@NonNull T view, int shadowColor) {}
@Override
public void setImportantForAccessibility(
@NonNull T view, @Nullable String importantForAccessibility) {}
@@ -74,6 +74,10 @@ public abstract class BaseViewManagerDelegate<T extends View, U extends BaseView
case ViewProps.ELEVATION:
mViewManager.setElevation(view, value == null ? 0.0f : ((Double) value).floatValue());
break;
case ViewProps.SHADOW_COLOR:
mViewManager.setShadowColor(
view, value == null ? 0 : ColorPropConverter.getColor(value, view.getContext()));
break;
case ViewProps.IMPORTANT_FOR_ACCESSIBILITY:
mViewManager.setImportantForAccessibility(view, (String) value);
break;
@@ -43,6 +43,8 @@ public interface BaseViewManagerInterface<T extends View> {
void setElevation(T view, float elevation);
void setShadowColor(T view, int shadowColor);
void setImportantForAccessibility(T view, @Nullable String importantForAccessibility);
void setNativeId(T view, @Nullable String nativeId);
@@ -140,6 +140,7 @@ public class ViewProps {
public static final String TRANSFORM = "transform";
public static final String ELEVATION = "elevation";
public static final String SHADOW_COLOR = "shadowColor";
public static final String Z_INDEX = "zIndex";
public static final String RENDER_TO_HARDWARE_TEXTURE = "renderToHardwareTextureAndroid";
public static final String ACCESSIBILITY_LABEL = "accessibilityLabel";