测量要在 Canvas 上绘制的文本宽度(Android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3257293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:19:04  来源:igfitidea点击:

Measuring text width to be drawn on Canvas ( Android )

androidtextandroid-canvasdraw

提问by NioX5199

Is there a method which returns the width ( in pixels ) of a text to be drawn on an Android canvas using the drawText() method according to the Paint used to draw it?

是否有一种方法可以根据用于绘制它的 Paint 使用 drawText() 方法返回要在 Android 画布上绘制的文本的宽度(以像素为单位)?

回答by hamid reza zavareh

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();

回答by Suragch

Supplemental answer

补充答案

There is a slight difference between the width returned by Paint.measureTextand Paint.getTextBounds. measureTextreturns a width that includes the glyph's advanceX value padding the beginning and end of the string. The Rectwidth returned by getTextBoundsdoes not have this padding because the bounds is the Rectthat tightly wraps the text.

Paint.measureText和返回的宽度略有不同 Paint.getTextBoundsmeasureText返回一个宽度,其中包括填充字符串开头和结尾的字形的 AdvanceX 值。Rect返回的宽度getTextBounds没有这个填充,因为边界是Rect紧紧包裹文本的。

source

来源

回答by Jim Baca

There's actually three different ways of measuring text.

实际上有三种不同的测量文本的方法。

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:

和 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.

请注意,如果您尝试确定何时将文本换行到下一行,则 getTextWidths 可能很有用。

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.

measureTextWidth 和 getTextWidth 是相等的,并且具有测量其他人发布的高级宽度。有些人认为这个空间过大。但是,这是非常主观的并且取决于字体。

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

图片取自Android Developers Guide to Custom Canvas Drawing

回答by Hiren Patel

Well I have done in different way:

好吧,我以不同的方式完成了:

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());

Hope this will help you.

希望这会帮助你。

回答by Armin J.

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.

我使用了 measureText() 和 getTextPath()+computeBounds() 方法并构建了一个 Excel,其中包含固定大小字体的所有文本属性,可以在https://github.com/ArminJo/android-blue-display/blob下找到/master/TextWidth.xlsx。在那里,您还可以找到其他文本属性(如上升等)的简单公式。

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

应用程序以及用于生成 excel 中使用的原始值的函数drawFontTest()也可在此 repo 中使用。

回答by Ashish John

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

您可以使用“textPaint.getTextSize()”来获取文本宽度