mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-27 10:15:00 +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:
@@ -98,17 +98,13 @@ public class LinearGaugeView extends View {
|
|||||||
return getWidth() / 100.0f * percent;
|
return getWidth() / 100.0f * percent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawCenteredText(Canvas canvas, String text, float centerX, float y,
|
private void drawCenteredText(Canvas canvas, String text, float centerX, float y, Paint paint) {
|
||||||
Paint paint, Rect textBounds) {
|
final float textWidth = paint.measureText(text);
|
||||||
float x = Math.max(0.0f, centerX - textBounds.width() / 2.0f);
|
float x = Math.max(0.0f, centerX - textWidth / 2.0f);
|
||||||
x = Math.min(x, getWidth() - textBounds.width());
|
x = Math.min(x, getWidth() - textWidth);
|
||||||
canvas.drawText(text, x, y, paint);
|
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
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
@@ -204,7 +200,7 @@ public class LinearGaugeView extends View {
|
|||||||
String valueStr = String.format("%.2f", value);
|
String valueStr = String.format("%.2f", value);
|
||||||
indicatorPaint.getTextBounds(valueStr, 0, valueStr.length(), bounds);
|
indicatorPaint.getTextBounds(valueStr, 0, valueStr.length(), bounds);
|
||||||
drawCenteredText(canvas, valueStr, valuePos,
|
drawCenteredText(canvas, valueStr, valuePos,
|
||||||
indicatorBottom + bounds.height() + textOffset, indicatorPaint, bounds);
|
indicatorBottom + bounds.height() + textOffset, indicatorPaint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user