Android自定义控件onMeasure、onLayout介绍

Android中自定义View时经常会需要重写View的onMeasure(),onLayout()方法。

onMeasure()主要是用来测量控件的大小位置,而onLayout()主要用来布局控件,绘制控件的位置面。

分别介绍下这两个方法。

一、onMeasure()方法

onMeasure(int widthMeasureSpec,int heightMeasureSpec)

1、调用时间:当控件的父元素放置该控件时,用于告诉父元素该控件需要的大小。

2、传入参数:widthMeasureSpec,heightMeasureSpec。这两个传入参数由高32位和低16位组成,高32位保存的值叫specMode,可以通过MeasureSpec.getMode()获取;低16位为specSize可以由MeasureSpec.getSize()获取。这两个值是由ViewGroup中的layout_width,layout_height和padding以及View自身的layout_margin共同决定。权值weight也是尤其需要考虑的因素,有它的存在情况可能会稍微复杂点。

specMode可以取三个值:MeasureSpec.EXACTLY ,MeasureSpec.AT_MOSTMeasureSpec.UNSPECIFIEDspecMode与layout_的对应关系如下:

match_parent- MeasureSpec.EXACTLY:当layout_为match_parent或者为某一具体值的时候specMode为EXACTLY代表精确的值

wrap_content- MeasureSpec.AT_MOST:表示能获得的最大尺寸;

当无法确定尺寸的时候则是 MeasureSpec.UNSPECIFIED,这时候specSize会为最小值(即0),也可以设置一个默认的最小值;

3、可以在onMeasure()中来计算控件的尺寸,然后根据setMeasuredDimension(mWidth,mHeight);方法来告诉父控件此控件需要的尺寸,onMeasure()方法中必须调用此方法。

4、值得注意的是:

1)specSize和传入setMeasuredDimension()方法中的值的单位都是px(dp*density就是px)。

2)match_parent并不是填充整个父容器,而是在不覆盖已经加入父容器的控件的情况下填充父容器。

例如:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
    height = 0;
    switch (heightSpecMode) {
        case MeasureSpec.AT_MOST: // wrap_content时,子最大可以达到的指定大小
            height = (int) dp2px(DEFAULT_HEIGHT_DP);
            break;
        case MeasureSpec.EXACTLY: // 指定View的高宽或者match_parent时,模式为EXACTLY
        case MeasureSpec.UNSPECIFIED: // 父不没有对子施加任何约束
            height = heightSpecSize;
            break;
    }
    setMeasuredDimension(widthSpecSize, height);
}

当有多个子控件时:

//我这里每行只放一个View@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentWidthSize = 0;
    int paretnHeightSize = 0;
    for (int i = 1; i < getChildCount(); i++) {
        View childView = getChildAt(i);
        // 设定子view宽、高
        childView.measure(
                MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY));
        parentWidthSize = childView.getMeasuredWidth();
        paretnHeightSize += childView.getMeasuredHeight();
    }
    setMeasuredDimension(parentWidthSize, paretnHeightSize);
}


二、onLayout  

    onLayout(boolean changed, int left, int top,int right,int bottom);

    父容器的onLayout()调用子类的onLayout()来确定子view在viewGroup中的位置,如:onLayout(10,10,100,100)表示子容器在父容器中(10,10)位置显示,长、宽都是90。结合onMeasure()方法使用可以确定子view的布局。

其中的

l是childView在这个自定义Viewgroup中距离左边的距离,

t是距离上边的距离,

r是自定义Viewgroup中多宽,

b是自定义Viewgroup中多高。

可以这样理解,把这个子View看作一个矩形,那么就可以把l、t看作是矩形左上角的坐标,r、b看作是右下角的坐标,这样比较好理解

  当只有一个子控件的时候:

@Override
protected  void  onLayout( boolean  changed,  int  l,  int  t,  int  r,  int  b) {
       View childView = getChildAt(i);
       int  Width = childView.getMeasuredWidth();
       int  height = childView.getMeasuredHeight();             
       childView.layout( l t , childWidth , height );
}

  当有多个子控件的时候:

@Override
protected  void  onLayout( boolean  changed,  int  l,  int  t,  int  r,  int  b) {
     for  ( int  i =  1 ; i < getChildCount(); i++) {
         View childView = getChildAt(i);
         int  Width = childView.getMeasuredWidth();
         int  height = childView.getMeasuredHeight();           
         childView.layout( 0 0 Width height );
     }
}

当然,也可以不用for循环来布局子控件,如在绘制弹性上下拉控件时:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (!isLayout) {
        // 这里是第一次进来的时候做一些初始化
        mHeadView = new LoadingLayout(mContext, getChildAt(0), true);
        pullableView = getChildAt(1);
        mFootView = new LoadingLayout(mContext, getChildAt(2), false);
        isLayout = true;
        refreshDist = ((ViewGroup) mHeadView.rootView).getChildAt(0)
                .getMeasuredHeight();
        loadmoreDist = ((ViewGroup) mFootView.rootView).getChildAt(0)
                .getMeasuredHeight();
    }
    // 改变子控件的布局,这里直接用(pullDownY + pullUpY)作为偏移量,这样就可以不对当前状态作区分
    mHeadView.rootView.layout(0,
            (int) (pullDownY + pullUpY) - mHeadView.rootView.getMeasuredHeight(),
            mHeadView.rootView.getMeasuredWidth(), (int) (pullDownY + pullUpY));
    pullableView.layout(0, (int) (pullDownY + pullUpY),
            pullableView.getMeasuredWidth(), (int) (pullDownY + pullUpY)
                    + pullableView.getMeasuredHeight());
    mFootView.rootView.layout(0,
            (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight(),
            mFootView.rootView.getMeasuredWidth(),
            (int) (pullDownY + pullUpY) + pullableView.getMeasuredHeight()
                    + mFootView.rootView.getMeasuredHeight());
}

pullableView是我们的弹性控件,可以是listview或者scrollview;
mFootView是底部控件;
mHeadView是头部控件;
通过不断的改变pullDownY或者pullUpY,然后不断的重绘视图,来达到弹性效果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值