Rect rect1 = new Rect(100,100,500,500);//画一个矩形
Paint rectPaint = new Paint();
rectPaint.setColor(Color.BLUE);
rectPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(rect1, rectPaint);
Paint textPaint = new Paint();
textPaint.setColor(SPConstants.TITLE_COLOR);
textPaint.setTextSize(SPConstants.TITLE_SIZE);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextAlign(Paint.Align.CENTER);
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
float top = fontMetrics.top;
float bottom = fontMetrics.bottom;
int baseLineY = (int) (rect.centerY() - top/2 - bottom/2);//基线中间点的y轴计算公式
canvas.drawText(str,rect.centerX(),baseLineY,textPaint);
![点击并拖拽以移动 wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==](https://i-blog.csdnimg.cn/blog_migrate/9d315b014bf374b061a24f763163c4df.png)