Android的自定义View及View的绘制流程

目标:实现Android中的自定义View,为理清楚Android中的View绘制流程“铺路”。

View在Activity中显示出来,要经历测量、布局和绘制三个步骤,分别对应三个动作:measure、layout和draw。

测量:onMeasure()决定View的大小;

布局:onLayout()决定View在ViewGroup中的位置;

绘制:onDraw()决定绘制这个View

自定义View的步骤:

  1. 自定义View的属性;

  2. 在View的构造方法中获得自定义的属性;

  3. 重写onMeasure(); --> 并不是必须的,大部分的时候还需要覆写

  4. 重写onDraw();

1.自定义属性:

<?xml version="1.0" encoding="utf-8"?> 使用自定义属性:



<com.spt.designview.view.CustomTitleView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_centerInParent=“true”
android:padding=“100dp”
custom:titleText=“3712”
custom:titleTextColor="#ff0000"
custom:titleTextSize=“40sp” />

2. 在View的构造方法中获得自定义的属性;

通常在构造方式调用3个参数的构造方法,然后在该方法中进行初始化操作。

/**

  • <默认构造函数> 获得自定义属性
    */
    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // R.styleable.CustomTitleView来自attrs.xml文件
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(
    attrs, R.styleable.CustomTitleView, defStyleAttr, 0);
    int n = typedArray.getIndexCount();
    for (int i = 0; i < n; i++) {
    int attr = typedArray.getIndex(i);
    switch (attr) {
    case R.styleable.CustomTitleView_titleText:
    mTitleText = typedArray.getString(attr);
    break;
    case R.styleable.CustomTitleView_titleTextColor:
    // 默认设置为黑色
    mTitleTextColor = typedArray.getColor(attr, Color.BLACK);
    break;
    case R.styleable.CustomTitleView_titleTextSize:
    // 默认设置为16sp,TypeValue将sp转为px
    mTitleTextSize = typedArray.getDimensionPixelSize(attr,
    (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_SP, 16,
    getResources().getDisplayMetrics()));
    default:
    break;
    }
    }
    typedArray.recycle();
    代码中引用的R.styleable.CustomTitleView就是attrs.xml中定义的名称:

3.什么时候调用onMeasure方法?

当控件的父元素正要放置该控件时调用View的onMeasure()。ViewGroup会问子控件View一个问题:“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec;这两个参数指明控件可获得的空间以及关于这个空间描述的元数据。更好的方法是传递子控件View的高度和宽度到setMeasuredDimension()里,直接告诉父控件需要多大地方放置子控件。在onMeasure()的最后都会调用setMeasuredDimension();如果不调用,将会由measure()抛出一个IllegalStateException()。

自定义View的onMeasure(): --> 测量View的大小

系统帮我们测量的高度和宽度都是MATCH_PARENT;当我们设置明确的宽度和高度时,系统测量的结果就是我们设置的结果。

当设置为WRAP_CONTENT,或者是MATCH_PARENT时,系统测量的结果就是MATCH_PARENT的长度。

当设置为WRAP_CONTENT时,而有需要进行自我测量时,就需要覆写onMeasure()。

重写之前先了解MeasureSpec的specMode,一共三种类型:

EXACTLY:一般是设置为明确的值或者是精确的值,Parent为子View决定了一个绝对尺寸,子View会被赋予这个边界限制,不管子View自己想要多大;

AT_MOST:表示子布局限制在一个最大值内,代表最大可获取的空间;代表子View可以是任意的大小,但是有一个绝对尺寸上限;

UNSPECIFIED:表示子布局想要多大就多大,很少使用;代表Parent没有对子View强加任何限制,子View可以是它想要的任何尺寸;

下面针对onMeasure()进行测量:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getMode(widthMeasureSpec);
int width = 0;

onMeasure()中传入的两个参数值,表示的是指明控件可获得的空间以及关于这个空间描述的元数据,也就是父容器对该子View的一种期望值或者一种要求。

上述的三种类型和我们.xml文件中的布局设置有什么关系?明确地说,和fill_parent、match_parent或者wrap_content有什么关系?

当设置为wrap_content时,传给onMeasure()的是AT_MOST, 表示子view的大小最多是多少,这样子View会根据这个上限来设置自己的尺寸。

当设置为fill_parent或者match_parent时,传给子View的onMeasure()的是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。

当子View的大小设置为精确值时,传给子View的onMeasure()的是EXACTLY,

