Implement version of toDataURL with width and height options

https://github.com/react-native-community/react-native-svg/issues/855
This commit is contained in:
Mikael Sand
2019-02-10 03:42:18 +02:00
parent e69d2320b2
commit 131ddb6a1a
6 changed files with 78 additions and 4 deletions
@@ -306,6 +306,20 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
String toDataURL(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(
width,
height,
Bitmap.Config.ARGB_8888);
drawChildren(new Canvas(bitmap));
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmap.recycle();
byte[] bitmapBytes = stream.toByteArray();
return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
void enableTouchEvents() {
if (!mResponsible) {
mResponsible = true;
@@ -13,6 +13,7 @@ import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
class SvgViewModule extends ReactContextBaseJavaModule {
SvgViewModule(ReactApplicationContext reactContext) {
@@ -33,4 +34,19 @@ class SvgViewModule extends ReactContextBaseJavaModule {
successCallback.invoke(svg.toDataURL());
}
}
@ReactMethod
public void toDataURL(int tag, ReadableMap options, Callback successCallback) {
SvgView svg = SvgViewManager.getSvgViewByTag(tag);
if (svg != null) {
successCallback.invoke(
svg.toDataURL(
options.getInt("width"),
options.getInt("height")
)
);
}
}
}