前言

最近自定义控件,需要绘制文本,用到了获取文本宽高的代码,在此做下记录。

Paint.measureText() 获取文本宽度

   Paint paint = new Paint();   paint.setTextSize(textSize);   float strWidth = paint.measureText(content);

Paint.getTextBounds() 获取文字所在区域,进而获取文本宽高

Paint paint = new Paint();Rect rect = new Rect();  paint.getTextBounds(content, 0, content.length(), rect);  int w = rect.width();  int h = rect.height();

Paint.FontMetrics or Paint.FontMetricsInt 获取文本高度

这两个类的属性意义相同,前者单位float后者单位int。
官方文档说明:

属性 说明
ascent The recommended distance above the baseline for singled spaced text
bottom The maximum distance below the baseline for the lowest glyph in the font at a given text size
descent The recommended distance below the baseline for singled spaced text.
leading The recommended additional space to add between lines of text.
top The maximum distance above the baseline for the tallest glyph in the font at a given text size.

ascent :系统建议的,绘制单个字符时,字符应当的最高高度所在线(相对baseline为负值)
bottom:可绘制的最低高度所在线(相对baseline 为正值)
descent:系统建议的,绘制单个字符时,字符应当的最低高度所在线(相对baseline 为正值)
leading:行间距,即前一行的descent与下一行的ascent之间的距离
top:可绘制的最高高度所在线(相对baseline为负值)
baseline:为绘制基准线
代码示例子

PaintForMetrics.java

package com.xol.widget;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;/** * Created by wwzhang on 2019/3/4 */public class PaintForMetrics extends View {    private Paint mPaint;    private int mTextSize = 120;    private int mTopColor = Color.parseColor("#cc3333");    private int mAscentColor = Color.parseColor("#003366");    private int mBaseLineColor = Color.parseColor("#993333");    private int mDescentColor = Color.parseColor("#cccc00");    private int mBottomColor = Color.parseColor("#663366");    private int mBackColor = Color.parseColor("#888888");    private int mTextColor = Color.parseColor("#ffffff");    public PaintForMetrics(Context context) {        this(context, null);    }    public PaintForMetrics(Context context, @Nullable AttributeSet attrs) {        this(context, attrs, 0);    }    public PaintForMetrics(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context);    }    private void init(Context context) {        mPaint = new Paint();        mPaint.setAntiAlias(true);        mPaint.setTextSize(mTextSize);    }    @Override    protected void onDraw(Canvas canvas) {        canvas.drawColor(mBackColor);        mPaint.setColor(mTextColor);        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();        //设置基准线        float baseLine = 150;        String testContent = "Aa Bb Ff Gg Jj 张?";        //绘制文本        canvas.drawText(testContent, 0, baseLine, mPaint);        //绘制topline        mPaint.setColor(mTopColor);        canvas.drawLine(0, baseLine + fontMetrics.top                , getMeasuredWidth(), baseLine + fontMetrics.top, mPaint);        //绘制ascent        mPaint.setColor(mAscentColor);        canvas.drawLine(0, baseLine + fontMetrics.ascent                , getMeasuredWidth(), baseLine + fontMetrics.ascent, mPaint);        //绘制baseLine        mPaint.setColor(mBaseLineColor);        canvas.drawLine(0, baseLine                , getMeasuredWidth(), baseLine, mPaint);        //绘制descent        mPaint.setColor(mDescentColor);        canvas.drawLine(0, baseLine + fontMetrics.descent,                getMeasuredWidth(), baseLine + fontMetrics.descent, mPaint);        //绘制bottom        mPaint.setColor(mBottomColor);        canvas.drawLine(0, baseLine + fontMetrics.bottom,                getMeasuredWidth(), baseLine + fontMetrics.bottom, mPaint);        //绘制中心线        float centerY = getMeasuredHeight() / 2;        canvas.drawLine(0, centerY,                getMeasuredWidth(), centerY, mPaint);        //绘制竖子居中文本        float centerBaseLine = centerY + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;        canvas.drawText(testContent, 0, centerBaseLine, mPaint);    }}

ActivityForFontMetrics.java

package com.xol.viewpagerfragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;/** * Created by wwzhang on 2019/3/4 */public class ActivityForFontMetrics extends AppCompatActivity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_for_fontmetrics);    }}

布局文件

<?xml version="1.0" encoding="utf-8"?>    

运行结果:

文本单行高度:fontMetrics.bottom-fontMetrics.top。
文本高度 :fontMetric.descent-fontMetrics.ascent。
总结:通过fontMetrics绘制文本,关键点是找到绘制的基准线坐标y值。
view中心线求竖直文本居中baseLine公式:centerY+(fontMetric.bottom-fontMetric.top)/2-fontMetroc.bottom。

更多相关文章

  1. 为什么要学习 Markdown?究竟有什么用?
  2. 6.22 android计算字符高度宽度,红蓝3D图片的制作原理及NDK生成实
  3. [Android] Failed to allocate memory: 8
  4. 11月28号
  5. Android压缩图片到100K以下并保持不失真的高效方法 - feicien的
  6. Android(安卓)ImageView的scaleType属性与adjustViewBounds属性
  7. Android——EditText自定义边框、圆角和其常用属性总结
  8. Android使用StaticLayout实现文本绘制自动换行
  9. android获取屏幕的尺寸,宽度和高度

随机推荐

  1. android 使用Intent传递数据之静态变量
  2. Android Textview 字数超长显示省略号
  3. Android 常用开源库总结-2020年
  4. Android(安卓)4编程入门经典pdf
  5. android生命周期(详细总结)
  6. android:onClick都做了什么
  7. Android 4.0冰淇淋三明治SDK怎么下载
  8. android bluetooth
  9. android:fastScrollEnabled和android:dra
  10. android 图片浏览功能 图片放大缩小 使用