Canvas 绘制文本时,使用FontMetrics对象,计算位置的坐标。

 public static class FontMetrics {
/**
* The maximum distance above the baseline for the tallest glyph in
* the font at a given text size.
*/
public float top;
/**
* The recommended distance above the baseline for singled spaced text.
*/
public float ascent;
/**
* The recommended distance below the baseline for singled spaced text.
*/
public float descent;
/**
* The maximum distance below the baseline for the lowest glyph in
* the font at a given text size.
*/
public float bottom;
/**
* The recommended additional space to add between lines of text.
*/
public float leading;
}

它的各基准线可以参考下图:

 /** 绘制FontMetrics对象的各种线 */
mPaint.reset();
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(80);
// FontMetrics对象
FontMetrics fontMetrics = mPaint.getFontMetrics();
String text = "abcdefg";
// 计算每一个坐标
float textWidth = mPaint.measureText(text);
float baseX = 30;
float baseY = 700;
float topY = baseY + fontMetrics.top;
float ascentY = baseY + fontMetrics.ascent;
float descentY = baseY + fontMetrics.descent;
float bottomY = baseY + fontMetrics.bottom;
// 绘制文本
canvas.drawText(text, baseX, baseY, mPaint);
// BaseLine描画
mPaint.setColor(Color.RED);
canvas.drawLine(baseX, baseY, baseX + textWidth, baseY, mPaint);
mPaint.setTextSize(20);
canvas.drawText("base", baseX + textWidth, baseY, mPaint);
// Base描画
canvas.drawCircle(baseX, baseY, 5, mPaint);
// TopLine描画
mPaint.setColor(Color.LTGRAY);
canvas.drawLine(baseX, topY, baseX + textWidth, topY, mPaint);
canvas.drawText("top", baseX + textWidth, topY, mPaint);
// AscentLine描画
mPaint.setColor(Color.GREEN);
canvas.drawLine(baseX, ascentY, baseX + textWidth, ascentY, mPaint);
canvas.drawText("ascent", baseX + textWidth, ascentY + 10, mPaint);
// DescentLine描画
mPaint.setColor(Color.YELLOW);
canvas.drawLine(baseX, descentY, baseX + textWidth, descentY, mPaint);
canvas.drawText("descent", baseX + textWidth, descentY, mPaint);
// ButtomLine描画
mPaint.setColor(Color.MAGENTA);
canvas.drawLine(baseX, bottomY, baseX + textWidth, bottomY, mPaint);
canvas.drawText("buttom", baseX + textWidth, bottomY + 10, mPaint);

相信通过以上程序,能够很好的理解topLine,buttomLine,baseLine,ascentLine,descentLine。

另外:Paint类有两个方法

 /**
* Return the distance above (negative) the baseline (ascent) based on the
* current typeface and text size.
*
* @return the distance above (negative) the baseline (ascent) based on the
* current typeface and text size.
*/
public native float ascent(); /**
* Return the distance below (positive) the baseline (descent) based on the
* current typeface and text size.
*
* @return the distance below (positive) the baseline (descent) based on
* the current typeface and text size.
*/
public native float descent();

ascent():the distance above the baseline(baseline以上的height)

descent():the distance below the baseline(baseline以下的height)

所以ascent() + descent() 可以看成文字的height。

到此为止,怎么获取文字的height和width都已经揭晓了:

获取height : mPaint.ascent() + mPaint.descent()

获取width : mPaint.measureText(text)

FontMetrics ----- 绘制文本,获取文本高度的更多相关文章

  1. jquery设置文本框值 与获取文本框的值

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. iOS 动态计算文本内容的高度

    关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. //  获取字符串的大小  ios6 - (CGSize)getStringRect_:(NSString* ...

  3. 【NLP】Python NLTK获取文本语料和词汇资源

    Python NLTK 获取文本语料和词汇资源 作者:白宁超 2016年11月7日13:15:24 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集 ...

  4. textarea文本域的高度随内容的变化而变化

    用css控制textarea文本域的高度随内容的变化而变化,不出现滚动条. CSS代码: 复制代码 代码如下: .t_area{ width:300px; overflow-y:visible } & ...

  5. 使用contenteditable+div模拟textarea文本域实现高度自适应

    使用contenteditable+div模拟textarea文本域实现高度自适应 开发过程中由于需要在发送消息的时候需要有一个可以高度自适应的文本域,一开始是使用textarea并搭配auto-si ...

  6. iOS之自动调节输入文本框的高度

    //自动调节输入文本框的高度 - (void)textViewDidChange:(UITableView *)textView{ float height; if ([[[UIDevice curr ...

  7. js/jquery获取文本框的值与改变文本框的值

    我们就用它来学习获取文本框的值及改变文本框的值. 代码如下 复制代码 <script>function get1(){ document.getElementById("txtb ...

  8. Jquery实现textarea根据文本内容自适应高度

    本文给大家分享的是Jquery实现textarea根据文本内容自适应高度,这些在平时的项目中挺实用的,所以抽空封装了一个文本框根据输入内容自适应高度的插件,这里推荐给小伙伴们. autoTextare ...

  9. jquery获取文本框的内容

    使用jquery获取文本框的内容有以下几种: 1.根据ID取值(id属性): // javascript <script type="text/javascript"> ...

  10. 【原】文本图片自适应高度小bug以及解决办法

    自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中

随机推荐

  1. 配置Tomcat的服务端口

  2. JavaWeb笔记——三大组件之过滤器

    过滤器JavaWeb三大组件之一,它与Servlet很相似!不它过滤器是用来拦截请求的,而不是处理请求的.  当用户请求某个Servlet时,会先执行部署在这个请求上的Filter,如果Filter“ ...

  3. Android:Android SDK Manager

    Android SDK Manager 包含:Tools(构建工具.编译工具.平台工具等) .各种版本SDK.Extras(安卓知识库和辅助工具) 每个SDK至少包含:1.SDK Plaform 2. ...

  4. OneThink实现多图片批量上传功能

    OneThink原生系统中的图片上传功能是uploadify.swf插件进行上传的,默认是只能上传一张图片的,但是uploadify.swf是支持多图片批量上传的,那么我们稍加改动就可实现OneThi ...

  5. Github 学习

    1.git$cd ~/hello-world.$git add . //这样可以自动判断新加了哪些文件,或者手动加入文件名字$git commit //提交到本地仓库,不加参数会提示,注意:^=Ctr ...

  6. Linux命令行通配符

    如果我们想对一类文件批量操作,例如批量查看硬盘文件属性,那么正常命令是如下所示: [root@localhost Desktop]# ls /dev/sda1 [root@localhost Desk ...

  7. spring springmvc mybatis 整合

    环境 apache-tomcat-8.0.33.jdk1.8.0_05 maven Dynamic Web Module 2.5 1.各个xml配置文件的配置 (1)pom.xml 配置清单文件 连接 ...

  8. What floating point types are available in .NET?

    The C# standard only lists double and float as floating points available (those being the C# shortha ...

  9. Form.ShowDialog和Form.DialogResult

    The dialog result of a form is the value that is returned from the form when it is displayed as a mo ...

  10. 红黑树、B(+)树、跳表、AVL等数据结构,应用场景及分析,以及一些英文缩写

    在网上学习了一些材料. 这一篇:https://www.zhihu.com/question/30527705 AVL树:最早的平衡二叉树之一.应用相对其他数据结构比较少.windows对进程地址空间 ...