1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-26 17:54:50 +02:00

Use measureText to get text width

getTextBounds seems to return incorrect result, possibly due to
rounding errors, and makes a small part of the text to the right
disappear.
This commit is contained in:
Erik Johansson
2018-01-20 20:03:39 +01:00
parent 09b3069268
commit 3bceae364c

View File

@@ -98,17 +98,13 @@ public class LinearGaugeView extends View {
return getWidth() / 100.0f * percent;
}
private void drawCenteredText(Canvas canvas, String text, float centerX, float y,
Paint paint, Rect textBounds) {
float x = Math.max(0.0f, centerX - textBounds.width() / 2.0f);
x = Math.min(x, getWidth() - textBounds.width());
private void drawCenteredText(Canvas canvas, String text, float centerX, float y, Paint paint) {
final float textWidth = paint.measureText(text);
float x = Math.max(0.0f, centerX - textWidth / 2.0f);
x = Math.min(x, getWidth() - textWidth);
canvas.drawText(text, x, y, paint);
}
private void drawCenteredText(Canvas canvas, String text, float centerX, float y, Paint paint) {
paint.getTextBounds(text, 0, text.length(), bounds);
drawCenteredText(canvas, text, centerX, y, paint, bounds);
}
@Override
protected void onDraw(Canvas canvas) {
@@ -204,7 +200,7 @@ public class LinearGaugeView extends View {
String valueStr = String.format("%.2f", value);
indicatorPaint.getTextBounds(valueStr, 0, valueStr.length(), bounds);
drawCenteredText(canvas, valueStr, valuePos,
indicatorBottom + bounds.height() + textOffset, indicatorPaint, bounds);
indicatorBottom + bounds.height() + textOffset, indicatorPaint);
}
@Override