Skip to content

Commit 9a56ad9

Browse files
committed
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.
1 parent f848cbf commit 9a56ad9

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
import android.content.Intent;
77
import android.graphics.Bitmap;
88
import android.graphics.Canvas;
9+
import android.graphics.Color;
10+
import android.graphics.Matrix;
11+
import android.graphics.Rect;
912
import android.net.Uri;
1013
import android.util.Base64;
14+
import android.util.Log;
15+
import android.view.TextureView;
1116
import android.view.View;
17+
import android.view.ViewGroup;
18+
import android.widget.LinearLayout;
1219
import android.widget.ScrollView;
1320

1421
import com.facebook.react.bridge.Promise;
@@ -21,6 +28,8 @@
2128
import java.io.FileOutputStream;
2229
import java.io.IOException;
2330
import java.io.OutputStream;
31+
import java.util.ArrayList;
32+
import java.util.List;
2433

2534
/**
2635
* Snapshot utility class allow to screenshot a view.
@@ -116,6 +125,27 @@ else if ("data-uri".equals(result)) {
116125
}
117126
}
118127

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+
119149
/**
120150
* Screenshot a view and return the captured bitmap.
121151
* @param view the view to capture
@@ -138,9 +168,21 @@ private void captureView (View view, OutputStream os) {
138168
}
139169
}
140170
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
171+
Bitmap childBitmapBuffer;
141172
Canvas c = new Canvas(bitmap);
142173
view.draw(c);
143174

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

0 commit comments

Comments
 (0)