Skip to content

Commit

Permalink
Refactor gauge layout
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynovikov committed Oct 24, 2023
1 parent 9998e2a commit 226036b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/mobi/maptrek/view/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
93 changes: 42 additions & 51 deletions app/src/main/java/mobi/maptrek/view/GaugePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -77,93 +79,83 @@ 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();

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(
MeasureSpec.makeMeasureSpec(childWidthSize, childWidthMode),
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) {
Expand All @@ -174,6 +166,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
);
}
}
*/

width += getPaddingLeft() + getPaddingRight();
height += getPaddingTop() + getPaddingBottom();
Expand All @@ -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();

Expand Down Expand Up @@ -231,19 +223,18 @@ 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);
if (child.getVisibility() == View.GONE)
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);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

<dimen name="image_button_padding">8dp</dimen>
<dimen name="image_button_size">48dp</dimen>
<dimen name="image_button_size_with_gap">52dp</dimen>
<dimen name="image_button_size_medium">32dp</dimen>
<dimen name="image_button_size_small">24dp</dimen>
<dimen name="image_button_size_margin">52dp</dimen>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<string name="downloadHillshades">Download concomitant hillshades</string>
<string name="iamin">Yes, I\'m in</string>
<string name="nameOrCoordinates">Name or coordinates</string>
<string name="satellitesStub" translatable="false">0/0</string>

<string name="actionSave">Save</string>
<string name="actionStop">Stop</string>
Expand Down

0 comments on commit 226036b

Please sign in to comment.