Skip to content

Commit bcaaa02

Browse files
authored
Merge pull request #84 from Bardolos/master
TextureView compatibility on Android as per Issue #57
2 parents f848cbf + c3c8047 commit bcaaa02

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import android.graphics.Canvas;
99
import android.net.Uri;
1010
import android.util.Base64;
11+
import android.view.TextureView;
1112
import android.view.View;
13+
import android.view.ViewGroup;
1214
import android.widget.ScrollView;
1315

1416
import com.facebook.react.bridge.Promise;
@@ -21,6 +23,8 @@
2123
import java.io.FileOutputStream;
2224
import java.io.IOException;
2325
import java.io.OutputStream;
26+
import java.util.ArrayList;
27+
import java.util.List;
2428

2529
/**
2630
* Snapshot utility class allow to screenshot a view.
@@ -116,6 +120,27 @@ else if ("data-uri".equals(result)) {
116120
}
117121
}
118122

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+
119144
/**
120145
* Screenshot a view and return the captured bitmap.
121146
* @param view the view to capture
@@ -138,9 +163,21 @@ private void captureView (View view, OutputStream os) {
138163
}
139164
}
140165
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
166+
Bitmap childBitmapBuffer;
141167
Canvas c = new Canvas(bitmap);
142168
view.draw(c);
143169

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+
144181
if (width != null && height != null && (width != w || height != h)) {
145182
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
146183
}

0 commit comments

Comments
 (0)