自定义View Demo

public class VerticalOffsetLayout extends ViewGroup {

private static final int OFFSET = 100;
private Paint mPaint;

public VerticalOffsetLayout(Context context) {
    super(context);
    init(context, null, 0);
}

public VerticalOffsetLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs, 0);
}

public VerticalOffsetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    mPaint = new Paint(Color.BLUE);
    mPaint.setAntiAlias(true);
    mPaint.setAlpha(125);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width = 0;
    int height = 0;

    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        ViewGroup.LayoutParams lp = child.getLayoutParams();
        int childWidthSpec = getChildMeasureSpec(widthMeasureSpec, 0, lp.width);
        int childHeightSpec = getChildMeasureSpec(heightMeasureSpec, 0, lp.height);
        child.measure(childWidthSpec, childHeightSpec);
    }

    switch (widthMode) {
        case MeasureSpec.EXACTLY:
            width = widthSize;
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.UNSPECIFIED:
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                int widthAddOffset = i * OFFSET + child.getMeasuredWidth();
                width = Math.max(width, widthAddOffset);
            }
            break;
        default:
            break;

    }

    switch (heightMode) {
        case MeasureSpec.EXACTLY:
            height = heightSize;
            break;
        case MeasureSpec.AT_MOST:
        case MeasureSpec.UNSPECIFIED:
            for (int i = 0; i < childCount; i++) {
                View child = getChildAt(i);
                height = height + child.getMeasuredHeight();
            }
            break;
        default:
            break;

    }

    setMeasuredDimension(width, height);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int left = 0;
    int right = 0;
    int top = 0;
    int bottom = 0;

    int childCount = getChildCount();

    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        left = i * OFFSET;
        right = left + child.getMeasuredWidth();
        bottom = top + child.getMeasuredHeight();

        child.layout(left, top, right, bottom);

        top += child.getMeasuredHeight();
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int x = getWidth()/2;
    int y = getHeight()/2;
    canvas.drawCircle(x, y, Math.min(x, y), mPaint);
}

}

转载于:https://blog.51cto.com/360douruanliang/2395347

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值