DrawText 奇怪的行为
我想在屏幕画布上绘制与第一个字符串相邻的第二个字符串。第二个字符串的起点应该是第一个字符串的宽度。我使用了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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.