TextView 精确获取各种高度

来源:互联网 发布:Java bint 编辑:程序博客网 时间:2024/05/17 03:57


FontMetrics对象


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


FontMetrics.top
FontMetrics.ascent
FontMetrics.descent

FontMetrics.bottom 




//精确获取字体的高度
public int getFontHeight(float fontSize)  {  
     Paint paint = new Paint();  
     paint.setTextSize(fontSize);  
     FontMetrics fm = paint.getFontMetrics();  
    return (int) Math.ceil(fm.descent - fm.ascent);  
}  


//精确获取控件的高度
public int getTextViewHeight(float fontSize)  {  
     Paint paint = new Paint();  
     paint.setTextSize(fontSize);  
     FontMetrics fm = paint.getFontMetrics();  
    return (int) Math.ceil(fm.bottom  - fm.top);  
}  










0 0
原创粉丝点击