(4.1.6.3)[Android]Android字体高度的研究

android自定义View绘制的时候,会有很多情况下要与字体打交道,涉及到字体宽度、高度的时候要特别注意一下几个概念,见图:

 

 

 

baseLine:一行文字的底线。

Ascent: 字符顶部到baseLine的距离。

Descent: 字符底部到baseLine的距离。

Leading: 字符行间距。

 

 

Java代码  复制代码  收藏代码
  1. public class TestOnDraw extends Activity {   
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {   
  5.         // TODO Auto-generated method stub   
  6.         super.onCreate(savedInstanceState);   
  7.         MyView v = new MyView(this);   
  8.         this.setContentView(v);   
  9.     }   
  10. }   
  11.   
  12. class MyView extends View   
  13. {   
  14.   
  15.     public MyView(Context context) {   
  16.         super(context);   
  17.     }   
  18.     @Override  
  19.     protected void onDraw(Canvas canvas) {   
  20.         super.onDraw(canvas);   
  21.            
  22.         Paint p = new Paint();   
  23.         p.setColor(Color.WHITE);   
  24.         p.setTextSize(50);   
  25.         p.setAntiAlias(true);   
  26.         FontMetrics fm = p.getFontMetrics();   
  27.            
  28.         System.out.println("top = "+ fm.top);   
  29.         System.out.println("ascent = "+ fm.ascent);   
  30.         System.out.println("descent = "+ fm.descent);   
  31.         System.out.println("bottom = "+ fm.bottom);   
  32.         System.out.println("leading = "+ fm.leading);   
  33.            
  34.         int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);   
  35.         System.out.println("textHeight = "  + textHeight);   
  36.            
  37.         float width =500;   
  38.         float baseline = 100f;   
  39.         float offsetAscent = baseline + fm.ascent;   
  40.         float offsetDescent = baseline +fm.descent;   
  41.         float offsetTop = baseline + fm.top;   
  42.         float offsetBottom = baseline + fm.bottom;   
  43.            
  44.         canvas.drawText("中国 bp Android"0, baseline, p);   
  45.            
  46.         canvas.drawLine(0, baseline, width, baseline, p);//baseline   
  47.         canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent   
  48.         canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent   
  49.         canvas.drawLine(0, offsetTop, width, offsetTop, p);//top   
  50.         canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom   
  51.     }   
  52. }  
[java]  view plain  copy
  1. public class TestOnDraw extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.         MyView v = new MyView(this);  
  8.         this.setContentView(v);  
  9.     }  
  10. }  
  11.   
  12. class MyView extends View  
  13. {  
  14.   
  15.     public MyView(Context context) {  
  16.         super(context);  
  17.     }  
  18.     @Override  
  19.     protected void onDraw(Canvas canvas) {  
  20.         super.onDraw(canvas);  
  21.           
  22.         Paint p = new Paint();  
  23.         p.setColor(Color.WHITE);  
  24.         p.setTextSize(50);  
  25.         p.setAntiAlias(true);  
  26.         FontMetrics fm = p.getFontMetrics();  
  27.           
  28.         System.out.println("top = "+ fm.top);  
  29.         System.out.println("ascent = "+ fm.ascent);  
  30.         System.out.println("descent = "+ fm.descent);  
  31.         System.out.println("bottom = "+ fm.bottom);  
  32.         System.out.println("leading = "+ fm.leading);  
  33.           
  34.         int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);  
  35.         System.out.println("textHeight = "  + textHeight);  
  36.           
  37.         float width =500;  
  38.         float baseline = 100f;  
  39.         float offsetAscent = baseline + fm.ascent;  
  40.         float offsetDescent = baseline +fm.descent;  
  41.         float offsetTop = baseline + fm.top;  
  42.         float offsetBottom = baseline + fm.bottom;  
  43.           
  44.         canvas.drawText("中国 bp Android"0, baseline, p);  
  45.           
  46.         canvas.drawLine(0, baseline, width, baseline, p);//baseline  
  47.         canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent  
  48.         canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent  
  49.         canvas.drawLine(0, offsetTop, width, offsetTop, p);//top  
  50.         canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom  
  51.     }  
  52. }  
 

运行效果:


 

 

这是程序的输出结果:


 

得出结论: canvas drawText() 的startX是从左下角的baseline的底线开始绘画的,如果我们要得到字体的高度需要关注descent - ascent (ascent线在baseline上面,所以是负数)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值