Paint.FontMetrics

本文详细解析了TextView中Paint.FontMetrics的作用及属性,包括ascent、descent等关键概念,并通过图表直观展示了这些属性之间的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要了解TextView对文本的绘制,那么就需要了解Paint.FontMetircs。

官方对该类的解释是:Class that describes the various metrics for a font at a given text size., 意思是说,这玩意儿是绘制文本内容时存储该文本内容位置信息的一个类。

 

这个类里面存储的字段有哪些?有下面五个字段:

 

而这五个字段除了leading,其余的都是根据BaseLine来确定,也就是基线。

 

1、何为BaseLine

 

2、接下来说说里面字段的具体含义,请看下图:

根据上图可知:

    • ascent
      文字内容的顶部到基线的距离。即 ascent=文字内容顶部Y坐标 - 基线Y坐标。由于android中坐标系是 右下为正,所以得到的ascent实际是一个负数。

    • descent
      文字内容的底部到基线的距离。即 descent=文字内容底部Y坐标 - 基线Y坐标。

    • 基线 
      在图中,基线的坐标用Y表示。实际上,基线的Y坐标=文字内容中间线Y坐标+1/2 (文字内容高度)

    • top
      对应图中 文字所在行的top 坐标

    • bottom
      对应图中 文字所在行的bottom 坐标
      需要注意:如果设置了行间距,且文本内容产生了换行,那么这个bottom 也会将行间距包裹。所以, 图中蓝色的文字内容中间线的Y轴坐标并不一定等于 (bottom+top)/2

参考链接:https://www.cnblogs.com/huolongluo/p/7057973.html

 

转载于:https://www.cnblogs.com/tangZH/p/8692910.html

package com.example.demo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; public class CustomView extends ViewGroup { public CustomView(Context context) { super(context); init(context); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { // 动态添加第一个子View:TextView TextView textView = new TextView(context); textView.setText("Hello"); addView(textView); // 动态添加第二个子View:Button Button button = new Button(context); button.setText("Click me"); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.width = Gravity.BOTTOM | Gravity.END; addView(button, params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 自定义测量逻辑 int desiredWidth = 200; int desiredHeight = 100; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { width = Math.min(desiredWidth, widthSize); } else { width = desiredWidth; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { height = Math.min(desiredHeight, heightSize); } else { height = desiredHeight; } setMeasuredDimension(width, height); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // 遍历子View并设置其位置 for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); // 简单示例:将子View放置在父View中心 int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); int childLeft = (getWidth() - childWidth) / 2; int childTop = (getHeight() - childHeight) / 2; child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 在此绘制内容,例如一个红色矩形 // Paint paint = new Paint(); // paint.setColor(Color.BLUE); // // canvas.drawRect(0, 0, getWidth(), getHeight(), paint); // 使用测量后的尺寸 Log.d("CustomView", "onDraw called"); // String text = "CustomView"; // paint.setTextSize(25); // paint.setColor(Color.YELLOW); // float textWidth = paint.measureText(text); // float centerX = getWidth() / 2f; // float centerY = getHeight() / 2f; // // // 获取字体度量 // Paint.FontMetrics fontMetrics = paint.getFontMetrics(); // // 计算基线位置:使文本垂直居中 // float baseline = centerY - (fontMetrics.top + fontMetrics.bottom) / 2f; // // // 绘制文本 // canvas.drawText(text, centerX - textWidth/2, baseline, paint); } } 我这对吗
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值