如何准确测量通过 drawTextOnPath() 在画布上绘制的文本的像素大小
我正在使用 drawTextOnPath() 在画布上显示一些文本,我需要知道正在绘制的文本的尺寸。我知道这对于由多个线段、曲线等组成的路径是不可行的,但我的路径是完全水平的单个线段。我正在使用 Paint.getTextBounds() 获取一个具有我想要绘制的文本尺寸的矩形。
当我在任意位置绘制文本时,我使用此矩形在文本周围绘制边界框。
下面是一些简化的代码,反映了我当前正在做的事情:
// to keep this example simple, always at origin (0,0)
public drawBoundedText(Canvas canvas, String text, Paint paint) {
Rect textDims = new Rect();
paint.getTextBounds(text,0, text.length(), textDims);
float hOffset = 0;
float vOffset = paint.getFontMetrics().descent; // vertically centers text
float startX = textDims.left; / 0
float startY = textDims.bottom;
float endX = textDims.right;
float endY = textDims.bottom;
path.moveTo(startX, startY);
path.lineTo(endX, endY);
path.close();
// draw the text
canvas.drawTextOnPath(text, path, 0, vOffset, paint);
// draw bounding box
canvas.drawRect(textDims, paint);
}
结果很接近,但并不完美。如果我将倒数第二行替换为:
canvas.drawText(text, startX, startY - vOffset, paint);
那么它就可以正常工作。通常右边缘和下边缘有 1-3 个像素的间隙。该错误似乎也随着字体大小的变化而变化。有什么想法吗?我可能做的一切都是正确的,问题出在drawTextOnPath();沿路径绘制时,即使路径是水平的,文本质量也会明显下降,这可能是由于插值算法或其在幕后使用的任何内容所致。如果发现尺寸抖动也来自那里,我不会感到惊讶。
I'm using drawTextOnPath() to display some text on a Canvas and I need to know the dimensions of the text being drawn. I know this is not feasible for paths composed of multiple segments, curves, etc. but my path is a single segment which is perfectly horizontal. I am using Paint.getTextBounds() to get a Rect with the dimensions of the text I want to draw.
I use this rect to draw a bounding box around the text when I draw it at an arbitrary location.
Here's some simplified code that reflects what I am currently doing:
// to keep this example simple, always at origin (0,0)
public drawBoundedText(Canvas canvas, String text, Paint paint) {
Rect textDims = new Rect();
paint.getTextBounds(text,0, text.length(), textDims);
float hOffset = 0;
float vOffset = paint.getFontMetrics().descent; // vertically centers text
float startX = textDims.left; / 0
float startY = textDims.bottom;
float endX = textDims.right;
float endY = textDims.bottom;
path.moveTo(startX, startY);
path.lineTo(endX, endY);
path.close();
// draw the text
canvas.drawTextOnPath(text, path, 0, vOffset, paint);
// draw bounding box
canvas.drawRect(textDims, paint);
}
The results are -close- but not perfect. If I replace the second to last line with:
canvas.drawText(text, startX, startY - vOffset, paint);
Then it works perfectly. Usually there is a gap of 1-3 pixels on the right and bottom edges. The error seems to vary with font size as well. Any ideas? It's possible I'm doing everything right and the problem is with drawTextOnPath(); the text quality very visibly degrades when drawing along paths, even if the path is horizontal, likely because of the interpolation algorithm or whatever its using behind the scenes. I wouldnt be surprised to find out that the size jitter is also coming from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个适合我的特定情况的解决方案,尽管它并没有真正解决最初的问题。
由于我的特定用例仅沿着单段直线路径绘制文本,因此我能够一起摆脱路径并使用 Canvas.drawText() 以及 Canvas.translate() 和 canvas.rotate() 。这消除了我在沿着路径绘制时看到的文本大小随机性和丑陋。
I've found a solution that works for my particular situation, although it doesn't really resolve the original question.
Since my particular use case only draws text along single segment straight paths, I was able to get rid of the paths all together and use Canvas.drawText() along with Canvas.translate() and canvas.rotate(). This got rid of the text size randomness and ugliness I was seeing when drawing along paths as well.