绘制文本在一些指定的矩形配件 [英] Draw text fitting in some specified Rectangle

查看:129
本文介绍了绘制文本在一些指定的矩形配件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的画布,我想提请其安装到一些指定的矩形一些简短的标签文本(1-2个字符)。
对于其它一些原因,我使用的缩放是使得该recangle的尺寸是小的,即约1

Using a canvas, I want to draw some short label text (1-2 characters) which fits into some specified rectangle. For some other reasons, the scaling I use is such that the dimensions of this recangle are small, i.e. about 1.

我现在面临的问题是计算出最优(尽可能的大,以便文本仍然适用)文本大小与 Paint.setTextSize 使用绘图之前文本(这一点我使用 Canva.drawText())。
为此,我既可以使用 Paint.Fontmetrics 对象,以获取一些普通的字体尺寸浮筒或 getTextBounds(字符串文字,诠释开始,诠释年底,矩形边界)来获取文本作为一个整数矩形的边框。由于我用的比例,从后者的整数边框是即时通讯precise计算出最佳的文字大小为我的目的。

The problem I'm facing is to calculate the optimal (as large as possible so that the text still fits) text size to use with Paint.setTextSize prior to drawing the text (which I do using Canva.drawText()). For that I can either use the Paint.Fontmetrics object to get some general font dimensions as floats or getTextBounds(String text, int start, int end, Rect bounds) to get the bounding box of the text as an integer rectangle. Due to the scaling I use, the integer bounding box from the latter is to imprecise to calculate the optimal text size for my purpose.

我需要一些方法来获取文本的边框具有更高的precision(即如 getStringBounds(字符串str,图形上下文) java.awt.FontMetrics中的),但我发现没有合适的方法。

What I would need is some method to get the bounding box of the text with higher precision (i.e. like getStringBounds(String str, Graphics context) in java.awt.FontMetrics), but I found no suitable method.

推荐答案

我现在也忙着这个问题。

I now was also busy with this problem.

不幸的是我没能安装本机还源,但我pretty确保文本度(包括getTextBounds()和measureText())从真实的文本输出heftily不同,尤其是对于小字体尺寸(如莫里茨截图待观察)。
我想,测量方法使用一个浮动宽度为一个字符,而真正的文本输出使用一个int(presumably由于性能)。这当然会失败,如果你需要一个绝对准确的大小(例如用于自动调整)。
我尝试了一下用等宽字体。此图片显示了一些这些实验的:

Unfortunately I didn't manage to install the native source yet, but I'm pretty sure that text measurements (both getTextBounds() and measureText()) differ heftily from the real text output, especially for small font sizes (as to be seen in Moritz' screenshot). I suppose that the measurement methods use a float width for a char, and the real text output uses an int (presumably due to performance). This will of course fail, if you need an absolutely exact size (e.g for autosize). I experimented a bit with a monospace font. This Image shows some of these experiments:

.

在这些实验的文字比例被自动设置进行缩放。

The text scale in these experiments was set by auto scaling it.

在最上面一行,我创建盒用一个整数增量。这符合了Android文本输出。你看,最后的密码没有在屏幕配合!

In the top row, I created boxes with an integer increment. These match the android text output. You see, the last cipher doesn't match the screen!

在第2行中,盒子是一个浮增量创建。这些箱子屏幕搭配更好,但他们不匹配了android文本输出!

In the 2nd row, the boxes were created with a float increment. The boxes match the screen better, but they don't match the android text output!

在最后一排,箱子被一个浮动的增量递增,文本由同一增量是由炭炭产量。这两个箱子和字符完美契合

In the last row, the boxes were incremented by a float increment and the text was output char by char by the same increment. Both boxes and chars fit perfect.

在我的结论,这是目前无法在Android文本输出设置为一个精确的宽度。这个问题似乎并没有被测量方法,这是不够准确的。这个问题似乎是,你不能用setTextSize()设置一个确切的文本的宽度。

In my conclusion, it is currently not possible to set an Android text output to an exact width. The problem seems not to be the measurement methods, which are exact enough. The problem seems to be, that you can not set an exact text width by setTextSize().

请尝试以下方法重现这样的:

Try the following to reproduce this:

    float width = 100; // define a width which should be achieved
    m_textPaint.setTextSize( 100 ); // set a text size surely big enough
    Rect r = new Rect();
    String s = new String("This is a test text");
    m_textPaint.getTextBounds( s, 0, s.length(), r ); // measure the text with a random size
    float fac = width / r.width(); // compute the factor, which will scale the text to our target width
    Log.i( "MyTestOutput", "current text width:" + r.width() );
    Log.i( "MyTestOutput", "wanted text width:" + width );
    Log.i( "MyTestOutput", "factor:" + fac );
    Log.i( "MyTestOutput", "expected result:" + (float) r.width() * fac );
    m_textPaint.setTextSize( m_textPaint.getTextSize() * fac );
    m_textPaint.getTextBounds( s, 0, s.length(), r ); // now final measurement: whats the real width?
    Log.i( "MyTestOutput", "real result:" + r.width() );

我得到的输出:

12月5日至26日:18:18.420:I / MyTestOutput(23607):当前文本的宽度1125

05-26 12:18:18.420: I/MyTestOutput(23607): current text width:1125

12月5日至26日:18:18.425:I / MyTestOutput(23607):希望文本宽度:100.0

05-26 12:18:18.425: I/MyTestOutput(23607): wanted text width:100.0

12月5日至26日:18:18.425:I / MyTestOutput(23607):因素:0.08888889

05-26 12:18:18.425: I/MyTestOutput(23607): factor:0.08888889

12月5日至26日:18:18.425:I / MyTestOutput(23607):预期的结果:100.0

05-26 12:18:18.425: I/MyTestOutput(23607): expected result:100.0

12月5日至26日:18:18.430:I / MyTestOutput(23607):真正的结果是:94

05-26 12:18:18.430: I/MyTestOutput(23607): real result:94

所以,在我看来,只有这样才能实现准确很文本自动定标的,是
一)通过测量和设置文字大小自动缩放它
二)将其输出字符成炭由自计算增量。

So in my opinion, the only way to achieve very exactly autoscaled text, is to a) autoscale it by measuring and setting text size b) output it char by char by a self computed increment.

不幸的是,这仅适用于等宽字体。对于其他字体,这将是更复杂,但也有可能。

Unfortunately, this works only for monospaced fonts. For other fonts, it will be more complicated, but also possible.

这篇关于绘制文本在一些指定的矩形配件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