之前都是通过计算左下角来居中绘制文本,发现setTextAlign可以直接实现该效果 //已知 rect Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt(); float baseLine = (mTextBgRect.top + mTextBgRect.botto
//已知 rect Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt(); float baseLine = (mTextBgRect.top + mTextBgRect.bottom - fontMetricsInt.bottom - fontMetricsInt.top) / 2; mTextPaint.setTextAlign(Paint.Align.CENTER); canvas.drawText(tip,mTextBgRect.centerX(),baseLine,mTextPaint);