From 16231226d78ab7f1db1d6213a4cea758ae9313a2 Mon Sep 17 00:00:00 2001 From: Horcrux Date: Sun, 8 Jan 2017 21:19:25 +0800 Subject: [PATCH] Fallback to bitmap again! --- .../main/java/com/horcrux/svg/SvgView.java | 22 +++++++- .../java/com/horcrux/svg/SvgViewManager.java | 2 +- .../com/horcrux/svg/SvgViewShadowNode.java | 56 ++++--------------- 3 files changed, 30 insertions(+), 50 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/SvgView.java b/android/src/main/java/com/horcrux/svg/SvgView.java index bbc674347..77308be9d 100644 --- a/android/src/main/java/com/horcrux/svg/SvgView.java +++ b/android/src/main/java/com/horcrux/svg/SvgView.java @@ -31,9 +31,9 @@ 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 String toString() { } } + private @Nullable Bitmap mBitmap; private RCTEventEmitter mEventEmitter; private EventDispatcher mEventDispatcher; private int mTargetTag; @@ -58,11 +59,26 @@ public String toString() { 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()); } diff --git a/android/src/main/java/com/horcrux/svg/SvgViewManager.java b/android/src/main/java/com/horcrux/svg/SvgViewManager.java index e8f15ad99..14241d3cf 100644 --- a/android/src/main/java/com/horcrux/svg/SvgViewManager.java +++ b/android/src/main/java/com/horcrux/svg/SvgViewManager.java @@ -68,7 +68,7 @@ protected SvgView createViewInstance(ThemedReactContext reactContext) { @Override public void updateExtraData(SvgView root, Object extraData) { - root.setSurfaceTextureListener((SvgViewShadowNode) extraData); + root.setBitmap((Bitmap) extraData); } @Override diff --git a/android/src/main/java/com/horcrux/svg/SvgViewShadowNode.java b/android/src/main/java/com/horcrux/svg/SvgViewShadowNode.java index 6f92aed87..3ec062cf0 100644 --- a/android/src/main/java/com/horcrux/svg/SvgViewShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/SvgViewShadowNode.java @@ -36,7 +36,7 @@ /** * 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 mTagToShadowNode = new SparseArray<>(); private @Nullable Surface mSurface; @@ -64,8 +64,7 @@ public boolean isVirtualAnchor() { @Override public void onCollectExtraUpdates(UIViewOperationQueue uiUpdater) { super.onCollectExtraUpdates(uiUpdater); - drawOutput(); - uiUpdater.enqueueUpdateExtraData(getReactTag(), this); + uiUpdater.enqueueUpdateExtraData(getReactTag(), drawOutput()); } @Override @@ -74,52 +73,17 @@ public void setReactTag(int reactTag) { mTagToShadowNode.put(getReactTag(), this); } - public void drawOutput() { - if (mSurface == null || !mSurface.isValid()) { - markChildrenUpdatesSeen(this); - return; - } - - 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"); - } - } - - 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(); - } + public Object drawOutput() { + Bitmap bitmap = Bitmap.createBitmap( + (int) getLayoutWidth(), + (int) getLayoutHeight(), + Bitmap.Config.ARGB_8888); - @Override - public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { - surface.release(); - mSurface = null; - return true; + Canvas canvas = new Canvas(bitmap); + drawChildren(canvas); + return bitmap; } - @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();