我正在使用 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);
}
任何帮助是极大的赞赏!