测量在缩放的帆布的文本 [英] measuring text on scaled canvas

查看:140
本文介绍了测量在缩放的帆布的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力与文本测量和缩放画布。

I've been struggling with text measuring and scaled canvases.

当画布未缩放时,getTextBounds和measureText提供准确的结果。但是,当缩放画布时,两种方法都不会提供与打印文本的实际大小相匹配的结果。

When the canvas is unscaled, getTextBounds and measureText deliver accurate results. However, when the canvas is scaled both methods do not deliver results that match the actual size of a printed text.

对于测试,我创建了一个View的子类,以下onDraw方法:

For testing I've created a subclass of View with the following onDraw method:

final float scaling = 0.51f;
final int fontSize = 50;

canvas.scale(scaling, scaling);
font = Typeface.create("Arial", Typeface.NORMAL);

Paint paint = new Paint();
paint.setColor(0xff4444ff);
paint.setTypeface(font);
paint.setTextSize(fontSize);
paint.setAntiAlias(true);

int x = 10;
int y = 100;
final String text = "Lorem ipsum dolor sit amet, consectetur adipisici elit...";
canvas.drawText(text, x, y, paint);

// draw border using getTextBounds

paint.setColor(0xffff0000);
paint.setStyle(Paint.Style.STROKE);
paint.setTypeface(font);
paint.setTextSize(fontSize);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
bounds.offset(x, y);
paint.setColor(0x80ffff00);
canvas.drawRect(bounds, paint);

// draw border using measureText

float w = paint.measureText(text);
bounds.left = x;
bounds.right = (int) Math.ceil(bounds.left + w);
bounds.top -= 10;
bounds.bottom += 10;
paint.setColor(0x8000ffff);
paint.setPathEffect(new DashPathEffect(new float[] { 10, 10 }, 0));
canvas.drawRect(bounds, paint);

可以得到以下输出:

for scaling = 0.5 I get the following output:

对于scaling = 0.51,显示以下结果:

for scaling = 0.51 the following result is shown:

黄色实心边框标记从getTextBounds传递的矩形,使用从measureText传递的宽度渲染虚线的青色矩形。

The yellow solid border marks the rect delivered from getTextBounds, the dashed cyan rect is rendered using the width delivered from measureText.

如图所示,缩放= 0.5的文本小于测量

As you can see, the text with scaling = 0.5 is smaller than the measured dimensions and with scaling=0.51 the drawn text is way bigger than the measured dimension.

任何帮助是非常感激的!

Any help is appreciated!

推荐答案

好的,只是找出了如何规避问题。

Ok, just found out how to circumvent the issue.

问题是Paint不知道Canvas缩放。因此,measureText和getTextBounds提供未缩放的结果。但是由于字体大小没有线性缩放(但是,绘制的矩形),你必须手动补偿该效果。

The problem is that the Paint does not know about the Canvas scaling. Therefore measureText and getTextBounds deliver the unscaled result. But since the font size does not scale linearly (however, the drawn rect does ), you have to make up for that effect manually.

所以解决方案是: / p>

So the solution would be:

// paint the text as usual
Paint paint = new Paint();
paint.setTypeface(font);
paint.setTextSize(fontSize);
canvas.drawText(text, x, y, paint);


// measure the text using scaled font size and correct the scaled value afterwards
Paint paint = new Paint();
paint.setTypeface(font);
paint.setTextSize(fontSize * scaling);
float w = paint.measureText(text) / scaling;

这篇关于测量在缩放的帆布的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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