Android 简单自定义TextView

写一个简单的自定义TextView,主要是熟悉自定义View流程。

1.在values目录下创建attrs.xml文件,在attrs.xml文件中添加自定义TextView的自定义属性


    
    
    
    

2.在布局文件中引用自己的TextView

    

3.创建自定义的TextView类

public class CMTextView extends TextView {
  public CMTextView(Context context) {
      this(context,null);
  }

  public CMTextView(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs,0);
  }

  public CMTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

  }
}

4.在构造方法中,获取自定义的属性,并且指定宽高,重新设置测量好的宽高

/**
 * View 的 测量
 * @param widthMeasureSpec
 * @param heightMeasureSpec
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    //1. 获取 自定义 View 的宽度,高度 的模式
    int heigthMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    if(MeasureSpec.AT_MOST == heigthMode){
        Rect bounds = new Rect();
        cmPaint.getTextBounds(mCmText,0,mCmText.length(),bounds);
        height = bounds.height() + getPaddingBottom() + getPaddingTop();
    }

    if(MeasureSpec.AT_MOST == widthMode){
        Rect bounds = new Rect();
        cmPaint.getTextBounds(mCmText,0,mCmText.length(),bounds);
        width = bounds.width() + getPaddingLeft() + getPaddingRight();
    }

    setMeasuredDimension(width,height);
}

5.重写onDraw()方法,重新绘制Text文字

/**
 * @param canvas
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //计算基线
    Paint.FontMetricsInt fontMetricsInt = cmPaint.getFontMetricsInt();
    int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
    int baseLine = getHeight()/2 + dy;
    int x = getPaddingLeft();
    // x: 开始的位置  y:基线
    canvas.drawText(mCmText,x,baseLine,cmPaint);
}

重点:获取TextView的基线

Canvas.drawText(text, x, y, paint) 中的参数y,指的是文字的基线(baseLine)。x 的值并不是最左边的字符的起点,绝大多数的字符,他们的宽度都是要略微大于实际显示的宽度,字符的左右会留出一部分空闲,用于文字之间的间隔,以及文字与边框之间的间隔。

FontMetircs getFontMetrics(),获取 Paint 的 FontMetrics。
FontMetrics 是个相对专业的工具类,它提供了几个文字排印方面的数值:ascent, descent, top, bottom, leading。


image.png

baseLine:基线

  • ascent/descent:上图中绿色和橙色的线,他们的作用是限制普通字符的顶部和底部范围。
    普通字符,上不会高过ascent,下不会低过descent。因此绝大部分字符都会被限定在ascent到descent之间的范围内。在Android中,ascent的值是图中绿线和baseLine的相对位移,值为负,descent的值是途中橙线基于baseLine的相对位移,值为正。
  • top/bottom:上图中蓝色线和红色线,他的作用是限制所有字形的顶部和底部范围。除了普通字符,有些字形的显示范围是会超过ascent和descent的,而top和bottom所限制的是所以字形的显示范围,包括特殊字形
  • leading:这个词的本意其实并不是行的额外间距,而是行距,即两个相邻行的 baseline 之间的距离。不过对于很多非专业领域,leading 的意思被改变了,被大家当做行的额外间距来用;而 Android 里的 leading ,同样也是行的额外间距的意思。

FontMetrics 提供的就是 Paint 根据当前字体和字号,得出的这些值的推荐值。它把这些值以变量的形式存储,供开发者需要时使用。

  • FontMetrics.ascent:float 类型。
  • FontMetrics.descent:float 类型。
  • FontMetrics.top:float 类型。
  • FontMetrics.bottom:float 类型。
  • FontMetrics.leading:float 类型。

另外,ascent 和 descent 这两个值还可以通过 Paint.ascent() 和 Paint.descent() 来快捷获取。

计算baseLine
//计算基线
Paint.FontMetricsInt fontMetricsInt = cmPaint.getFontMetricsInt();
int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
int baseLine = getHeight()/2 + dy;

从本期开始,文中Demo均上传GitHub

自定义CMTextVeiw:
https://github.com/hualianrensheng/CMViewDemo

文章引用:
Hencoder http://hencoder.com/ui-1-3/
Darren https://www.jianshu.com/p/b272528165a2

你可能感兴趣的:(Android 简单自定义TextView)