Преглед на файлове

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 години
родител
ревизия
9a56ad99da
променени са 1 файла, в които са добавени 42 реда и са изтрити 0 реда
  1. 42
    0
      android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

+ 42
- 0
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java Целия файл

6
 import android.content.Intent;
6
 import android.content.Intent;
7
 import android.graphics.Bitmap;
7
 import android.graphics.Bitmap;
8
 import android.graphics.Canvas;
8
 import android.graphics.Canvas;
9
+import android.graphics.Color;
10
+import android.graphics.Matrix;
11
+import android.graphics.Rect;
9
 import android.net.Uri;
12
 import android.net.Uri;
10
 import android.util.Base64;
13
 import android.util.Base64;
14
+import android.util.Log;
15
+import android.view.TextureView;
11
 import android.view.View;
16
 import android.view.View;
17
+import android.view.ViewGroup;
18
+import android.widget.LinearLayout;
12
 import android.widget.ScrollView;
19
 import android.widget.ScrollView;
13
 
20
 
14
 import com.facebook.react.bridge.Promise;
21
 import com.facebook.react.bridge.Promise;
21
 import java.io.FileOutputStream;
28
 import java.io.FileOutputStream;
22
 import java.io.IOException;
29
 import java.io.IOException;
23
 import java.io.OutputStream;
30
 import java.io.OutputStream;
31
+import java.util.ArrayList;
32
+import java.util.List;
24
 
33
 
25
 /**
34
 /**
26
  * Snapshot utility class allow to screenshot a view.
35
  * Snapshot utility class allow to screenshot a view.
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
      * Screenshot a view and return the captured bitmap.
150
      * Screenshot a view and return the captured bitmap.
121
      * @param view the view to capture
151
      * @param view the view to capture
138
             }
168
             }
139
         }
169
         }
140
         Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
170
         Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
171
+        Bitmap childBitmapBuffer;
141
         Canvas c = new Canvas(bitmap);
172
         Canvas c = new Canvas(bitmap);
142
         view.draw(c);
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
         if (width != null && height != null && (width != w || height != h)) {
186
         if (width != null && height != null && (width != w || height != h)) {
145
             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
187
             bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
146
         }
188
         }