Browse Source

Merge pull request #35 from manweill/master

Fix ScrollView Snapshots
Gaëtan Renaudeau 8 years ago
parent
commit
bb92625cdd

+ 1
- 0
README.md View File

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*.
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
     - `"data-uri"`: same as `base64` but also includes the [Data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) header.
43
     - `"data-uri"`: same as `base64` but also includes the [Data URI scheme](https://en.wikipedia.org/wiki/Data_URI_scheme) header.
44
  - **`filename`** *(string)*: the name of the generated file if any (Android only). Defaults to `ReactNative_snapshot_image_${timestamp}`.
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
 ## Caveats
47
 ## Caveats
47
 
48
 

+ 2
- 1
android/src/main/java/fr/greweb/reactnativeviewshot/RNViewShotModule.java View File

64
         Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
64
         Integer width = options.hasKey("width") ? (int)(displayMetrics.density * options.getDouble("width")) : null;
65
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
65
         Integer height = options.hasKey("height") ? (int)(displayMetrics.density * options.getDouble("height")) : null;
66
         String result = options.hasKey("result") ? options.getString("result") : "file";
66
         String result = options.hasKey("result") ? options.getString("result") : "file";
67
+        Boolean snapshotContentContainer = options.hasKey("snapshotContentContainer") ? options.getBoolean("snapshotContentContainer") : false;
67
         try {
68
         try {
68
             String name = options.hasKey("filename") ? options.getString("filename") : null;
69
             String name = options.hasKey("filename") ? options.getString("filename") : null;
69
             File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
70
             File tmpFile = "file".equals(result) ? createTempFile(getReactApplicationContext(), format, name) : null;
70
             UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
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
         catch (Exception e) {
74
         catch (Exception e) {
74
             promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag "+tag);
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 View File

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