- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 Paint.getTextBounds()
测量文本,因为我对获取要呈现的文本的高度和宽度感兴趣。但是,实际呈现的文本总是比 .width()
宽一点。的Rect
getTextBounds()
填写的信息.
令我惊讶的是,我测试了 .measureText()
,并发现它返回一个不同的(更高的)值。我试了一下,发现是对的。
为什么他们报告不同的宽度?如何正确获取高度和宽度?我的意思是,我可以使用 .measureText()
,但是我不知道我是否应该信任 .height()
由 getTextBounds()
返回.
根据要求,这里是重现问题的最少代码:
final String someText = "Hello. I believe I'm some text!";
Paint p = new Paint();
Rect bounds = new Rect();
for (float f = 10; f < 40; f += 1f) {
p.setTextSize(f);
p.getTextBounds(someText, 0, someText.length(), bounds);
Log.d("Test", String.format(
"Size %f, measureText %f, getTextBounds %d",
f,
p.measureText(someText),
bounds.width())
);
}
输出显示差异不仅大于 1(并且不是最后一分钟的舍入误差),而且似乎随着大小而增加(我正要得出更多结论,但它可能完全取决于字体):
D/Test ( 607): Size 10.000000, measureText 135.000000, getTextBounds 134
D/Test ( 607): Size 11.000000, measureText 149.000000, getTextBounds 148
D/Test ( 607): Size 12.000000, measureText 156.000000, getTextBounds 155
D/Test ( 607): Size 13.000000, measureText 171.000000, getTextBounds 169
D/Test ( 607): Size 14.000000, measureText 195.000000, getTextBounds 193
D/Test ( 607): Size 15.000000, measureText 201.000000, getTextBounds 199
D/Test ( 607): Size 16.000000, measureText 211.000000, getTextBounds 210
D/Test ( 607): Size 17.000000, measureText 225.000000, getTextBounds 223
D/Test ( 607): Size 18.000000, measureText 245.000000, getTextBounds 243
D/Test ( 607): Size 19.000000, measureText 251.000000, getTextBounds 249
D/Test ( 607): Size 20.000000, measureText 269.000000, getTextBounds 267
D/Test ( 607): Size 21.000000, measureText 275.000000, getTextBounds 272
D/Test ( 607): Size 22.000000, measureText 297.000000, getTextBounds 294
D/Test ( 607): Size 23.000000, measureText 305.000000, getTextBounds 302
D/Test ( 607): Size 24.000000, measureText 319.000000, getTextBounds 316
D/Test ( 607): Size 25.000000, measureText 330.000000, getTextBounds 326
D/Test ( 607): Size 26.000000, measureText 349.000000, getTextBounds 346
D/Test ( 607): Size 27.000000, measureText 357.000000, getTextBounds 354
D/Test ( 607): Size 28.000000, measureText 369.000000, getTextBounds 365
D/Test ( 607): Size 29.000000, measureText 396.000000, getTextBounds 392
D/Test ( 607): Size 30.000000, measureText 401.000000, getTextBounds 397
D/Test ( 607): Size 31.000000, measureText 418.000000, getTextBounds 414
D/Test ( 607): Size 32.000000, measureText 423.000000, getTextBounds 418
D/Test ( 607): Size 33.000000, measureText 446.000000, getTextBounds 441
D/Test ( 607): Size 34.000000, measureText 455.000000, getTextBounds 450
D/Test ( 607): Size 35.000000, measureText 468.000000, getTextBounds 463
D/Test ( 607): Size 36.000000, measureText 474.000000, getTextBounds 469
D/Test ( 607): Size 37.000000, measureText 500.000000, getTextBounds 495
D/Test ( 607): Size 38.000000, measureText 506.000000, getTextBounds 501
D/Test ( 607): Size 39.000000, measureText 521.000000, getTextBounds 515
最佳答案
您可以按照我的做法检查此类问题:
学习 Android 源代码,Paint.java 源,同时查看 measureText 和 getTextBounds 方法。您将了解到 measureText 调用 native_measureText,getTextBounds 调用 nativeGetStringBounds,它们是用 C++ 实现的本地方法。
因此,您将继续研究 Paint.cpp,它实现了两者。
native_measureText -> SkPaintGlue::measureText_CII
nativeGetStringBounds -> SkPaintGlue::getStringBounds
现在您的研究将检查这些方法的不同之处。经过一些参数检查后,都调用了 Skia Lib(Android 的一部分)中的函数 SkPaint::measureText,但它们都调用了不同的重载形式。
深入研究 Skia,我发现两个调用都在同一个函数中产生相同的计算,只是返回结果不同。
回答您的问题:您的两个调用都进行相同的计算。结果可能的不同之处在于 getTextBounds 以整数返回边界,而 measureText 返回浮点值。
所以你得到的是在将 float 转换为 int 期间出现舍入错误,这发生在 SkPaintGlue::doTextBounds 调用函数 SkRect::roundOut 中的 Paint.cpp 中。
这两个调用的计算宽度之差可能最大为 1。
2011 年 10 月 4 日编辑
什么可能比可视化更好。我付出了努力,为了自己的探索,为了值得赏金:)
这是60号字体,红色是bounds矩形,紫色是measureText的结果。
可以看出bounds left部分从左边开始一些像素,并且measureText的值在左右都增加了这个值。这就是所谓的 Glyph 的 AdvanceX 值。 (我在 SkPaint.cpp 的 Skia 资源中发现了这一点)
所以测试的结果是 measureText 为两边的文本添加了一些提前值,而 getTextBounds 计算给定文本适合的最小边界。
希望这个结果对你有用。
测试代码:
protected void onDraw(Canvas canvas){
final String s = "Hello. I'm some text!";
Paint p = new Paint();
Rect bounds = new Rect();
p.setTextSize(60);
p.getTextBounds(s, 0, s.length(), bounds);
float mt = p.measureText(s);
int bw = bounds.width();
Log.i("LCG", String.format(
"measureText %f, getTextBounds %d (%s)",
mt,
bw, bounds.toShortString())
);
bounds.offset(0, -bounds.top);
p.setStyle(Style.STROKE);
canvas.drawColor(0xff000080);
p.setColor(0xffff0000);
canvas.drawRect(bounds, p);
p.setColor(0xff00ff00);
canvas.drawText(s, 0, bounds.bottom, p);
}
关于Android Paint : . measureText() 与 .getTextBounds(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549182/
我正在尝试以编程方式创建位图,但发现绘制带阴影的 Rect 会忽略传入的颜色 arg。我已将事情简化为这种情况 - 代码只是绘制了一个旋转的蓝色方 block ,它应该有一个灰色的影子,但影子总是蓝色
我可以轻松使用为 Paint.NET 制作的插件吗?在我自己的 C# 应用程序中(假设我遵守插件许可)? 我的目标是使某些效果自动化,Paint.NET 似乎有很多。 最佳答案 Paint.NET 以
我很好奇这些方法的作用以及如何使用它们,因为除了 Javadocs 中的单句描述之外似乎没有任何细节: setStrokeJoin setStrokeMiter 有人有一些示例代码或好的描述吗? 最佳
这个问题在这里已经有了答案: Error printing image in PyQt (1 个回答) PyQt print QWidget (1 个回答) 关闭 2 年前。 我试图在 QPixmap
我对这个感到困惑。我尝试按照一些人的建议将 QPainter 移动到它自己的 def ,但它给出了完全相同的错误。这是我创建的 def。 def PaintButtons(self): sol
我遇到这个问题,当我执行 repaint() 时,类中的 paint() 或 update() 方法没有被调用。这是代码: public class BufferedDisplay extends C
我一直在阅读关于 chrome 中的关键渲染路径 here和 here .我对这些资源的理解是“合成”步骤发生在主线程之外,并且依赖于之前的“绘制”步骤,该步骤生成要合成在一起的光栅化图层。 但是,当
在我的应用程序中,用户之一有时会出错并且应用程序崩溃。用户向我发送日志: java.lang.IndexOutOfBoundsException at android.graphics.Paint.
我正在编写简单的画图程序,您可以在其中通过拖动鼠标来绘制任何您想要的东西。您可以更改画笔的颜色和大小,但在这个版本中,当我更改画笔的颜色或大小时,当我通过拖动鼠标再次开始绘制时,之前绘制的所有内容也会
我已经实现了可点击的 Recyclerview 项并设置了 android:background="?selectableItemBackground" 以获得点击效果,但是在检查代码时我发现了这个
这是我使用 firemonkey + Delphi XE2 制作的 Delphi 应用程序的示例。 如您所见,您可以使用 Firemonkey 中的视觉样式“样式书”来自定义窗口框架内大多数内容的外观
我想请教您如何在绘画应用程序中实现自由形式的绘画。鉴于命令对象将包含单击、任意拖动和释放,这将如何必然存储在命令中并绘制到位图上下文中? 对于初学者来说,数据是否只是放在一个大列表中的 mousemo
似乎显而易见的答案是否定的,但我还是想找出答案。 如果我的面板中有很多复杂的组件,需要一段时间才能加载,然后在顶部有另一个非常基本的面板,完全覆盖它,背景面板是否仍会在 paint 方法中绘制? 最佳
首先是code: import 'package:flutter/cupertino.dart'; class Test extends StatelessWidget { @override
import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.Ac
我的java代码有问题。我想做一个画家程序,但每当我选择一个形状并绘制它时之前绘制的所有形状都变得与此形状相同。这是代码。我知道问题出在 paintComponent 中的 for 语句,但我可以用什
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这是代码,正方形会渲染但不会移动,即使它应该渲染每个循环。问题似乎出在 Core 类的 run() 中 package com.game; import java.awt.*; import
这里的问题是调用了paintComponent()方法,它获取了fillRect()所需的变量,但在按下按键后实际上并没有绘制任何东西。我不明白为什么,因为每次按下 D 键时 mato.getPosi
程序未在屏幕上显示我的 Oval。我没有收到任何错误,所以我有点停滞不前。我查看了我的另一个程序,我几乎逐字编写了它。 游戏.java public class Game extends JPanel
我是一名优秀的程序员,十分优秀!