Bach, that was stupid from me. I just needed to change this commented line:
super.getPaint().getTextBounds(text, 0, text.length(), mTextBounds);
Also, for the actual rendering of the text, I need to average this height and the height of the text:
// draw everything canvas.drawText(text, super.getWidth() * 0.5f, (super.getHeight() + mTextBounds.height()) * 0.5f, mStrokePaint);
All code is now read as follows:
public class OutlinedTextView extends TextView { private static final float OUTLINE_PROPORTION = 0.1f; private final Paint mStrokePaint = new Paint(); private final Rect mTextBounds = new Rect(); private int mOutlineColor = Color.TRANSPARENT; public OutlinedTextView(Context context) { super(context); this.setupPaint(); } public OutlinedTextView(Context context, AttributeSet attrs) { super(context, attrs); this.setupPaint(); this.setupAttributes(context, attrs); } public OutlinedTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.setupPaint(); this.setupAttributes(context, attrs); } @Override protected void onDraw(Canvas canvas) {
source share