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)

Android_FontMetrics相关推荐

  1. Android中获取文本宽度和高度

    本文罗列Android中字体宽度和高度的相关概念,及测量方法 . 原文请参考Android_FontMetrics.Android字符串进阶之三:字体属性及测量(FontMetrics). Andro ...

最新文章

  1. 论分层思想在各行各业的应用
  2. javaweb学习总结(七):HttpServletResponse对象(一)
  3. 每个产品经理都应该知道的机器学习术语
  4. sql 可以多个分组吗_你知道吗?罗非鱼的品种多达100多个,不单是食用鱼也可以是观赏鱼...
  5. Redis服务器被劫持风波
  6. 助力社区防疫,百数提供了一款管理系统模板
  7. 考研408(操作系统、计算机组成原理、数据结构、计算机网络)
  8. 巴西电商olist store订单数据分析
  9. 元宇宙构建基石:三维重建技术
  10. mongos魔兽世界模拟器
  11. 机械类和计算机类哪个累哪个难,这几个大学专业累死人还难学,但是毕业却很好就业...
  12. 基于Web服务的物联网-WoT(Web of Things)
  13. Xcode8/iOS10 升级后遇到的问题小结
  14. 2010年度总结-在淘宝的半年日子
  15. 微信小程序选择地址填写详细地址定位地点
  16. Pytorch:优化器、损失函数与深度神经网络框架
  17. 相位解包裹 matlab,二维相位解包裹matlab
  18. 戴眼镜会需要眼镜盒,该怎么创建眼镜盒嗯?下面我们就来看看3dmax创建眼镜盒模型的技巧。
  19. QT上位机:局域网特定设备ip查询和显示
  20. 李开复给大学生的第一封信---从诚信谈起

热门文章

  1. 微服务化不同阶段 Kubernetes 的不同玩法
  2. Linux 学习--一篇就学懂Linux(万字长文)
  3. Magento 屏蔽国内IP,允许国内特定地区的IP访问
  4. 动漫文字卡片封面制作
  5. 使用python生成随机中奖姓名和电话号码并存入excel
  6. c语言除号“/”的基本规则
  7. HDU 5445 (多重背包)
  8. 腾讯云卖向“有币”区块链
  9. NASA官网下载全球雨量数据
  10. python属于什么部门_如何理解简历中部门名称的模式?