android自定义view之测量父view和迭代测量子view

来源:互联网 发布:老调网新域名 编辑:程序博客网 时间:2024/04/28 03:54
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int resWidth = 0;    int resHeight = 0;    int width = MeasureSpec.getSize(widthMeasureSpec);    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int height = MeasureSpec.getSize(heightMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {        //如果不是精确值对应的处理        resWidth = getSuggestedMinimumWidth();//取背景图的宽度        resWidth = resWidth == 0 ? getDefaultWidht() : resWidth;        resHeight = getSuggestedMinimumHeight();        resHeight = resHeight == 0 ? getDefaultWidht() : resHeight;    } else {        //如果是精确值,则取宽和高中值小的        resWidth = resHeight = Math.min(width, height);    }    setMeasuredDimension(resWidth, resHeight);    //获得半径    mRadius = Math.max(getMeasuredWidth(), getMeasuredHeight());    //menu item个数    final int childCount = getChildCount();    //menu item 尺寸    int childSize = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION);    int childMode = MeasureSpec.EXACTLY;        //迭代测量    for (int i = 0; i < childCount; i++) {        final View child = getChildAt(i);        if (child.getVisibility() == GONE) {            continue;        }        //测量menu item        int makeMeasureSpec = -1;        if (child.getId() == R.id.id_circle_menu_item_center) {            makeMeasureSpec = MeasureSpec.makeMeasureSpec((int) (mRadius * RADIO_DEFAULT_CENTERITEM_DIMENSION), childMode);        } else {            makeMeasureSpec = MeasureSpec.makeMeasureSpec(childSize, childMode);        }        child.measure(makeMeasureSpec, makeMeasureSpec);    }    mPadding = (int) (RADIO_PADDING_LAYOUT * mRadius);}
原创粉丝点击