而MeasureSpec的UNSPECIFIED模式目前还没有发现在什么情况下使用

此处有一个问题:为什么.xml文件中设置为wrap_content时,内容布局会全覆盖整个界面?

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width;
int height;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Log.d(TAG, “onMeasure::widthMode=” + widthMode + “; widthSize=”
+ widthSize);
Log.d(TAG, “onMeasure::heightMode=” + heightMode + “; heightSize=”
+ heightSize);
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
width = (int) (getPaddingLeft() + mBound.width() + getPaddingRight());
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
height = (int) (getPaddingTop() + mBound.height() + getPaddingBottom());
}
setMeasuredDimension(width, height);
}
上面为解决办法,如果.xml文件中写入的是wrap_content,则计算显示全部文本内容所需要的空间大小,实现展示全部内容。

  1. 重写onDraw();onDraw()绘制View,让UI界面显示出来。

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d(TAG, “onDraw::getMeasuredWidth()=” + getMeasuredWidth()
+ “; getMeasuredHeight()=” + getMeasuredHeight());
Log.d(TAG, “onDraw::getWidth()=” + getWidth() + “; getHeight()=”
+ getHeight());
mPaint.setColor(Color.YELLOW);
// 绘制背景(一个矩形框),长度为getMeasuredWidth(),高度为:getMeasuredHeight()
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint.setColor(mTitleTextColor);
// 绘制文字
canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2,
getHeight() / 2 + mBound.height() / 2, mPaint);
/**
* getMeasuredWidth()和getWidth()有什么区别?上述输出结构相同,都是300(200dp)和150(100dp)
* 什么时候上述两种方法返回不同结果?
*/
}

目标:实现Android中的自定义View,为理清楚Android中的View绘制流程“铺路”。

View在Activity中显示出来,要经历测量、布局和绘制三个步骤,分别对应三个动作:measure、layout和draw。

测量:onMeasure()决定View的大小;

布局:onLayout()决定View在ViewGroup中的位置;

绘制:onDraw()决定绘制这个View

自定义View的步骤:

  1. 自定义View的属性;

  2. 在View的构造方法中获得自定义的属性;

  3. 重写onMeasure(); --> 并不是必须的,大部分的时候还需要覆写

  4. 重写onDraw();

1.自定义属性:

<?xml version="1.0" encoding="utf-8"?>                                                 使用自定义属性:


   
   <com.spt.designview.view.CustomTitleView
       android:layout_width=“match_parent”
       android:layout_height=“match_parent”
       android:layout_centerInParent=“true”
       android:padding=“100dp”
       custom:titleText=“3712”
       custom:titleTextColor="#ff0000"
       custom:titleTextSize=“40sp” />

2.  在View的构造方法中获得自定义的属性;

通常在构造方式调用3个参数的构造方法,然后在该方法中进行初始化操作。

/**

  • <默认构造函数> 获得自定义属性
    */
    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       // R.styleable.CustomTitleView来自attrs.xml文件
       TypedArray typedArray = context.getTheme().obtainStyledAttributes(
               attrs, R.styleable.CustomTitleView, defStyleAttr, 0);
       int n = typedArray.getIndexCount();
       for (int i = 0; i < n; i++) {
           int attr = typedArray.getIndex(i);
           switch (attr) {
               case R.styleable.CustomTitleView_titleText:
                   mTitleText = typedArray.getString(attr);
                   break;
               case R.styleable.CustomTitleView_titleTextColor:
                   // 默认设置为黑色
                   mTitleTextColor = typedArray.getColor(attr, Color.BLACK);
                   break;
               case R.styleable.CustomTitleView_titleTextSize:
                   // 默认设置为16sp,TypeValue将sp转为px
                   mTitleTextSize = typedArray.getDimensionPixelSize(attr,
                          (int) TypedValue.applyDimension(
                                   TypedValue.COMPLEX_UNIT_SP, 16,
                                   getResources().getDisplayMetrics()));
               default:
                   break;
          }
      }
       typedArray.recycle();
    代码中引用的R.styleable.CustomTitleView就是attrs.xml中定义的名称:

3.什么时候调用onMeasure方法?

当控件的父元素正要放置该控件时调用View的onMeasure()。ViewGroup会问子控件View一个问题:“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec;这两个参数指明控件可获得的空间以及关于这个空间描述的元数据。更好的方法是传递子控件View的高度和宽度到setMeasuredDimension()里,直接告诉父控件需要多大地方放置子控件。在onMeasure()的最后都会调用setMeasuredDimension();如果不调用,将会由measure()抛出一个IllegalStateException()。

