Programming

Canvas에 그릴 텍스트 너비 측정 (Android)

procodes 2020. 7. 12. 12:04
반응형

Canvas에 그릴 텍스트 너비 측정 (Android)


안드로이드 캔버스에 그릴 텍스트의 너비 (픽셀)를 그리는 데 사용되는 Paint에 따라 drawText () 메서드를 사용하여 반환하는 메서드가 있습니까?


android.graphics.Paint.measureText (String txt)를 보셨습니까 ?


Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);// your preference here
paint.setTextSize(25);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_height =  bounds.height();
text_width =  bounds.width();

보충 답변

Paint.measureText와에서 반환되는 너비에는 약간의 차이가 Paint.getTextBounds있습니다. measureText문자열의 시작과 끝을 채우는 글리프의 advanceX 값을 포함하는 너비를 반환합니다. Rect에 의해 반환 폭은 getTextBounds경계가 있기 때문에이 패딩이없는 Rect단단히 텍스트를 감싸고있다.

출처


글쎄, 나는 다른 방식으로 해왔다.

String finalVal ="Hiren Patel";

Paint paint = new Paint();
paint.setTextSize(40);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");
paint.setTypeface(typeface);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);

Rect result = new Rect();
paint.getTextBounds(finalVal, 0, finalVal.length(), result);

Log.i("Text dimensions", "Width: "+result.width()+"-Height: "+result.height());

이것이 도움이되기를 바랍니다.


실제로 텍스트를 측정하는 방법에는 세 가지가 있습니다.

GetTextBounds :

val paint = Paint()
paint.typeface = ResourcesCompat.getFont(context, R.font.kaushanscript)
paint.textSize = 500f
paint.color = Color.argb(255, 3, 221, 252)
val contents = "g"
val rect = Rect()
paint.getTextBounds(contents, 0, 1, rect)
val width = rect.width()

MeasureTextWidth :

val paint = Paint()
paint.typeface = ResourcesCompat.getFont(context, R.font.kaushanscript)
paint.textSize = 500f
paint.color = Color.argb(255, 3, 221, 252)
val contents = "g"
val width = paint.measureText(contents, 0, 1)

And getTextWidths:

val paint = Paint()
paint.typeface = ResourcesCompat.getFont(context, R.font.kaushanscript)
paint.textSize = 500f
paint.color = Color.argb(255, 3, 221, 252)
val contents = "g"
val rect = Rect()
val arry = FloatArray(contents.length)
paint.getTextBounds(contents, 0, contents.length, rect)
paint.getTextWidths(contents, 0, contents.length, arry)
val width = ary.sum()

Note that getTextWidths could be useful if you are trying to determine when to wrap text to the next line.

The measureTextWidth and getTextWidth are equal and have the advanced width that measure that others have posted about. Some consider this space excessive. However, this is very subjective and dependent on the font.

For example the width from measure text bounds may actually look too small:

measure text bounds looks small

However when adding an additional text the bounds for one letter looks normal: measure text bounds looks normal for strings

Images taken from Android Developers Guide to Custom Canvas Drawing


I used the methods measureText() and getTextPath()+computeBounds() and build up an Excel with all text attributes for fixed size font that can be found under https://github.com/ArminJo/android-blue-display/blob/master/TextWidth.xlsx . There you will find also simple formulas for other text attributes like ascend etc.

The app as well as the function drawFontTest() for generating the raw values used in the excel are also available in this repo.


you can use "textPaint.getTextSize()" to get text width

참고URL : https://stackoverflow.com/questions/3257293/measuring-text-width-to-be-drawn-on-canvas-android

반응형