onMeasure的写法

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}

private int measureWidth(int widthMeasureSpec) {
    int specMode = MeasureSpec.getMode(widthMeasureSpec);
    int specSize = MeasureSpec.getSize(widthMeasureSpec);
    int result = 0;
    if (specMode == MeasureSpec.EXACTLY) {
        result = specSize;
    } else {
        result = getPaddingLeft() + getPaddingRight() + 7 * mLineWidth;

        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }

    return result;
}


private int measureHeight(int heightMeasureSpec) {
    int specMode = MeasureSpec.getMode(heightMeasureSpec);
    int specSize = MeasureSpec.getSize(heightMeasureSpec);
    int result;
    if (specMode == MeasureSpec.EXACTLY) {
        result = specSize;
    } else {
        result = getPaddingBottom() + getPaddingTop() + mRadius * 2;

        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }

    return result;
}

你可能感兴趣的:(Android)