I am using DynamicLayout to render a SpannableString but I am not able to precisely center the text. It seems like there is some kind of offset at start that I can't get rid of.
Here is my implementation:
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);
}
Any help is greatly appreciated!