Android 基本属性绘制文本对象FontMetrics介绍
发布日期:2021-06-30 22:35:03
浏览次数:3
分类:技术文章
本文共 3589 字,大约阅读时间需要 11 分钟。
一、Android绘制文本对象FontMetrics介绍
FontMetrics对象
它以四个基本坐标为基准,分别为:・FontMetrics.top
・FontMetrics.ascent ・FontMetrics.descent ・FontMetrics.bottom要点如下:
1. 基准点是baseline 2. Ascent是baseline之上至字符最高处的距离 3. Descent是baseline之下至字符最低处的距离 4. Leading文档说的很含糊,其实是上一行字符的descent到下一行的ascent之间的距离 5. Top指的是指的是最高字符到baseline的值,即ascent的最大值 6. 同上,bottom指的是最下字符到baseline的值,即descent的最大值 Note:网上有很多错误的图,如果有疑问,就参看文档,区分对错。为了帮助理解,我特此搜索了不同的示意图。对照示意图,会很容易理解FontMetrics的参数。
pic-1pic-2
pic-3
pic-4
pic-5
pic-6二、 测试
1,测试的代码直接使用网上的代码,因为重复着众多,无所给出原始出处,故不注出。 我增加了Bitmap作为输出显示,完整代码如下:public class FontMetricsDemoActivity extends Activity { private Canvas canvas; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize( 55); textPaint.setColor( Color.WHITE); // FontMetrics对象 FontMetrics fontMetrics = textPaint.getFontMetrics(); String text = "abcdefghijklmnopqrstu"; // 计算每一个坐标 float baseX = 0; float baseY = 100; float topY = baseY + fontMetrics.top; float ascentY = baseY + fontMetrics.ascent; float descentY = baseY + fontMetrics.descent; float bottomY = baseY + fontMetrics.bottom; float leading = baseY + fontMetrics.leading; Log.d("fontMetrics", "baseX is:" + 0); Log.d("fontMetrics", "baseY is:" + 100); Log.d("fontMetrics", "topY is:" + topY); Log.d("fontMetrics", "ascentY is:" + ascentY); Log.d("fontMetrics", "descentY is:" + descentY); Log.d("fontMetrics", "bottomY is:" + bottomY); Log.d("fontMetrics", "leading is:" + leading); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fontmetrics); Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); canvas = new Canvas(mutableBitmap); // 绘制文本 canvas.drawText(text, baseX, baseY, textPaint); // BaseLine描画 Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); baseLinePaint.setColor( Color.RED); canvas.drawLine(0, baseY, canvas.getWidth(), baseY, baseLinePaint); // Base描画 canvas.drawCircle( baseX, baseY, 5, baseLinePaint); // TopLine描画 Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); topLinePaint.setColor( Color.LTGRAY); canvas.drawLine(0, topY, canvas.getWidth(), topY, topLinePaint); // AscentLine描画 Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); ascentLinePaint.setColor( Color.GREEN); canvas.drawLine(0, ascentY, canvas.getWidth(), ascentY, ascentLinePaint); // DescentLine描画 Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); descentLinePaint.setColor( Color.YELLOW); canvas.drawLine(0, descentY, canvas.getWidth(), descentY, descentLinePaint); // ButtomLine描画 Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); bottomLinePaint.setColor( Color.MAGENTA); canvas.drawLine(0, bottomY, canvas.getWidth(), bottomY, bottomLinePaint); ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap(mutableBitmap); } }
结论:
A:虽然paint和textView所设置的textSize均为55,且为相同的字符串,但是两个获得的FontMetrics属性值并不相同。但是,我们发现,做除法之后,均为1.5倍关系。做出猜测,即Paint下,为mdpi对应的size,而TextView的size已经关联到了显示屏幕本身的320dip。所以获得属性值均为整1.5倍数 B:各种情况下,均未获得leading值。转载地址:https://lovoo.blog.csdn.net/article/details/51226755 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月17日 23时08分02秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Dubbo+zookeeper 最简单的分布式搭建
2019-05-01
Zookeeper简单介绍
2019-05-01
https数字证书交换过程
2019-05-01
http协议缓存详解
2019-05-01
Echarts使用及动态加载图表数据 折线图X轴数据动态加载
2019-05-01
微信小程序 获取对应页面二维码
2019-05-01
高并发量网站解决方案
2019-05-01
接口api开发中安全性问题
2019-05-01
spring boot 知识点整理
2019-05-01
URL重定向,referer,referrer和安全
2019-05-01
Android生命周期
2019-05-01
Android游戏开发基础
2019-05-01
Android Action
2019-05-01
Android 界面切换动画控制overridePendingTransition
2019-05-01
Android进度条——ProgressDialog
2019-05-01
Android进度条自定义——类似ProgressDialog效果的Dialog
2019-05-01
Android 之 ContentProvider 与 ContentResolver
2019-05-01
【接口自动化】
2019-05-01
SpringBoot搭建一个Web工程
2019-05-01