android canvas drawText从宽度设置字体大小?

Bad*_*dal 50 java android android-canvas

我想canvas使用一定宽度绘制文本.drawtext

例如,400px无论输入文本是什么,文本的宽度应始终为.

如果输入文本较长则会减小字体大小,如果输入文本较短,则会相应地增加字体大小.

Mic*_*per 104

这是一种更有效的方法:

/**
 * Sets the text size for a Paint object so a given string of text will be a
 * given width.
 * 
 * @param paint
 *            the Paint to set the text size for
 * @param desiredWidth
 *            the desired width
 * @param text
 *            the text that should be that width
 */
private static void setTextSizeForWidth(Paint paint, float desiredWidth,
        String text) {

    // Pick a reasonably large value for the test. Larger values produce
    // more accurate results, but may cause problems with hardware
    // acceleration. But there are workarounds for that, too; refer to
    // http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache
    final float testTextSize = 48f;

    // Get the bounds of the text, using our testTextSize.
    paint.setTextSize(testTextSize);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);

    // Calculate the desired size as a proportion of our testTextSize.
    float desiredTextSize = testTextSize * desiredWidth / bounds.width();

    // Set the paint for that size.
    paint.setTextSize(desiredTextSize);
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要做的就是setTextSizeForWidth(paint, 400, str);(400是问题中的示例宽度).

为了获得更高的效率,您可以创建Rect一个静态类成员,从而使其不会每次都被实例化.但是,这可能会引入并发问题,并且可能会阻碍代码清晰度.

  • 哎呀!:-)很高兴它有所帮助. (3认同)

sly*_*oty 27

试试这个:

/**
 * Retrieve the maximum text size to fit in a given width.
 * @param str (String): Text to check for size.
 * @param maxWidth (float): Maximum allowed width.
 * @return (int): The desired text size.
 */
private int determineMaxTextSize(String str, float maxWidth)
{
    int size = 0;       
    Paint paint = new Paint();

    do {
        paint.setTextSize(++ size);
    } while(paint.measureText(str) < maxWidth);

    return size;
} //End getMaxTextSize()
Run Code Online (Sandbox Code Playgroud)

  • 这有一个错误.您不处理str没有字符或只有空格的情况.你因为它而永远迭代.将其添加为第一行...`if(str == null || str.trim().length()== 0)return 0;` (4认同)
  • 哦,这个工作现在我传递了不同的字符串值并绘制了不同的字符串.我的错.并且它使用`size`而不是`size/2`请编辑你的答案. (2认同)

Far*_*sbi 5

Michael Scheper 的解决方案 看起来不错,但它对我不起作用,我需要获得可以在我的视图中绘制的最大文本大小,但这种方法取决于您设置的第一个文本大小,每次您设置不同的大小时会得到不同的结果,不能说它在每种情况下都是正确的答案。

所以我尝试了另一种方法:

private float calculateMaxTextSize(String text, Paint paint, int maxWidth, int maxHeight) {
    if (text == null || paint == null) return 0;
    Rect bound = new Rect();
    float size = 1.0f;
    float step= 1.0f;    
    while (true) {
        paint.getTextBounds(text, 0, text.length(), bound);
        if (bound.width() < maxWidth && bound.height() < maxHeight) {
            size += step;
            paint.setTextSize(size);
        } else {
            return size - step;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这很简单,我增加文本大小,直到文本矩形边界尺寸足够接近 和maxWidthmaxHeight为了减少循环重复,只需更改step为更大的值(精度与速度),也许这不是实现此目的的最佳方法,但它有效。