Android_FontMetrics
2024-06-24 20:50:39
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相关推荐
- Android中获取文本宽度和高度
本文罗列Android中字体宽度和高度的相关概念,及测量方法 . 原文请参考Android_FontMetrics.Android字符串进阶之三:字体属性及测量(FontMetrics). Andro ...
最新文章
- 论分层思想在各行各业的应用
- javaweb学习总结(七):HttpServletResponse对象(一)
- 每个产品经理都应该知道的机器学习术语
- sql 可以多个分组吗_你知道吗?罗非鱼的品种多达100多个,不单是食用鱼也可以是观赏鱼...
- Redis服务器被劫持风波
- 助力社区防疫,百数提供了一款管理系统模板
- 考研408(操作系统、计算机组成原理、数据结构、计算机网络)
- 巴西电商olist store订单数据分析
- 元宇宙构建基石:三维重建技术
- mongos魔兽世界模拟器
- 机械类和计算机类哪个累哪个难,这几个大学专业累死人还难学,但是毕业却很好就业...
- 基于Web服务的物联网-WoT(Web of Things)
- Xcode8/iOS10 升级后遇到的问题小结
- 2010年度总结-在淘宝的半年日子
- 微信小程序选择地址填写详细地址定位地点
- Pytorch:优化器、损失函数与深度神经网络框架
- 相位解包裹 matlab,二维相位解包裹matlab
- 戴眼镜会需要眼镜盒,该怎么创建眼镜盒嗯?下面我们就来看看3dmax创建眼镜盒模型的技巧。
- QT上位机:局域网特定设备ip查询和显示
- 李开复给大学生的第一封信---从诚信谈起