Rect mBounds = new Rect();
mPaint.setColor(Color.BLUE);
canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
mPaint.setColor(Color.YELLOW);
mPaint.setTextSize(30);
String text = String.valueOf(mCount);
mPaint.getTextBounds(text, 0, text.length(), mBounds);
float textWidth = mBounds.width();
float textHeight = mBounds.height();
canvas.drawText(text, getWidth() / 2 - textWidth / 2, getHeight() / 2
+ textHeight / 2, mPaint);
getTextBounds 是将TextView 的文本放入一个矩形中, 测量TextView的高度和宽度,还有一下方法
参考:http://blog.youkuaiyun.com/it_transformers/article/details/46041899