自定义ViewDemo-创新互联

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);
}

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:自定义ViewDemo-创新互联
新闻来源:http://bzwzjz.com/article/ddgjhh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站设计制作公司 教育网站设计方案 手机网站建设 成都网站建设 外贸网站设计方案 响应式网站建设 网站建设 四川成都网站制作 成都营销网站建设 成都网站建设 成都商城网站建设 成都网站设计 成都网站建设 成都网站设计 成都网站建设 app网站建设 成都h5网站建设 网站建设推广 温江网站设计 成都网站设计 手机网站制作 达州网站设计