1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-23 16:53:04 +02:00

Use dynamic min and max values for gauge

Calculate min and max to put the normal zone in the middle while
keeping the current value visible. Makes it easier to see where one is
compared to the normal values.
This commit is contained in:
Erik Johansson
2017-11-22 22:38:16 +01:00
parent ff9c92e9bc
commit b344772dce
2 changed files with 30 additions and 14 deletions

View File

@@ -51,8 +51,6 @@ public class LinearGaugeView extends View {
private Paint infoTextPaint;
private float value;
private float minValue;
private float maxValue;
private float firstLimit = -1.0f;
private float secondLimit = -1.0f;
@@ -95,7 +93,7 @@ public class LinearGaugeView extends View {
infoTextPaint.setTextAlign(Paint.Align.CENTER);
}
private float valueToPosition(float value) {
private float valueToPosition(float value, float minValue, float maxValue) {
final float percent = (value - minValue) / (maxValue - minValue) * 100.0f;
return getWidth() / 100.0f * percent;
}
@@ -123,9 +121,35 @@ public class LinearGaugeView extends View {
return;
}
final float firstPos = valueToPosition(firstLimit);
final float secondPos = valueToPosition(secondLimit);
final float valuePos = valueToPosition(value);
final boolean hasFirstLimit = firstLimit >= 0;
// Calculate the size of the "normal" span with a fallback if there is no such span
float span = hasFirstLimit ? secondLimit - firstLimit : 0.3f * secondLimit;
// Adjust the span if needed to make the value fit inside of it
if (hasFirstLimit && value < firstLimit - span) {
span = firstLimit - value;
} else if (!hasFirstLimit && value < secondLimit - span) {
span = secondLimit - value;
} else if (value > secondLimit + span) {
span = value - secondLimit;
}
// Round span to some nice value
if (span <= 1.0f) {
span = (float)Math.ceil(span * 10.0) / 10.0f;
} else if (span <= 10.0f) {
span = (float)Math.ceil(span);
} else {
span = 5.0f * (float)Math.ceil(span / 5.0);
}
final float minValue = Math.max(0.0f, (hasFirstLimit ? firstLimit : secondLimit) - span);
final float maxValue = secondLimit + span;
final float firstPos = valueToPosition(firstLimit, minValue, maxValue);
final float secondPos = valueToPosition(secondLimit, minValue, maxValue);
final float valuePos = valueToPosition(value, minValue, maxValue);
// Bar
final float barTop = getHeight() / 2.0f - barHeight / 2.0f;
@@ -221,13 +245,6 @@ public class LinearGaugeView extends View {
setMeasuredDimension(width, height);
}
public void setMinMaxValue(float min, float max) {
minValue = min;
maxValue = max;
invalidate();
requestLayout();
}
public void setLimits(float first, float second) {
firstLimit = first;
secondLimit = second;

View File

@@ -382,7 +382,6 @@ public abstract class MeasurementView extends TableLayout {
evalResult = new EvaluationResult();
}
evaluatorView.setMinMaxValue(getMinValue(), getMaxValue());
evaluatorView.setLimits(evalResult.lowLimit, evalResult.highLimit);
evaluatorView.setValue(value);