简体   繁体   中英

android create custom shape layout

I need to create ViewGroup custom shape. Its may be FrameLayout.It should look like 在此处输入图片说明

Its must be ViewGroup cause I need to add text or image inside.How can i do this? 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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