字体高度获取(TextView相关介绍)

Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标。

它的思路和Java.awt.FontMetrics的基本相同。

 

FontMetrics对象

它以四个基本坐标为基准,分别为:

・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent
・FontMetrics.bottom

 

该图片将如下

 

 

Java代码 复制代码  收藏代码
  1. Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);   
  2. textPaint.setTextSize( 35);   
  3. textPaint.setColor( Color.WHITE);   
  4.   
  5. // FontMetrics对象  
  6. FontMetrics fontMetrics = textPaint.getFontMetrics();   
  7.   
  8. String text = "abcdefghijklmnopqrstu";   
  9.   
  10. // 计算每一个坐标   
  11. float baseX = 0;   
  12. float baseY = 100;   
  13. float topY = baseY + fontMetrics.top;   
  14. float ascentY = baseY + fontMetrics.ascent;   
  15. float descentY = baseY + fontMetrics.descent;   
  16. float bottomY = baseY + fontMetrics.bottom;   
  17.   
  18. // 绘制文本   
  19. canvas.drawText( text, baseX, baseY, textPaint);   
  20.   
  21. // BaseLine描画   
  22. Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);>   
  23. baseLinePaint.setColor( Color.RED);   
  24. canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);   
  25.   
  26. // Base描画   
  27. canvas.drawCircle( baseX, baseY, 5, baseLinePaint);   
  28.   
  29. // TopLine描画   
  30. Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);   
  31. topLinePaint.setColor( Color.LTGRAY);   
  32. canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);   
  33.   
  34. // AscentLine描画  
  35. Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);   
  36. ascentLinePaint.setColor( Color.GREEN);   
  37. canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);   
  38.   
  39. // DescentLine描画  
  40. Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);   
  41. descentLinePaint.setColor( Color.YELLOW);   
  42. canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);   
  43.   
  44. // ButtomLine描画  
  45. Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);   
  46. bottomLinePaint.setColor( Color.MAGENTA);   
  47. canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);  


转自:http://blog.csdn.net/wangpeiyao5566/article/details/7653012

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值