Android UI

Android 简单自定义TextView

2018-01-21  本文已影响748人  花椒人生

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

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

<declare-styleable name="CMTextView">
    <attr name="cmTextColor" format="color"></attr>
    <attr name="cmText" format="string"></attr>
    <attr name="cmTextSize" format="dimension"></attr>
    <attr name="cmTextMaxLength" format="integer"></attr>
</declare-styleable>

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

    <com.test.cmviewdemo.CMTextView
        android:background="@color/colorAccent"
        app:cmTextColor="@color/colorPrimary"
        app:cmText="@string/app_name"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

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:基线

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

另外,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

上一篇下一篇

猜你喜欢

热点阅读