Android: Support setting background color on ARTSurfaceView

Summary:
This fixes support for the `backgroundColor` style prop on an `ART.Surface`.

`ARTSurfaceViewManager` inherits its `setBackgroundColor` implementation from `BaseViewManager`. This implementation broke in API 24 because API 24 removed support for `setBackgroundColor`on `TextureViews`. `ARTSurfaceView` inherits from `TextureView` so it also lost support for `setBackgroundColor`.

To fix this, the implementation of `ART.Surface's` `setBackgroundColor` was moved to the shadow node. The implementation now draws the background color on the canvas.

In a test app, verified that initializing and changing the background color on an `ART.Surface` works. Verified that the background renders as transparent when a background color isn't set. Also, this change is being used in my team's app.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/14117

Differential Revision: D5419574

Pulled By: hramos

fbshipit-source-id: 022bfed553e33e2d493f68b4bf5aa16dd304934d
This commit is contained in:
Adam Comella 2017-07-13 16:07:53 -07:00 committed by Facebook Github Bot
parent c885357cc9
commit 09401ed56f
2 changed files with 21 additions and 0 deletions

View File

@ -64,4 +64,12 @@ public class ARTSurfaceViewManager extends
public void updateExtraData(ARTSurfaceView root, Object extraData) {
root.setSurfaceTextureListener((ARTSurfaceViewShadowNode) extraData);
}
@Override
public void setBackgroundColor(ARTSurfaceView view, int backgroundColor) {
// As of Android N TextureView does not support calling setBackground on it.
// It will also throw an exception when target SDK is set to N or higher.
// Setting the background color for this view is handled in the shadow node.
}
}

View File

@ -24,6 +24,8 @@ import com.facebook.react.common.ReactConstants;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.UIViewOperationQueue;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
/**
* Shadow node for ART virtual tree root - ARTSurfaceView
@ -33,6 +35,14 @@ public class ARTSurfaceViewShadowNode extends LayoutShadowNode
private @Nullable Surface mSurface;
private @Nullable Integer mBackgroundColor;
@ReactProp(name = ViewProps.BACKGROUND_COLOR, customType = "Color")
public void setBackgroundColor(Integer color) {
mBackgroundColor = color;
markUpdated();
}
@Override
public boolean isVirtual() {
return false;
@ -59,6 +69,9 @@ public class ARTSurfaceViewShadowNode extends LayoutShadowNode
try {
Canvas canvas = mSurface.lockCanvas(null);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
if (mBackgroundColor != null) {
canvas.drawColor(mBackgroundColor);
}
Paint paint = new Paint();
for (int i = 0; i < getChildCount(); i++) {