Fallback to bitmap again!

This commit is contained in:
Horcrux
2017-01-08 21:19:25 +08:00
parent 074166b111
commit 16231226d7
3 changed files with 30 additions and 50 deletions
@@ -31,9 +31,9 @@ import com.facebook.react.uimanager.events.EventDispatcher;
import javax.annotation.Nullable;
/**
* Custom {@link View} implementation that draws an RNSVGSvg React view and its \children.
* Custom {@link View} implementation that draws an RNSVGSvg React view and its children.
*/
public class SvgView extends TextureView {
public class SvgView extends View {
public enum Events {
EVENT_DATA_URL("onDataURL");
@@ -49,6 +49,7 @@ public class SvgView extends TextureView {
}
}
private @Nullable Bitmap mBitmap;
private RCTEventEmitter mEventEmitter;
private EventDispatcher mEventDispatcher;
private int mTargetTag;
@@ -58,11 +59,26 @@ public class SvgView extends TextureView {
public SvgView(ReactContext reactContext) {
super(reactContext);
setOpaque(false);
mEventEmitter = reactContext.getJSModule(RCTEventEmitter.class);
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
}
public void setBitmap(Bitmap bitmap) {
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = bitmap;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
private SvgViewShadowNode getShadowNode() {
return SvgViewShadowNode.getShadowNodeByTag(getId());
}
@@ -68,7 +68,7 @@ public class SvgViewManager extends BaseViewManager<SvgView, SvgViewShadowNode>
@Override
public void updateExtraData(SvgView root, Object extraData) {
root.setSurfaceTextureListener((SvgViewShadowNode) extraData);
root.setBitmap((Bitmap) extraData);
}
@Override
@@ -36,7 +36,7 @@ import java.util.Map;
/**
* Shadow node for RNSVG virtual tree root - RNSVGSvgView
*/
public class SvgViewShadowNode extends LayoutShadowNode implements TextureView.SurfaceTextureListener {
public class SvgViewShadowNode extends LayoutShadowNode {
private static final SparseArray<SvgViewShadowNode> mTagToShadowNode = new SparseArray<>();
private @Nullable Surface mSurface;
@@ -64,8 +64,7 @@ public class SvgViewShadowNode extends LayoutShadowNode implements TextureView.S
@Override
public void onCollectExtraUpdates(UIViewOperationQueue uiUpdater) {
super.onCollectExtraUpdates(uiUpdater);
drawOutput();
uiUpdater.enqueueUpdateExtraData(getReactTag(), this);
uiUpdater.enqueueUpdateExtraData(getReactTag(), drawOutput());
}
@Override
@@ -74,52 +73,17 @@ public class SvgViewShadowNode extends LayoutShadowNode implements TextureView.S
mTagToShadowNode.put(getReactTag(), this);
}
public void drawOutput() {
if (mSurface == null || !mSurface.isValid()) {
markChildrenUpdatesSeen(this);
return;
}
public Object drawOutput() {
Bitmap bitmap = Bitmap.createBitmap(
(int) getLayoutWidth(),
(int) getLayoutHeight(),
Bitmap.Config.ARGB_8888);
try {
Canvas canvas = mSurface.lockCanvas(null);
drawChildren(canvas);
if (mSurface != null) {
mSurface.unlockCanvasAndPost(canvas);
}
} catch (IllegalArgumentException | IllegalStateException e) {
FLog.e(ReactConstants.TAG, e.getClass().getSimpleName() + " in Svg.unlockCanvasAndPost");
}
Canvas canvas = new Canvas(bitmap);
drawChildren(canvas);
return bitmap;
}
private void markChildrenUpdatesSeen(ReactShadowNode shadowNode) {
for (int i = 0; i < shadowNode.getChildCount(); i++) {
ReactShadowNode child = shadowNode.getChildAt(i);
child.markUpdateSeen();
markChildrenUpdatesSeen(child);
}
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurface = new Surface(surface);
drawOutput();
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
surface.release();
mSurface = null;
return true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
private void drawChildren(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Paint paint = new Paint();