Przeglądaj źródła

Add support for TextureViews as per issue #57

As per owner's suggestion:
Firstly get all children recursively;
Then check if each child is an instance of TextureView with instanceof;
Make sure child knows it's not opaque and get its bitmap into a buffer;
Draw said bitmap into the current canvas.

This assumes that these TextureView elements are ordered in the tree.
Bardolos 7 lat temu
rodzic
commit
9a56ad99da

+ 42
- 0
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Wyświetl plik

@@ -6,9 +6,16 @@ import android.app.Activity;
6 6
 import android.content.Intent;
7 7
 import android.graphics.Bitmap;
8 8
 import android.graphics.Canvas;
9
+import android.graphics.Color;
10
+import android.graphics.Matrix;
11
+import android.graphics.Rect;
9 12
 import android.net.Uri;
10 13
 import android.util.Base64;
14
+import android.util.Log;
15
+import android.view.TextureView;
11 16
 import android.view.View;
17
+import android.view.ViewGroup;
18
+import android.widget.LinearLayout;
12 19
 import android.widget.ScrollView;
13 20
 
14 21
 import com.facebook.react.bridge.Promise;
@@ -21,6 +28,8 @@ import java.io.File;
21 28
 import java.io.FileOutputStream;
22 29
 import java.io.IOException;
23 30
 import java.io.OutputStream;
31
+import java.util.ArrayList;
32
+import java.util.List;
24 33
 
25 34
 /**
26 35
  * Snapshot utility class allow to screenshot a view.
@@ -116,6 +125,27 @@ public class ViewShot implements UIBlock {
116 125
         }
117 126
     }
118 127
 
128
+    private List<View> getAllChildren(View v) {
129
+
130
+        if (!(v instanceof ViewGroup)) {
131
+            ArrayList<View> viewArrayList = new ArrayList<View>();
132
+            viewArrayList.add(v);
133
+            return viewArrayList;
134
+        }
135
+
136
+        ArrayList<View> result = new ArrayList<View>();
137
+
138
+        ViewGroup viewGroup = (ViewGroup) v;
139
+        for (int i = 0; i < viewGroup.getChildCount(); i++) {
140
+
141
+            View child = viewGroup.getChildAt(i);
142
+
143
+            //Do not add any parents, just add child elements
144
+            result.addAll(getAllChildren(child));
145
+        }
146
+        return result;
147
+    }
148
+
119 149
     /**
120 150
      * Screenshot a view and return the captured bitmap.
121 151
      * @param view the view to capture
@@ -138,9 +168,21 @@ public class ViewShot implements UIBlock {
138 168
             }
139 169
         }
140 170
         Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
171
+        Bitmap childBitmapBuffer;
141 172
         Canvas c = new Canvas(bitmap);
142 173
         view.draw(c);
143 174
 
175
+        //after view is drawn, go through children
176
+        List<View> childrenList = getAllChildren(view);
177
+
178
+        for (View child : childrenList) {
179
+            if(child instanceof TextureView) {
180
+                ((TextureView) child).setOpaque(false);
181
+                childBitmapBuffer = ((TextureView) child).getBitmap(child.getWidth(), child.getHeight());
182
+                c.drawBitmap(childBitmapBuffer, child.getLeft() + ((ViewGroup)child.getParent()).getLeft() +  child.getPaddingLeft(), child.getTop() + ((ViewGroup)child.getParent()).getTop() + child.getPaddingTop(), null);
183
+            }
184
+        }
185
+
144 186
         if (width != null && height != null && (width != w || height != h)) {
145 187
             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
146 188
         }