您当前的位置: 首页 >  android

命运之手

暂无认证

  • 2浏览

    0关注

    747博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android】【绘图】MeasureSpec

命运之手 发布时间:2016-03-25 00:36:38 ,浏览量:2

一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求。一个MeasureSpec由大小和模式组成。它有三种模式:

UNSPECIFIED:父元素部队自元素施加任何束缚,子元素可以得到任意想要的大小。

EXACTLY:父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小。

AT_MOST:子元素至多达到指定大小的值。

static int getMode(int measureSpec){}
//根据测量值提取mode

static int getSize(int measureSpec){}
//根据测量值提取size

static int makeMeasureSpec(int size,int mode){}
//根据size和mode创建测量值

//函数原型
public static int makeMeasureSpec(int size, int mode) 
{
    return size + mode;
}

//示例:ListView.measureItem(View child)
private void measureItem(View child) 
{
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) 
    {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,mListPadding.left + mListPadding.right, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) 
    {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    else 
    {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}
//makeMeasureSpec()的深层解析

//mode的值:00,01,11,按位向左移动30位
private static final int MODE_SHIFT = 30;
public static final int UNSPECIFIED = 0             
关注
打赏
1654938663
查看更多评论
0.0414s