From 226036b0e18ff365aeb7962eb2a7fdfc79cc8b50 Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Tue, 24 Oct 2023 21:24:48 +0300 Subject: [PATCH] Refactor gauge layout --- .../main/java/mobi/maptrek/view/Gauge.java | 1 + .../java/mobi/maptrek/view/GaugePanel.java | 93 +++++++++---------- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/values/dimens.xml | 1 - app/src/main/res/values/strings.xml | 1 + 5 files changed, 45 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/mobi/maptrek/view/Gauge.java b/app/src/main/java/mobi/maptrek/view/Gauge.java index b2702dd6..e436e8c8 100644 --- a/app/src/main/java/mobi/maptrek/view/Gauge.java +++ b/app/src/main/java/mobi/maptrek/view/Gauge.java @@ -38,6 +38,7 @@ public class Gauge extends ConstraintLayout { public static final int TYPE_VMG = 0x80000; public static final int TYPE_XTK = 0x100000; public static final int TYPE_ETE = 0x200000; + // If new gauge type is added sizes array should be adjusted in GaugePanel private int mType; private TextView mValueView; diff --git a/app/src/main/java/mobi/maptrek/view/GaugePanel.java b/app/src/main/java/mobi/maptrek/view/GaugePanel.java index 230bd385..29b5cf3e 100644 --- a/app/src/main/java/mobi/maptrek/view/GaugePanel.java +++ b/app/src/main/java/mobi/maptrek/view/GaugePanel.java @@ -62,6 +62,8 @@ public class GaugePanel extends ViewGroup implements View.OnLongClickListener, P private boolean mVisible; private boolean mHasSensors; + private final int[][] childSizes = new int[][] {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; + public GaugePanel(Context context) { super(context); } @@ -77,35 +79,25 @@ public GaugePanel(Context context, AttributeSet attrs, int defStyleAttr) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - logger.error("onMeasure"); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); - logger.error("{} {}", sizeWidth, sizeHeight); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int modeHeight = MeasureSpec.getMode(heightMeasureSpec); int width = 0; int height = 0; - int maxWidth = 0; - - int lineWidth = 0; - int lineHeight = 0; int childCount = getChildCount(); + int visibleCount = 0; + // First pass - measure children for (int i = 0; i < childCount; i++) { View child = getChildAt(i); - boolean lastChild = i == childCount - 1; - if (child.getVisibility() == View.GONE) { - if (lastChild) { - width += lineWidth; - height = Math.max(height, lineHeight); - } + if (child.getVisibility() == View.GONE) continue; - } measureChild(child, widthMeasureSpec, heightMeasureSpec); LayoutParams lp = child.getLayoutParams(); @@ -113,27 +105,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int childWidthMode = MeasureSpec.AT_MOST; int childWidthSize = sizeWidth; - int childHeightMode = MeasureSpec.AT_MOST; - int childHeightSize = sizeHeight; + int childHeightMode = MeasureSpec.UNSPECIFIED; + int childHeightSize = 0; if (lp.width == LayoutParams.MATCH_PARENT) { childWidthMode = MeasureSpec.EXACTLY; } else if (lp.width >= 0) { childWidthMode = MeasureSpec.EXACTLY; childWidthSize = lp.width; - } else if (modeWidth == MeasureSpec.UNSPECIFIED) { - childWidthMode = MeasureSpec.UNSPECIFIED; - childWidthSize = 0; - } - - if (lp.height == LayoutParams.MATCH_PARENT) { - childWidthMode = MeasureSpec.EXACTLY; - } else if (lp.height >= 0) { - childHeightMode = MeasureSpec.EXACTLY; - childHeightSize = lp.height; - } else if (modeHeight == MeasureSpec.UNSPECIFIED) { - childHeightMode = MeasureSpec.UNSPECIFIED; - childHeightSize = 0; } child.measure( @@ -141,29 +120,42 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { MeasureSpec.makeMeasureSpec(childHeightSize, childHeightMode) ); - int childWidth = child.getMeasuredWidth(); - if (maxWidth < childWidth) - maxWidth = childWidth; - - int childHeight = child.getMeasuredHeight(); + childSizes[i][0] = child.getMeasuredWidth(); + childSizes[i][1] = child.getMeasuredHeight(); - if (lineHeight + childHeight > sizeHeight) { - height = Math.max(height, lineHeight); - lineHeight = childHeight; - width += lineWidth; - lineWidth = child.getMeasuredWidth(); - } else { - lineHeight += childHeight; - lineWidth = Math.max(lineWidth, child.getMeasuredWidth()); - } + width = Math.max(width, childSizes[i][0]); + height += childSizes[i][1]; + visibleCount++; + } - if (lastChild) { - height = Math.max(height, lineHeight); - width += lineWidth; + if (height > sizeHeight) { + int lines = (height + sizeHeight - 1) / sizeHeight; + int childrenInLine = (visibleCount + lines - 1) / lines; // currently we assume that all children are the same height, this can change in future + height = 0; + width = 0; + int lineWidth = 0; + int lineHeight = 0; + int j = 0; + for (int i = 0; i < childCount; i++) { + View child = getChildAt(i); + if (child.getVisibility() == GONE) + continue; + j++; + if (j > childrenInLine) { + width += lineWidth; + lineWidth = childSizes[i][0]; + height = Math.max(height, lineHeight); + lineHeight = childSizes[i][1]; + j = 0; + } else { + lineWidth = Math.max(lineWidth, childSizes[i][0]); + lineHeight += childSizes[i][1]; + } } + width += lineWidth; } - // set all children width to most wide one + /* for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { @@ -174,6 +166,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ); } } + */ width += getPaddingLeft() + getPaddingRight(); height += getPaddingTop() + getPaddingBottom(); @@ -186,7 +179,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { - logger.error("onLayout"); mLines.clear(); mLineWidths.clear(); @@ -231,8 +223,8 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { int left = getPaddingLeft() + horizontalGravityMargin; int line = 0; - int nextLine = mLines.size() > 0 ? mLines.get(0) : childCount; lineWidth = mLineWidths.get(0); + int nextLine = mLines.size() > 0 ? mLines.get(0) : childCount; for (int i = 0; i < childCount; i++) { View child = getChildAt(i); @@ -240,10 +232,9 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { continue; if (i == nextLine) { - line++; - lineWidth = mLineWidths.get(line); top = getPaddingTop(); - left += lineWidth; + left += mLineWidths.get(line++); // get width of previous line and increase line count + lineWidth = mLineWidths.get(line); if (line < mLines.size()) nextLine = mLines.get(line); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2e8c5a25..0f1dd55a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -62,7 +62,6 @@ android:id="@+id/gaugePanel" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="@dimen/image_button_size_with_gap" android:background="@color/panelBackground" android:visibility="gone" app:layout_constraintStart_toStartOf="parent" @@ -145,6 +144,7 @@ android:textAppearance="?android:attr/textAppearanceLarge" android:textStyle="bold" android:translationY="-200dp" + android:text="@string/satellitesStub" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 10ddb1fc..59ac6f9a 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -9,7 +9,6 @@ 8dp 48dp - 52dp 32dp 24dp 52dp diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 689036da..444747d6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -40,6 +40,7 @@ Download concomitant hillshades Yes, I\'m in Name or coordinates + 0/0 Save Stop