diff --git a/android/src/main/java/com/horcrux/svg/RNSVGSvgView.java b/android/src/main/java/com/horcrux/svg/RNSVGSvgView.java index f25ca37f..9ab3d276 100644 --- a/android/src/main/java/com/horcrux/svg/RNSVGSvgView.java +++ b/android/src/main/java/com/horcrux/svg/RNSVGSvgView.java @@ -9,11 +9,12 @@ package com.horcrux.svg; +import android.graphics.Bitmap; +import android.graphics.Canvas; import android.graphics.Point; import android.util.Log; import android.view.MotionEvent; import android.view.View; -import android.view.TextureView; import com.facebook.react.ReactRootView; import com.facebook.react.bridge.Arguments; @@ -26,10 +27,12 @@ import com.facebook.react.uimanager.events.TouchEventCoalescingKeyHelper; import com.facebook.react.uimanager.events.TouchEventType; import com.facebook.react.uimanager.events.EventDispatcher; +import javax.annotation.Nullable; + /** * Custom {@link View} implementation that draws an RNSVGSvg React view and its \children. */ -public class RNSVGSvgView extends TextureView { +public class RNSVGSvgView extends View { public enum Events { EVENT_DATA_URL("onDataURL"); @@ -45,6 +48,7 @@ public class RNSVGSvgView extends TextureView { } } + private @Nullable Bitmap mBitmap; private RCTEventEmitter mEventEmitter; private EventDispatcher mEventDispatcher; private int mTargetTag; @@ -54,7 +58,6 @@ public class RNSVGSvgView extends TextureView { public RNSVGSvgView(ReactContext reactContext) { super(reactContext); - setOpaque(false); mEventEmitter = reactContext.getJSModule(RCTEventEmitter.class); mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); } @@ -63,6 +66,22 @@ public class RNSVGSvgView extends TextureView { return RNSVGSvgViewShadowNode.getShadowNodeByTag(getId()); } + 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); + } + } + @Override public boolean dispatchTouchEvent(MotionEvent ev) { mTargetTag = getShadowNode().hitTest(new Point((int) ev.getX(), (int) ev.getY())); diff --git a/android/src/main/java/com/horcrux/svg/RNSVGSvgViewManager.java b/android/src/main/java/com/horcrux/svg/RNSVGSvgViewManager.java index 5e585548..4fb9103a 100644 --- a/android/src/main/java/com/horcrux/svg/RNSVGSvgViewManager.java +++ b/android/src/main/java/com/horcrux/svg/RNSVGSvgViewManager.java @@ -9,6 +9,8 @@ package com.horcrux.svg; +import android.graphics.Bitmap; + import com.facebook.react.bridge.ReadableArray; import com.facebook.react.common.MapBuilder; import com.facebook.react.uimanager.BaseViewManager; @@ -50,7 +52,7 @@ public class RNSVGSvgViewManager extends BaseViewManager mTagToShadowNode = new SparseArray<>(); @@ -43,7 +37,6 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode implements TextureV return mTagToShadowNode.get(tag); } - private @Nullable Surface mSurface; private boolean mResponsible = false; private static final Map mDefinedClipPaths = new HashMap<>(); private static final Map mDefinedTemplates = new HashMap<>(); @@ -62,27 +55,25 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode implements TextureV @Override public void onCollectExtraUpdates(UIViewOperationQueue uiUpdater) { super.onCollectExtraUpdates(uiUpdater); - drawOutput(); - uiUpdater.enqueueUpdateExtraData(getReactTag(), this); + uiUpdater.enqueueUpdateExtraData(getReactTag(), drawOutput()); + } - public void drawOutput() { - if (mSurface == null || !mSurface.isValid()) { - markChildrenUpdatesSeen(this); - return; - } + @Override + public void setReactTag(int reactTag) { + super.setReactTag(reactTag); + mTagToShadowNode.put(getReactTag(), this); + } - try { - Canvas canvas = mSurface.lockCanvas(null); - drawChildren(canvas); + public Object drawOutput() { + Bitmap bitmap = Bitmap.createBitmap( + (int) getLayoutWidth(), + (int) getLayoutHeight(), + Bitmap.Config.ARGB_8888); - 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 drawChildren(Canvas canvas) { @@ -106,14 +97,6 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode implements TextureV } } - private void markChildrenUpdatesSeen(ReactShadowNode shadowNode) { - for (int i = 0; i < shadowNode.getChildCount(); i++) { - ReactShadowNode child = shadowNode.getChildAt(i); - child.markUpdateSeen(); - markChildrenUpdatesSeen(child); - } - } - public String getBase64() { Bitmap bitmap = Bitmap.createBitmap( (int) getLayoutWidth(), @@ -128,27 +111,6 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode implements TextureV return Base64.encodeToString(bitmapBytes, Base64.DEFAULT); } - @Override - public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { - mSurface = new Surface(surface); - mTagToShadowNode.put(getReactTag(), this); - drawOutput(); - } - - @Override - public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { - mTagToShadowNode.remove(getReactTag()); - surface.release(); - mSurface = null; - return true; - } - - @Override - public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {} - - @Override - public void onSurfaceTextureUpdated(SurfaceTexture surface) {} - public void enableTouchEvents() { if (!mResponsible) { mResponsible = true; @@ -199,4 +161,8 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode implements TextureV public PropHelper.RNSVGBrush getDefinedBrush(String brushRef) { return mDefinedBrushes.get(brushRef); } + + public void finalize() { + mTagToShadowNode.remove(getReactTag()); + } } diff --git a/package.json b/package.json index 095b389a..1e234455 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "4.3.2", + "version": "4.3.3", "name": "react-native-svg", "description": "SVG library for react-native", "repository": {