Fix Image not rendering the first time svg is displayed

Fix Image not rendering the first time svg is displayed
Add toDataURL method for Svg elements for Android (not finished yet)
This commit is contained in:
Horcrux
2016-08-11 16:27:15 +08:00
parent 76a3e8e32d
commit 857b5f5207
6 changed files with 81 additions and 44 deletions
@@ -110,8 +110,9 @@ public class RNSVGImageShadowNode extends RNSVGPathShadowNode {
paint.reset();
mLoading.set(false);
getSvgShadowNode().drawChildren(canvas, paint);
getSvgShadowNode().invalidateView(getRect());
RNSVGSvgViewShadowNode svgShadowNode = getSvgShadowNode();
svgShadowNode.drawChildren(canvas, paint);
svgShadowNode.invalidateView();
}
}
@@ -44,11 +44,6 @@ public class RNSVGSvgView extends ViewGroup {
private final TouchEventCoalescingKeyHelper mTouchEventCoalescingKeyHelper =
new TouchEventCoalescingKeyHelper();
public RNSVGSvgView(Context context, RNSVGSvgViewShadowNode shadowNode) {
super(context);
mSvgViewShadowNode = shadowNode;
}
public RNSVGSvgView(Context context) {
super(context);
}
@@ -86,6 +81,11 @@ public class RNSVGSvgView extends ViewGroup {
}
public void setShadowNode(RNSVGSvgViewShadowNode shadowNode) {
mSvgViewShadowNode = shadowNode;
shadowNode.setSvgView(this);
}
public void handleTouchEvent(MotionEvent ev, EventDispatcher eventDispatcher) {
int action = ev.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_DOWN) {
@@ -10,13 +10,29 @@
package com.horcrux.svg;
import android.graphics.Bitmap;
import android.telecom.Call;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.BaseViewManager;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManagerPropertyUpdater;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.security.auth.callback.Callback;
/**
* ViewManager for RNSVGSvgView React views. Renders as a {@link RNSVGSvgView} and handles
@@ -25,26 +41,54 @@ import javax.annotation.Nullable;
public class RNSVGSvgViewManager extends ViewGroupManager<RNSVGSvgView> {
private static final String REACT_CLASS = "RNSVGSvgView";
private RNSVGSvgView svgView = null;
// TODO: use an ArrayList to connect RNSVGSvgViewShadowNode with RNSVGSvgView, not sure if there will be a race condition.
// TODO: find a better way to replace this
private ArrayList<RNSVGSvgViewShadowNode> SvgShadowNodes = new ArrayList<>();
private ArrayList<RNSVGSvgViewShadowNode> mSvgShadowNodes = new ArrayList<>();
public static final int COMMAND_TO_DATA_URL = 100;
@Override
public String getName() {
return REACT_CLASS;
}
@Nullable
public RNSVGSvgView getSvgView() {
return this.svgView;
@Override
public @Nullable Map<String, Integer> getCommandsMap() {
Map<String, Integer> commandsMap = super.getCommandsMap();
if (commandsMap == null) {
commandsMap = new HashMap<>();
}
commandsMap.put("toDataURL", COMMAND_TO_DATA_URL);
return commandsMap;
}
@Override
public void receiveCommand(RNSVGSvgView root, int commandId, @Nullable ReadableArray args) {
super.receiveCommand(root, commandId, args);
switch (commandId) {
case COMMAND_TO_DATA_URL:
toDataURL(root);
break;
}
}
private void toDataURL(RNSVGSvgView root) {
WritableMap event = Arguments.createMap();
event.putString("message", "MyMessage");
ReactContext reactContext = (ReactContext)root.getContext();
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
root.getId(),
"onDataURL",
event);
}
@Override
public RNSVGSvgViewShadowNode createShadowNodeInstance() {
RNSVGSvgViewShadowNode node = new RNSVGSvgViewShadowNode(this);
SvgShadowNodes.add(node);
RNSVGSvgViewShadowNode node = new RNSVGSvgViewShadowNode();
mSvgShadowNodes.add(node);
return node;
}
@@ -55,10 +99,9 @@ public class RNSVGSvgViewManager extends ViewGroupManager<RNSVGSvgView> {
@Override
protected RNSVGSvgView createViewInstance(ThemedReactContext reactContext) {
RNSVGSvgViewShadowNode shadowNode = SvgShadowNodes.get(0);
SvgShadowNodes.remove(0);
this.svgView = new RNSVGSvgView(reactContext, shadowNode);
return this.svgView;
RNSVGSvgView svgView = new RNSVGSvgView(reactContext);
svgView.setShadowNode(mSvgShadowNodes.remove(0));
return svgView;
}
@Override
@@ -14,11 +14,13 @@ import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.UIViewOperationQueue;
import java.util.HashMap;
@@ -32,17 +34,11 @@ import javax.annotation.Nonnull;
public class RNSVGSvgViewShadowNode extends LayoutShadowNode {
private boolean mResponsible = false;
private RNSVGSvgView mSvgView;
private static final Map<String, RNSVGVirtualNode> mDefinedClipPaths = new HashMap<>();
private static final Map<String, RNSVGVirtualNode> mDefinedTemplates = new HashMap<>();
private static final Map<String, PropHelper.RNSVGBrush> mDefinedBrushes = new HashMap<>();
@Nonnull private final RNSVGSvgViewManager viewManager;
public RNSVGSvgViewShadowNode(@Nonnull final RNSVGSvgViewManager viewManager) {
super();
this.viewManager = viewManager;
}
@Override
public void onCollectExtraUpdates(UIViewOperationQueue uiUpdater) {
super.onCollectExtraUpdates(uiUpdater);
@@ -88,16 +84,6 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode {
}
}
protected void invalidateView(@Nonnull final Rect dirtyRect) {
final RNSVGSvgView svgView = this.viewManager.getSvgView();
if (svgView != null) {
final View rootView = svgView.getRootView();
if (rootView != null) {
rootView.invalidate(dirtyRect);
}
}
}
public void enableTouchEvents() {
if (!mResponsible) {
mResponsible = true;
@@ -145,7 +131,15 @@ public class RNSVGSvgViewShadowNode extends LayoutShadowNode {
mDefinedBrushes.put(brushRef, brush);
}
public PropHelper.RNSVGBrush getDefinedBrush(String brushRef) {
public PropHelper.RNSVGBrush getDefinedBrush(String brushRef) {
return mDefinedBrushes.get(brushRef);
}
public void setSvgView(RNSVGSvgView svgView) {
mSvgView = svgView;
}
protected void invalidateView() {
mSvgView.invalidate();
}
}