[Android]Android字体高度的研究
android自定义View绘制的时候,会有很多情况下要与字体打交道,涉及到字体宽度、高度的时候要特别注意一下几个概念,见图:
baseLine:一行文字的底线。
Ascent: 字符顶部到baseLine的距离。
Descent: 字符底部到baseLine的距离。
Leading: 字符行间距。
![](/images/icon_copy.gif)
![](/images/icon_star.png)
![](/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
- }
- }
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
}
}
运行效果:
这是程序的输出结果:
得出结论: canvas drawText() 的startX是从左下角的baseline的底线开始绘画的,如果我们要得到字体的高度需要关注descent - ascent (ascent线在baseline上面,所以是负数)
[Android]Android字体高度的研究相关推荐
- Android]Android字体高度的研究
android自定义View绘制的时候,会有很多情况下要与字体打交道,涉及到字体宽度.高度的时候要特别注意一下几个概念,见图: baseLine:一行文字的底线. Ascent: 字符顶部到baseL ...
- android 自定义字体_Android自定义字体教程
android 自定义字体 In this tutorial, we'll explain how to set up android custom fonts in TextViews and Bu ...
- Android系统字体加载流程
一.背景 视觉同学提了一个需求,要求手机中显示的字体可以支持medium字体,经过分析,android原生的字体库中并没有中文的medium字体,如果使用bold,显示又太粗,为满足需求,需要分析an ...
- Android系统字体规范
我们在做Android移动APP设计的时候,字号的选择也是很让人头疼,转载一份有关Android系统字体规范,如果在做Android项目的用户应该看看,如果有任何建议欢迎在留言处与我们交流探讨. 主要 ...
- Android 闪光字体
参考文章地址:https://www.jianshu.com/p/a9d09cb7577f http://blog.csdn.net/zhangphil/article/details/5200402 ...
- android 5.0主题风格研究
虽然关于Material Design开发的文章已经很多了,我也转载和翻译了一些,但是在实际应用过之后才感叹,"实践才是检验真理的唯一标准". 通常,我们使用5.0的主题风格是利用 ...
- android Button背景高度被拉伸问题--解决方案
android Button背景高度被拉伸问题--解决方案 参考文章: (1)android Button背景高度被拉伸问题--解决方案 (2)https://www.cnblogs.com/slys ...
- Android改变字体方法——Typeface
Android除了能够改变字体大小,还能改变字体样式.方法非常简单只需要将相应的字体复制到assets目录边可以实现. 步骤如下: 1.在准备字体,在windows/fonts随便拷贝一个字体将其粘贴 ...
- android 字体渲染机制,Android:字体渲染问题.ttf 3MB
我的Android应用程序需要支持各种语言(如中文等).他们大多数都在Ascender的DroidSansFallback.ttf文件中.Android:字体渲染问题.ttf> 3MB 在某些A ...
最新文章
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- [BJDCTF2020]EasySearch
- Eureka修改实例名称
- node mysql安装目录_nodejs 指定全局安装路径和缓存路径
- 自身经历第二次面试(阿里)
- python进程通信方式有几种_python全栈开发基础【第二十一篇】互斥锁以及进程之间的三种通信方式(IPC)以及生产者个消费者模型...
- 稀疏矩阵建立十字链表c语言,实验6-稀疏矩阵十字链表的存储.doc
- 如何从零设计一款牛逼的高并发架构(建议收藏)
- 那年学过的Quartz框架笔记
- Golang爬取网页数据时403
- SpringBoot 启动失败 Failed to determine a suitable driver class 问题解决方案
- 基于PHP构建OAuth 2.0 服务端 认证平台
- 使用 ListView 控件展示数据
- 与大家分享学钢琴会出现的一些问题及解决方法
- uniapp实现打印PDF文件
- python校园排课选课系统毕业设计开题报告
- 英语语法回顾1——简单句
- 破解“无法复制粘贴”
- VUE路由懒加载的方式——VUE异步组件
- CreateJS奥运五环动画
热门文章
- 【Hands-On Machine Learning】机器学习的类型
- html在input中加超链接,如何给文本框添加超链接
- Ta-Lib源码解析(三):蜡烛图指标 (Candlestick Indicator) #(附Python重构代码和示意图)(补充中)
- 2023 苹果cmsV10 动漫影视网站主题模板
- 何时何地看黄山落日晚霞
- 【Netty系列】使用Netty搭建WebSocket服务器
- 【RL】基于神经网络(深度学习)的Q学习算法(DQN)
- 小程序手动修改购物车数量的问题
- 基于SSM+Redis+RocketMQ实现的校园快递配送代拿管理系统
- 广州高腾化工科技有限公司是家骗子公司!!!