Browse Source

Merge pull request #84 from Bardolos/master

TextureView compatibility on Android as per Issue #57
Gaëtan Renaudeau 7 years ago
parent
commit
bcaaa02ffb
1 changed files with 37 additions and 0 deletions
  1. 37
    0
      android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

+ 37
- 0
android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java View File

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