我正在使用DynamicLayout渲染SpannableString,但无法精确居中文本。 似乎在开始时就存在某种我无法摆脱的偏差。
这是我的实现方式:
private Editable edit;
private DynamicLayout layout;
private void drawText(Canvas canvas) {
if (layout != null) {
edit.clear();
edit.append(textValue);
layout.draw(canvas);
return;
}
edit = Editable.Factory.getInstance().newEditable(textValue);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
layout = DynamicLayout.Builder.obtain(edit , mTextPaint, (int) mCircleBounds.right)
.setAlignment(Layout.Alignment.ALIGN_CENTER)
.setLineSpacing(0, 0)
.setIncludePad(false)
.build();
} else {
layout = new DynamicLayout(edit,
mTextPaint,
canvas.getWidth(),
Layout.Alignment.ALIGN_CENTER,
0, 0,
false);
}
layout.draw(canvas);
}
非常感谢您的帮助!