Skip to content

Commit

Permalink
Android: Support setting background color on ARTSurfaceView
Browse files Browse the repository at this point in the history
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 #14117

Differential Revision: D5419574

Pulled By: hramos

fbshipit-source-id: 022bfed553e33e2d493f68b4bf5aa16dd304934d
  • Loading branch information
Adam Comella authored and facebook-github-bot committed Jul 13, 2017
1 parent c885357 commit 09401ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ protected ARTSurfaceView createViewInstance(ThemedReactContext reactContext) {
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.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
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
Expand All @@ -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;
Expand All @@ -59,6 +69,9 @@ private void drawOutput() {
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++) {
Expand Down

0 comments on commit 09401ed

Please sign in to comment.