Skip to content

Commit e76f475

Browse files
committed
perf(android): avoid redundant traversal and transform allocations
1 parent 6acbec5 commit e76f475

1 file changed

Lines changed: 16 additions & 38 deletions

File tree

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

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.graphics.Bitmap;
55
import android.graphics.Canvas;
66
import android.graphics.Color;
7-
import android.graphics.Matrix;
87
import android.graphics.Paint;
98
import android.graphics.Point;
109
import android.net.Uri;
@@ -41,7 +40,6 @@
4140
import java.util.ArrayList;
4241
import java.util.Arrays;
4342
import java.util.Collections;
44-
import java.util.LinkedList;
4543
import java.util.List;
4644
import java.util.Locale;
4745
import java.util.Set;
@@ -511,24 +509,20 @@ public void run() {
511509

512510
@NonNull
513511
static List<View> getAllChildren(@NonNull final View v) {
514-
if (!(v instanceof ViewGroup)) {
515-
final ArrayList<View> viewArrayList = new ArrayList<>();
516-
viewArrayList.add(v);
517-
518-
return viewArrayList;
519-
}
520-
521512
final ArrayList<View> result = new ArrayList<>();
513+
collectChildren(v, result);
514+
return result;
515+
}
522516

523-
ViewGroup viewGroup = (ViewGroup) v;
524-
for (int i = 0; i < viewGroup.getChildCount(); i++) {
525-
View child = viewGroup.getChildAt(i);
526-
527-
//Do not add any parents, just add child elements
528-
result.addAll(getAllChildren(child));
517+
private static void collectChildren(@NonNull final View v, @NonNull final List<View> result) {
518+
if (!(v instanceof ViewGroup)) {
519+
result.add(v);
520+
} else {
521+
final ViewGroup group = (ViewGroup) v;
522+
for (int i = 0; i < group.getChildCount(); i++) {
523+
collectChildren(group.getChildAt(i), result);
524+
}
529525
}
530-
531-
return result;
532526
}
533527

534528
/**
@@ -860,7 +854,7 @@ public void onPixelCopyFinished(int copyResult) {
860854
*/
861855
@NonNull
862856
static List<View> walkAncestors(@NonNull final View child, @NonNull final View root) {
863-
final LinkedList<View> ms = new LinkedList<>();
857+
final List<View> ms = new ArrayList<>();
864858
if (child == root) {
865859
return ms;
866860
}
@@ -876,35 +870,19 @@ static List<View> walkAncestors(@NonNull final View child, @NonNull final View r
876870
/**
877871
* Concat all the transformation matrix's from parent to child.
878872
*/
879-
@NonNull
880-
@SuppressWarnings("UnusedReturnValue")
881-
private Matrix applyTransformations(final Canvas c, @NonNull final View root, @NonNull final View child) {
882-
final Matrix transform = new Matrix();
883-
final LinkedList<View> ms = new LinkedList<>(walkAncestors(child, root));
884-
if (ms.isEmpty()) {
885-
return transform;
886-
}
887-
873+
private void applyTransformations(final Canvas c, @NonNull final View root, @NonNull final View child) {
874+
final List<View> ms = walkAncestors(child, root);
888875
// apply transformations from parent --> child order
889-
Collections.reverse(ms);
890-
891-
for (final View v : ms) {
892-
c.save();
893-
876+
for (int i = ms.size() - 1; i >= 0; i--) {
877+
final View v = ms.get(i);
894878
// apply each view transformations, so each child will be affected by them
895879
final float dx = v.getLeft() + ((v != child) ? v.getPaddingLeft() : 0) + v.getTranslationX();
896880
final float dy = v.getTop() + ((v != child) ? v.getPaddingTop() : 0) + v.getTranslationY();
897881
c.translate(dx, dy);
898882
c.rotate(v.getRotation(), v.getPivotX(), v.getPivotY());
899883
c.scale(v.getScaleX(), v.getScaleY());
900884

901-
// compute the matrix just for any future use
902-
transform.postTranslate(dx, dy);
903-
transform.postRotate(v.getRotation(), v.getPivotX(), v.getPivotY());
904-
transform.postScale(v.getScaleX(), v.getScaleY());
905885
}
906-
907-
return transform;
908886
}
909887

910888
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)