简体   繁体   English

android创建自定义形状布局

[英]android create custom shape layout

I need to create ViewGroup custom shape. 我需要创建ViewGroup自定义形状。 Its may be FrameLayout.It should look like 它可能是FrameLayout.It应该看起来像 在此处输入图片说明

Its must be ViewGroup cause I need to add text or image inside.How can i do this? 它必须是ViewGroup,因为我需要在其中添加文本或图像。我该怎么做? Thanks a lot. 非常感谢。

There is a good tutorial here on creating custom polygon shapes. 这里有一个很好的教程,介绍如何创建自定义多边形形状。 It is a fairly long process, but you will get there. 这是一个相当长的过程,但是您会到达那里。 In short, you will have to create custom XML attributes for a custom View . 简而言之,您将必须为自定义View创建自定义XML属性。

The real magic is here: 真正的魔力在这里:

    @Override
    protected void onDraw(Canvas canvas) {
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int x = (measuredWidth/2)  ;
        int y = (measuredHeight/2) ;
        int radius = Math.min(x,y) ;

        if (sides < 3) return;

        float a = (float) (Math.PI * 2)/sides;
        int workingRadius = radius;
        polyPath.reset();

        // The poly is created as a shape in a path.
        // If there is a hole in the poly, draw a 2nd shape inset from the first
        for(int j = 0; j < ((fillPercent < 100) ? 2 : 1) ; j++){
            polyPath.moveTo(workingRadius,0);
            for (int i = 1; i < sides; i++) {
                polyPath.lineTo((float)(workingRadius*Math.cos(a*i)),
                (float)(workingRadius*Math.sin(a*i)));
            }
            polyPath.close();

            workingRadius -= radius * fillPercent;
            a = -a;
        }

        canvas.save();
        canvas.translate(x, y);
        canvas.rotate(startAngle);
        canvas.drawPath(polyPath, fillPaint);

        canvas.restore();

        if(showInscribedCircle){
            canvas.drawCircle(x,y,radius, inscribedCirclePaint);
        }
        super.onDraw(canvas);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM