Android 字体高度的研究
2017-02-08 14:47
459 查看
Android自定义View绘制的时候,会有很多情况下要与字体打交道,涉及到字体宽度、高度的时候要特别注意一下几个概念,见图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/07/8cf18c22fc404aac1eac734302184f75.gif)
baseLine:一行文字的底线。
Ascent: 字符顶部到baseLine的距离。
Descent: 字符底部到baseLine的距离。
Leading: 字符行间距。
Java代码
![](http://blog.csdn.net/images/icon_copy.gif)
![](http://blog.csdn.net/images/icon_star.png)
![](http://blog.csdn.net/images/spinner.gif)
public class TestOnDraw extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MyView v = new MyView(this);
this.setContentView(v);
}
}
class MyView extends View
{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setTextSize(50);
p.setAntiAlias(true);
FontMetrics fm = p.getFontMetrics();
System.out.println("top = "+ fm.top);
System.out.println("ascent = "+ fm.ascent);
System.out.println("descent = "+ fm.descent);
System.out.println("bottom = "+ fm.bottom);
System.out.println("leading = "+ fm.leading);
int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
System.out.println("textHeight = " + textHeight);
float width =500;
float baseline = 100f;
float offsetAscent = baseline + fm.ascent;
float offsetDescent = baseline +fm.descent;
float offsetTop = baseline + fm.top;
float offsetBottom = baseline + fm.bottom;
canvas.drawText("中国 bp Android", 0, baseline, p);
canvas.drawLine(0, baseline, width, baseline, p);//baseline
canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent
canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent
canvas.drawLine(0, offsetTop, width, offsetTop, p);//top
canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom
}
}
[java] view
plain copy
public class TestOnDraw extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MyView v = new MyView(this);
this.setContentView(v);
}
}
class MyView extends View
{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setTextSize(50);
p.setAntiAlias(true);
FontMetrics fm = p.getFontMetrics();
System.out.println("top = "+ fm.top);
System.out.println("ascent = "+ fm.ascent);
System.out.println("descent = "+ fm.descent);
System.out.println("bottom = "+ fm.bottom);
System.out.println("leading = "+ fm.leading);
int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
System.out.println("textHeight = " + textHeight);
float width =500;
float baseline = 100f;
float offsetAscent = baseline + fm.ascent;
float offsetDescent = baseline +fm.descent;
float offsetTop = baseline + fm.top;
float offsetBottom = baseline + fm.bottom;
canvas.drawText("中国 bp Android", 0, baseline, p);
canvas.drawLine(0, baseline, width, baseline, p);//baseline
canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent
canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent
canvas.drawLine(0, offsetTop, width, offsetTop, p);//top
canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom
}
}
运行效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/07/8ff9552a3b26d08ef6db8bf4f8be1b91.jpg)
这是程序的输出结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/07/df1b545140b80917a90292212daf277a.jpg)
得出结论: canvas drawText() 的startX是从左下角的baseline的底线开始绘画的,如果我们要得到字体的高度需要关注descent - ascent (ascent线在baseline上面,所以是负数)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/07/8cf18c22fc404aac1eac734302184f75.gif)
baseLine:一行文字的底线。
Ascent: 字符顶部到baseLine的距离。
Descent: 字符底部到baseLine的距离。
Leading: 字符行间距。
Java代码
![](http://blog.csdn.net/images/icon_copy.gif)
![](http://blog.csdn.net/images/icon_star.png)
![](http://blog.csdn.net/images/spinner.gif)
public class TestOnDraw extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MyView v = new MyView(this);
this.setContentView(v);
}
}
class MyView extends View
{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setTextSize(50);
p.setAntiAlias(true);
FontMetrics fm = p.getFontMetrics();
System.out.println("top = "+ fm.top);
System.out.println("ascent = "+ fm.ascent);
System.out.println("descent = "+ fm.descent);
System.out.println("bottom = "+ fm.bottom);
System.out.println("leading = "+ fm.leading);
int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
System.out.println("textHeight = " + textHeight);
float width =500;
float baseline = 100f;
float offsetAscent = baseline + fm.ascent;
float offsetDescent = baseline +fm.descent;
float offsetTop = baseline + fm.top;
float offsetBottom = baseline + fm.bottom;
canvas.drawText("中国 bp Android", 0, baseline, p);
canvas.drawLine(0, baseline, width, baseline, p);//baseline
canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent
canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent
canvas.drawLine(0, offsetTop, width, offsetTop, p);//top
canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom
}
}
[java] view
plain copy
public class TestOnDraw extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MyView v = new MyView(this);
this.setContentView(v);
}
}
class MyView extends View
{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.WHITE);
p.setTextSize(50);
p.setAntiAlias(true);
FontMetrics fm = p.getFontMetrics();
System.out.println("top = "+ fm.top);
System.out.println("ascent = "+ fm.ascent);
System.out.println("descent = "+ fm.descent);
System.out.println("bottom = "+ fm.bottom);
System.out.println("leading = "+ fm.leading);
int textHeight = (int) (Math.ceil(fm.descent - fm.ascent) + 2);
System.out.println("textHeight = " + textHeight);
float width =500;
float baseline = 100f;
float offsetAscent = baseline + fm.ascent;
float offsetDescent = baseline +fm.descent;
float offsetTop = baseline + fm.top;
float offsetBottom = baseline + fm.bottom;
canvas.drawText("中国 bp Android", 0, baseline, p);
canvas.drawLine(0, baseline, width, baseline, p);//baseline
canvas.drawLine(0, offsetAscent, width, offsetAscent, p);//ascent
canvas.drawLine(0, offsetDescent, width, offsetDescent, p);//descent
canvas.drawLine(0, offsetTop, width, offsetTop, p);//top
canvas.drawLine(0, offsetBotton, width, offsetBottom, p);//bottom
}
}
运行效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/07/8ff9552a3b26d08ef6db8bf4f8be1b91.jpg)
这是程序的输出结果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201408/07/df1b545140b80917a90292212daf277a.jpg)
得出结论: canvas drawText() 的startX是从左下角的baseline的底线开始绘画的,如果我们要得到字体的高度需要关注descent - ascent (ascent线在baseline上面,所以是负数)
相关文章推荐
- (4.1.36.10)[Android]Android字体高度的研究
- [Android]Android字体高度的研究
- [Android]Android字体高度的研究
- Android]Android字体高度的研究
- android[API]Android字体高度的研究
- [Android]Android字体高度的研究
- Android字体高度的研究
- Android字体高度的研究
- Android获取字体高度和设置行高
- 如何改变Android tab 的高度和字体大小
- Android根据所设置的textSize获取字体高度和设置行高
- Android自定义TextView根据控件高度和宽度,解决字体适配问题
- android 关于app内设置字体样式的研究
- 如何改变Android tab 的高度和字体大小
- IOS、Android、WP移动平台浏览器下常用字体支持度研究
- 『ANDROID』IOS、Android、WP移动平台浏览器下常用字体支持度研究
- 如何改变Android tab 的高度和字体大小
- android 得到字体的高度
- android字体高度,宽度计算方法
- android根据字体大小设置控件高度