【Android UI】TextView的垂直方向概念之top,bottom,ascent,descent,baseline
2016-05-29 17:21
633 查看
一图以蔽之
需要区分的是这里的top,bottom,ascent,descent,baseline是指字内容的属性,通过getPaint().getFontMetricsInt()来获取得到。和字体内容的外部容器的属性要区分开来。
一个小测试
我自定义了一个MyTextView:@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Ln.e("font bottom:" + getPaint().getFontMetricsInt().bottom + " \ndescent:" + getPaint().getFontMetricsInt().descent + " \nascent:" + getPaint().getFontMetricsInt().ascent + " \ntop:" + getPaint().getFontMetricsInt().top + " \nbaseline:" + getBaseline()); /** * TextView组件的属性 */ Ln.e("textview bottom:" + getBottom() + " \ntop:" + getTop() + " \nbaseline:" + getBaseline()); }
结果是:
font bottom:16
descent:14
ascent:-52
top:-60
baseline:60
textview bottom:76
top:0
baseline:60
可以总结:
字内容的坐标系和TextView组件的坐标系是不一样的字内容是以其父容器的baseline为原点的。
如果我们想自己实现一个TextView,并且实现字内容能够垂直居中,我们在画布中绘制文本的时候,会调用Canvas.drawText(String text, float x, float y, Paint paint)这个方法,其中y的坐标就是上图中baseline的y坐标,所以,如果我们只是简单地把drawText方法中的y设置为控件高度的1/2是不准确的。实际上:
yBaseline = Height/2 + (fontbottom-fontTop)/2 - fontBotton
看,这个时候就体现出以baseline为原点的好处了,因为我们在drawText的时候,都是需要输入字内容的baseline 的y坐标的。而不是bottom.
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories