perf(android): gridlayout with less JNI calls (#10402)

This commit is contained in:
farfromrefuge
2024-07-01 18:20:14 +02:00
committed by GitHub
parent 7036f12b5c
commit 6dd441d6ba
8 changed files with 288 additions and 125 deletions

View File

@@ -1,11 +1,16 @@
package org.nativescript.widgets;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.MeasureSpec;
import android.util.AttributeSet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
@@ -13,6 +18,7 @@ import java.util.HashMap;
* @author hhristov
*/
public class GridLayout extends LayoutBase {
protected final static String TAG = "GridLayout";
private final MeasureHelper helper = new MeasureHelper(this);
@@ -31,6 +37,14 @@ public class GridLayout extends LayoutBase {
public GridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public GridLayout(Context context, String rows) {
this(context);
this.addRowsFromJSON(rows);
}
public GridLayout(Context context, String rows, String columns) {
this(context, rows);
this.addColumnsFromJSON(rows);
}
private static void validateItemSpec(ItemSpec itemSpec) {
if (itemSpec == null) {
@@ -53,6 +67,10 @@ public class GridLayout extends LayoutBase {
this.requestLayout();
}
public void addRow(int value, GridUnitType type) {
addRow(new ItemSpec(value, type));
}
public void addColumn(ItemSpec itemSpec) {
validateItemSpec(itemSpec);
itemSpec.owner = this;
@@ -64,19 +82,46 @@ public class GridLayout extends LayoutBase {
this.requestLayout();
}
public void removeColumn(ItemSpec itemSpec) {
if (itemSpec == null) {
throw new Error("itemSpec is null.");
}
int index = this._cols.indexOf(itemSpec);
if (itemSpec.owner != this || index < 0) {
throw new Error("itemSpec is not child of this GridLayout");
}
this.removeColumnAt(index);
public void addColumn(int value, GridUnitType type) {
addColumn(new ItemSpec(value, type));
}
public void addRowsFromJSON(String value) {
try {
if (value == null) {
return;
}
JSONArray rows = new JSONArray(value);
for (int i = 0; i < rows.length() ; i++) {
JSONObject row = rows.getJSONObject(i);
addRow(row.getInt("value"), GridUnitType.values()[row.getInt("type")]);
}
} catch (JSONException exception) {
Log.e(TAG, "Caught JSONException...");
exception.printStackTrace();
}
}
public void addColumnsFromJSON(String value) {
try {
if (value == null) {
return;
}
JSONArray columns = new JSONArray(value);
for (int i = 0; i < columns.length() ; i++) {
JSONObject column = columns.getJSONObject(i);
addColumn(column.getInt("value"), GridUnitType.values()[column.getInt("type")]);
}
} catch (JSONException exception) {
Log.e(TAG, "Caught JSONException...");
exception.printStackTrace();
}
}
public void addRowsAndColumnsFromJSON(String rowsString, String jsonString) {
addRowsFromJSON(rowsString);
addColumnsFromJSON(jsonString);
}
public void removeColumnAt(int index) {
this._cols.remove(index);
this.helper.columns.get(index).children.clear();
@@ -84,19 +129,6 @@ public class GridLayout extends LayoutBase {
this.requestLayout();
}
public void removeRow(ItemSpec itemSpec) {
if (itemSpec == null) {
throw new Error("itemSpec is null.");
}
int index = this._rows.indexOf(itemSpec);
if (itemSpec.owner != this || index < 0) {
throw new Error("itemSpec is not child of this GridLayout");
}
this.removeRowAt(index);
}
public void removeRowAt(int index) {
this._rows.remove(index);
this.helper.rows.get(index).children.clear();
@@ -116,6 +148,30 @@ public class GridLayout extends LayoutBase {
return copy;
}
public void reset() {
clearRows();
clearColumns();
}
public void clearRows() {
this._rows.clear();
for (int i = 0; i < this.helper.rows.size(); i++) {
this.helper.rows.get(i).children.clear();
}
this.helper.rows.clear();
this.requestLayout();
}
public void clearColumns() {
this._cols.clear();
for (int i = 0; i < this.helper.columns.size(); i++) {
this.helper.columns.get(i).children.clear();
}
this.helper.columns.clear();
this.requestLayout();
}
@Override
public void addView(View child) {
super.addView(child);
@@ -235,6 +291,16 @@ public class GridLayout extends LayoutBase {
}
}
/* only used for N tests */
public float getRowActualLength(int index) {
return this.helper.rows.get(index).rowOrColumn._actualLength;
}
/* only used for N tests */
public float getColumnActualLength(int index) {
return this.helper.columns.get(index).rowOrColumn._actualLength;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
CommonLayoutParams.adjustChildrenLayoutParams(this, widthMeasureSpec, heightMeasureSpec);
@@ -823,7 +889,6 @@ class MeasureHelper {
this.measureChild(measureSpec, false);
}
}
// try fix stars!
boolean fixColumns = canFix(this.columns);
boolean fixRows = canFix(this.rows);