public void onDraw (Canvas canvas) {
    Rect targetRect = new Rect(50501000200);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint.setStrokeWidth(3); 
    paint.setTextSize(80);
    String testString = "Text:中文asdfghjkl";
    paint.setColor(Color.CYAN); 
    canvas.drawRect(targetRect, paint);
    paint.setColor(Color.RED);
    FontMetricsInt fontMetrics = paint.getFontMetricsInt();
    int baseline = targetRect.top + (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
    // 以下这行是实现水平居中。drawText相应改为传入targetRect.centerX() 
    paint.setTextAlign(Paint.Align.CENTER);  
    canvas.drawText(testString, targetRect.centerX(), baseline, paint);  
}

相关文章:

  • 2021-08-31
  • 2021-11-13
  • 2021-06-12
  • 2021-09-08
  • 2021-03-29
  • 2021-09-08
猜你喜欢
  • 2022-02-21
  • 2021-11-16
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案