【发布时间】:2013-06-16 05:52:37
【问题描述】:
我有一个钢琴应用程序,上面有一些带有音符名称的圆形标签。
这些注释名称需要完全集中在标签上。
我终于想出了如何使用ascent 和descent 垂直地做到这一点。但是,我仍然无法完全确定水平对齐方式。
主要是关于单个字符的渲染。无论我做什么,有些角色都没有了。它有点,但它就在那里。
例如,请注意 E 稍微偏右,而 B 偏左。
我在onDraw() 中呈现标签,而不是使用自定义视图。我还尝试使用标签大小的TextView 并使用Gravity.CENTER,但这给出了相同的结果。另请注意,我也尝试过Align.Center。
代码:
usedPaint.setTextAlign(Align.LEFT); //Also tried it with Center
Rect textBounds = new Rect();
usedPaint.getTextBounds(infoText, 0, infoText.length(), textBounds);
float textWidth = usedPaint.measureText(infoText);
canvas.drawText(infoText, circleX - (0.5F * textWidth), circleY - ((usedPaint.descent() + usedPaint.ascent())/2),
usedPaint);
canvas.drawRect((circleX - (0.5F* textWidth)), circleY - (0.5F * textBounds.height()), circleX + (0.5F * textWidth), circleY + (0.5F * textBounds.height()), otherPaint);
绘制框的宽度是measureText 的结果。在E 字符处,您会看到它以某种方式测量左侧的一些空白,从而使字符向右漂移。
这是使用 Android 4.0.3 上的标准字体。使用自定义 TTF 字体会导致相同类型的问题,但每个字符不同。
我想知道我还能做什么? (除了克服它;))
【问题讨论】: