Skip to content

Commit bb92625

Browse files
authored
Merge pull request #35 from manweill/master
Fix ScrollView Snapshots
2 parents d7f3fd8 + fe420b1 commit bb92625

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Returns a Promise of the image URI.
4242
- `"base64"`: encode as base64 and returns the raw string. Use only with small images as this may result of lags (the string is sent over the bridge). *N.B. This is not a data uri, use `data-uri` instead*.
4343
- `"data-uri"`: same as `base64` but also includes the [Data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) header.
4444
- **`filename`** *(string)*: the name of the generated file if any (Android only). Defaults to `ReactNative_snapshot_image_${timestamp}`.
45+
- **`snapshotContentContainer`** *(bool)*: if true and when view is a ScrollView, the "content container" height will be evaluated instead of the container height. (Android only)
4546

4647
## Caveats
4748

android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ public void takeSnapshot(int tag, ReadableMap options, Promise promise) {
6464
Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
6565
Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
6666
String result = options.hasKey("result") ? options.getString("result") : "file";
67+
Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer") : false;
6768
try {
6869
String name = options.hasKey("filename") ? options.getString("filename") : null;
6970
File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
7071
UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
71-
uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, promise));
72+
uiManager.addUIBlock(new ViewShot(tag, format, compressFormat, quality, width, height, tmpFile, result, snapshotContentContainer, promise));
7273
}
7374
catch (Exception e) {
7475
promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);

android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package fr.greweb.reactnativeviewshot;
22

33
import javax.annotation.Nullable;
4+
5+
import android.app.Activity;
46
import android.graphics.Bitmap;
57
import android.graphics.Canvas;
68
import android.net.Uri;
79
import android.util.Base64;
810
import android.view.View;
11+
import android.widget.ScrollView;
912

1013
import com.facebook.react.bridge.Promise;
1114
import com.facebook.react.uimanager.NativeViewHierarchyManager;
@@ -33,6 +36,7 @@ public class ViewShot implements UIBlock {
3336
private File output;
3437
private String result;
3538
private Promise promise;
39+
private Boolean snapshotContentContainer;
3640

3741
public ViewShot(
3842
int tag,
@@ -43,6 +47,7 @@ public ViewShot(
4347
@Nullable Integer height,
4448
File output,
4549
String result,
50+
Boolean snapshotContentContainer,
4651
Promise promise) {
4752
this.tag = tag;
4853
this.extension = extension;
@@ -52,6 +57,7 @@ public ViewShot(
5257
this.height = height;
5358
this.output = output;
5459
this.result = result;
60+
this.snapshotContentContainer = snapshotContentContainer;
5561
this.promise = promise;
5662
}
5763

@@ -112,10 +118,20 @@ else if ("data-uri".equals(result)) {
112118
private void captureView (View view, OutputStream os) {
113119
int w = view.getWidth();
114120
int h = view.getHeight();
121+
115122
if (w <= 0 || h <= 0) {
116123
throw new RuntimeException("Impossible to snapshot the view: view is invalid");
117124
}
118-
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
125+
126+
//evaluate real height
127+
if (this.snapshotContentContainer){
128+
h=0;
129+
ScrollView scrollView = (ScrollView)view;
130+
for (int i = 0; i < scrollView.getChildCount(); i++) {
131+
h += scrollView.getChildAt(i).getHeight();
132+
}
133+
}
134+
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
119135
Canvas c = new Canvas(bitmap);
120136
view.draw(c);
121137

0 commit comments

Comments
 (0)