Android DrawText 基线的确定问题
来源:互联网 发布:matlab 2017b mac 编辑:程序博客网 时间:2024/05/01 22:27
在自定义控件的时候,有时候会用到DrawText 方法
- @param text The text to be drawn
- @param x The x-coordinate of the origin of the text being drawn
- @param y The y-coordinate of the baseline of the text being drawn
- @param paint The paint used for the text (e.g. color, size, style)
*/
public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)
x,y 分别表示 基线的开始坐标,并不是 文字左上角的坐标,因为文字的绘制是以基线为基础的
图中的 五角星 所在的线 就是基线 BaseLine
那么如何确定基线的x,y坐标呢
需要借助 Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
FontMetricsInt 类 有 top 、bottom 两个成员,
top表示基线到文字最上面的位置的距离 是个负值 bottom为基线到最下面的距离,为正值
如果想要基于一个位置竖直居中,那么居中的位置 坐标假设为 centerY
baseLineY = centerY - (fm.bottom-fm.top)/2- fm.top;
这样就可以确定基线的坐标
所以只需要修改centerY的位置 就可以基于这个位置居中了
或者 比如想在 top 到 bottom 之间竖直居中 那么center = (bottom-top)/2;
baseLineY = (bottom-top)/2 - (fm.bottom-fm.too)/2 - fm.top=
(bottom+top-fm.bottom-fm.top)/2;
最后 不要忘记在 paint.getFontMetricsInt(); 之前设置一下字体大小哦
0 0
- Android DrawText 基线的确定问题
- Android自定义控件---DrawText 基线的确定
- android下event的确定
- android动画时间的确定
- drawText()中的文字基线
- 聚类分析中分类数的确定问题
- Android布局(控件高度的确定)
- Android中View大小的确定过程
- Android中View大小的确定过程
- Android 设备屏幕尺寸的确定
- android Dialog的确定按钮点击后不取消对话框
- Android监听键盘上的确定键等
- android Dialog的确定按钮点击后不取消对话框
- Android监听键盘上的确定键等
- 工作风格的确定
- 重载函数的确定
- J2EE项目的确定
- 指针类型的确定
- 【前端笔试题】数组去重
- AngularJS入门(10)-Angular事件
- 一部分常用的正则表达式代码
- 【C语言】顺序表并集与归并
- 第10周 项目2- 二叉树遍历的递归算法
- Android DrawText 基线的确定问题
- svn合并的一些坑
- path与classpath区别
- 第10周项目3-利用二叉树遍历思想解决问题
- 单例模式、双检测锁定DCL、volatile(转)
- Perl常用lib索引
- openlayers cgi proxy跨域问题总结
- zabbix 监控web并添加报警
- Bootstrap Responsive Buttons