DrawText 奇怪的行为

发布于 2024-12-25 17:47:06 字数 425 浏览 0 评论 0原文

我想在屏幕画布上绘制与第一个字符串相邻的第二个字符串。第二个字符串的起点应该是第一个字符串的宽度。我使用了Android的paint.measuretext()方法。但它返回的宽度小于实际宽度。所以我的第二个字符串与第一个字符串重叠。任何人都可以解释如何获得字符串的准确宽度吗?

testString1 = "44444444444444444444444444444444444444444";
testString2 = "Sample Test String";
canvas.drawText(testString1,0,50, paint);
int textWidth = (int)paint.measureText(testString1);
canvas.drawText(testString2,textWidth,50, paint);

I want to draw a second string adjacent to first string on the screen canvass. The starting point of the second string should be the width of the first string.I used paint.measuretext() method of Android. But it returns the width which is lesser than the actual width. So my second String overlaps the first one. Can any body explain how to get the accurate width of a string?

testString1 = "44444444444444444444444444444444444444444";
testString2 = "Sample Test String";
canvas.drawText(testString1,0,50, paint);
int textWidth = (int)paint.measureText(testString1);
canvas.drawText(testString2,textWidth,50, paint);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2025-01-01 17:47:06

measuretext() 是更精确的测量器。因此,您在测量和绘图之间更改了文本本身或字体或字体大小或其他内容。

另请参阅此处了解其他测量方法。但不要等待太久——measuretext 是一个很好的测量工具,错误几乎肯定是在你这边。

新编辑:
检查 string1+string1 的相同函数 - 结果是否是前一个的 2 倍?如果是,则意味着您正在等待一个单元的结果,并且您正在其他单元中获得结果。 (测量文本以缩放像素工作)。也许油漆或画布使用不同的度量来设置 x、y 和文本度量。

编辑2:
不正确的缩放可以由模拟器定义。在那里尝试不同的分辨率/屏幕尺寸。如果它影响结果,您应该找到 Paint/Canvas 参数,通过它可以补偿屏幕分辨率的影响。

measuretext() is the more precise measurer. So, you have changed text itself or font or font size or something else between measuring and drawing.

Look also here for other methods of measurement. But don't wait for much - measuretext IS a good measurer and the mistake almost surely is on your side.

New Edit:
Check the same function for string1+string1 - if the result will be 2x the previous? If yes - that means that you are awaiting the result in one units and you are getting them in the other ones. (measuretext works in scaled pixels). And maybe paint or canvas use different measures for setting x,y and for text measure.

Edit 2:
The incorrect scaling can be defined by emulator. Try different resolution/screen sizes there. If it influences the result, you should find the Paint/Canvas parameter by which you can compensate the screen resolution influence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文