テキスト幅の取得
【Android】SurfaceView上でCanvasに描画する文字列の幅を取得する
最初はreturn fontMetrics.descent-fontMetrics.ascent;にしてたけど、
bottom
The maximum distance below the baseline for the lowest glyph in the font at a given text size.descent
The recommended distance below the baseline for singled spaced text.
Paint.FontMetrics | Android Developers
ということなのでfontMetrics.bottom-fontMetrics.top;にした。
こんな感じでいいのかな?
public float getTextWidth(String string){
Paint paint = new Paint();
return paint.measureText(string);
}
public float getTextHeight(String message) {
Paint paint = new Paint();
FontMetrics fontMetrics = paint.getFontMetrics();
return fontMetrics.bottom-fontMetrics.top;
}