Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 16 additions & 38 deletions android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.net.Uri;
Expand Down Expand Up @@ -41,7 +40,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -511,24 +509,20 @@ public void run() {

@NonNull
static List<View> getAllChildren(@NonNull final View v) {
if (!(v instanceof ViewGroup)) {
final ArrayList<View> viewArrayList = new ArrayList<>();
viewArrayList.add(v);

return viewArrayList;
}

final ArrayList<View> result = new ArrayList<>();
collectChildren(v, result);
return result;
}

ViewGroup viewGroup = (ViewGroup) v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);

//Do not add any parents, just add child elements
result.addAll(getAllChildren(child));
private static void collectChildren(@NonNull final View v, @NonNull final List<View> result) {
if (!(v instanceof ViewGroup)) {
result.add(v);
} else {
final ViewGroup group = (ViewGroup) v;
for (int i = 0; i < group.getChildCount(); i++) {
collectChildren(group.getChildAt(i), result);
}
}

return result;
}

/**
Expand Down Expand Up @@ -860,7 +854,7 @@ public void onPixelCopyFinished(int copyResult) {
*/
@NonNull
static List<View> walkAncestors(@NonNull final View child, @NonNull final View root) {
final LinkedList<View> ms = new LinkedList<>();
final List<View> ms = new ArrayList<>();
if (child == root) {
return ms;
}
Expand All @@ -876,35 +870,19 @@ static List<View> walkAncestors(@NonNull final View child, @NonNull final View r
/**
* Concat all the transformation matrix's from parent to child.
*/
@NonNull
@SuppressWarnings("UnusedReturnValue")
private Matrix applyTransformations(final Canvas c, @NonNull final View root, @NonNull final View child) {
final Matrix transform = new Matrix();
final LinkedList<View> ms = new LinkedList<>(walkAncestors(child, root));
if (ms.isEmpty()) {
return transform;
}

private void applyTransformations(final Canvas c, @NonNull final View root, @NonNull final View child) {
final List<View> ms = walkAncestors(child, root);
// apply transformations from parent --> child order
Collections.reverse(ms);

for (final View v : ms) {
c.save();

for (int i = ms.size() - 1; i >= 0; i--) {
final View v = ms.get(i);
// apply each view transformations, so each child will be affected by them
final float dx = v.getLeft() + ((v != child) ? v.getPaddingLeft() : 0) + v.getTranslationX();
final float dy = v.getTop() + ((v != child) ? v.getPaddingTop() : 0) + v.getTranslationY();
c.translate(dx, dy);
c.rotate(v.getRotation(), v.getPivotX(), v.getPivotY());
c.scale(v.getScaleX(), v.getScaleY());

// compute the matrix just for any future use
transform.postTranslate(dx, dy);
transform.postRotate(v.getRotation(), v.getPivotX(), v.getPivotY());
transform.postScale(v.getScaleX(), v.getScaleY());
}

return transform;
}

@SuppressWarnings("unchecked")
Expand Down
Loading