自定义View的onMeasure(): --> 测量View的大小

系统帮我们测量的高度和宽度都是MATCH_PARENT;当我们设置明确的宽度和高度时,系统测量的结果就是我们设置的结果。

当设置为WRAP_CONTENT,或者是MATCH_PARENT时,系统测量的结果就是MATCH_PARENT的长度。

当设置为WRAP_CONTENT时,而有需要进行自我测量时,就需要覆写onMeasure()。

重写之前先了解MeasureSpec的specMode,一共三种类型:

EXACTLY:一般是设置为明确的值或者是精确的值,Parent为子View决定了一个绝对尺寸,子View会被赋予这个边界限制,不管子View自己想要多大;

AT_MOST:表示子布局限制在一个最大值内,代表最大可获取的空间;代表子View可以是任意的大小,但是有一个绝对尺寸上限;

UNSPECIFIED:表示子布局想要多大就多大,很少使用;代表Parent没有对子View强加任何限制,子View可以是它想要的任何尺寸;

下面针对onMeasure()进行测量:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   int widthMode = MeasureSpec.getMode(widthMeasureSpec);
   int widthSize = MeasureSpec.getMode(widthMeasureSpec);
   int width = 0;

onMeasure()中传入的两个参数值,表示的是指明控件可获得的空间以及关于这个空间描述的元数据,也就是父容器对该子View的一种期望值或者一种要求。

上述的三种类型和我们.xml文件中的布局设置有什么关系?明确地说,和fill_parent、match_parent或者wrap_content有什么关系?

当设置为wrap_content时,传给onMeasure()的是AT_MOST, 表示子view的大小最多是多少,这样子View会根据这个上限来设置自己的尺寸。

当设置为fill_parent或者match_parent时,传给子View的onMeasure()的是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。

当子View的大小设置为精确值时,传给子View的onMeasure()的是EXACTLY,

而MeasureSpec的UNSPECIFIED模式目前还没有发现在什么情况下使用

此处有一个问题:为什么.xml文件中设置为wrap_content时,内容布局会全覆盖整个界面?

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   super.onMeasure(widthMeasureSpec, heightMeasureSpec);
   int width;
   int height;
   int widthMode = MeasureSpec.getMode(widthMeasureSpec);
   int widthSize = MeasureSpec.getSize(widthMeasureSpec);
   int heightMode = MeasureSpec.getMode(heightMeasureSpec);
   int heightSize = MeasureSpec.getSize(heightMeasureSpec);
   Log.d(TAG, “onMeasure::widthMode=” + widthMode + “; widthSize=”
           + widthSize);
   Log.d(TAG, “onMeasure::heightMode=” + heightMode + “; heightSize=”
           + heightSize);
   if (widthMode == MeasureSpec.EXACTLY) {
       width = widthSize;
  } else {
       width = (int) (getPaddingLeft() + mBound.width() + getPaddingRight());
  }
   if (heightMode == MeasureSpec.EXACTLY) {
       height = heightSize;
  } else {
       height = (int) (getPaddingTop() + mBound.height() + getPaddingBottom());
  }
   setMeasuredDimension(width, height);
}
上面为解决办法,如果.xml文件中写入的是wrap_content,则计算显示全部文本内容所需要的空间大小,实现展示全部内容。

  1. 重写onDraw();onDraw()绘制View,让UI界面显示出来。

@Override
protected void onDraw(Canvas canvas) {
   super.onDraw(canvas);
   Log.d(TAG, “onDraw::getMeasuredWidth()=” + getMeasuredWidth()
           + “; getMeasuredHeight()=” + getMeasuredHeight());
   Log.d(TAG, “onDraw::getWidth()=” + getWidth() + “; getHeight()=”
           + getHeight());
   mPaint.setColor(Color.YELLOW);
   // 绘制背景(一个矩形框),长度为getMeasuredWidth(),高度为:getMeasuredHeight()
   canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
   mPaint.setColor(mTitleTextColor);
   // 绘制文字
   canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2,
           getHeight() / 2 + mBound.height() / 2, mPaint);
   /**
    * getMeasuredWidth()和getWidth()有什么区别?上述输出结构相同,都是300(200dp)和150(100dp)
    * 什么时候上述两种方法返回不同结果?
    */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值