Просмотр исходного кода

Merge pull request #35 from manweill/master

Fix ScrollView Snapshots
Gaëtan Renaudeau 7 лет назад
Родитель
Сommit
bb92625cdd

+ 1
- 0
README.md Просмотреть файл

@@ -42,6 +42,7 @@ Returns a Promise of the image URI.
42 42
     - `"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*.
43 43
     - `"data-uri"`: same as `base64` but also includes the [Data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) header.
44 44
  - **`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)
45 46
 
46 47
 ## Caveats
47 48
 

+ 2
- 1
android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java Просмотреть файл

@@ -64,11 +64,12 @@ public class RNViewShotModule extends ReactContextBaseJavaModule {
64 64
         Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
65 65
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
66 66
         String result = options.hasKey("result") ? options.getString("result") : "file";
67
+        Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer") : false;
67 68
         try {
68 69
             String name = options.hasKey("filename") ? options.getString("filename") : null;
69 70
             File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
70 71
             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));
72 73
         }
73 74
         catch (Exception e) {
74 75
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);

+ 17
- 1
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Просмотреть файл

@@ -1,11 +1,14 @@
1 1
 package fr.greweb.reactnativeviewshot;
2 2
 
3 3
 import javax.annotation.Nullable;
4
+
5
+import android.app.Activity;
4 6
 import android.graphics.Bitmap;
5 7
 import android.graphics.Canvas;
6 8
 import android.net.Uri;
7 9
 import android.util.Base64;
8 10
 import android.view.View;
11
+import android.widget.ScrollView;
9 12
 
10 13
 import com.facebook.react.bridge.Promise;
11 14
 import com.facebook.react.uimanager.NativeViewHierarchyManager;
@@ -33,6 +36,7 @@ public class ViewShot implements UIBlock {
33 36
     private File output;
34 37
     private String result;
35 38
     private Promise promise;
39
+    private Boolean snapshotContentContainer;
36 40
 
37 41
     public ViewShot(
38 42
             int tag,
@@ -43,6 +47,7 @@ public class ViewShot implements UIBlock {
43 47
             @Nullable Integer height,
44 48
             File output,
45 49
             String result,
50
+            Boolean snapshotContentContainer,
46 51
             Promise promise) {
47 52
         this.tag = tag;
48 53
         this.extension = extension;
@@ -52,6 +57,7 @@ public class ViewShot implements UIBlock {
52 57
         this.height = height;
53 58
         this.output = output;
54 59
         this.result = result;
60
+        this.snapshotContentContainer = snapshotContentContainer;
55 61
         this.promise = promise;
56 62
     }
57 63
 
@@ -112,10 +118,20 @@ public class ViewShot implements UIBlock {
112 118
     private void captureView (View view, OutputStream os) {
113 119
         int w = view.getWidth();
114 120
         int h = view.getHeight();
121
+
115 122
         if (w <= 0 || h <= 0) {
116 123
             throw new RuntimeException("Impossible to snapshot the view: view is invalid");
117 124
         }
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);
119 135
         Canvas c = new Canvas(bitmap);
120 136
         view.draw(c);
121 137