Android Canvas绘制正多边形和正多角星

顶点数在<5时没有正多角星,顶点数>5时正多角星不唯一,本文只绘制最具代表性的一种


代码如下:

public class CanvasStar extends View{

    public CanvasStar(Context context) {
        super(context);
    }

    public CanvasStar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    public CanvasStar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint=new Paint();
        paint.setAntiAlias(true);

        drawStar(canvas,paint,Color.RED,getHeight()/12,3,false);
        canvas.translate(getHeight()/6,0);
        drawStar(canvas,paint,Color.RED,getHeight()/12,3,true);
        canvas.translate(-getHeight()/6,0);
        canvas.translate(0,getHeight()/6);
        drawStar(canvas,paint,Color.YELLOW,getHeight()/12,4,false);
        canvas.translate(getHeight()/6,0);
        drawStar(canvas,paint,Color.YELLOW,getHeight()/12,4,true);
        canvas.translate(-getHeight()/6,0);
        canvas.translate(0,getHeight()/6);
        drawStar(canvas,paint,Color.GREEN,getHeight()/12,5,false);
        canvas.translate(getHeight()/6,0);
        drawStar(canvas,paint,Color.GREEN,getHeight()/12,5,true);
        canvas.translate(-getHeight()/6,0);
        canvas.translate(0,getHeight()/6);
        drawStar(canvas,paint,Color.CYAN,getHeight()/12,6,false);
        canvas.translate(getHeight()/6,0);
        drawStar(canvas,paint,Color.CYAN,getHeight()/12,6,true);
        canvas.translate(-getHeight()/6,0);
        canvas.translate(0,getHeight()/6);
        drawStar(canvas,paint,Color.BLUE,getHeight()/12,7,false);
        canvas.translate(getHeight()/6,0);
        drawStar(canvas,paint,Color.BLUE,getHeight()/12,7,true);
        canvas.translate(-getHeight()/6,0);
        canvas.translate(0,getHeight()/6);
        drawStar(canvas,paint,Color.BLACK,getHeight()/12,8,false);
        canvas.translate(getHeight()/6,0);
        drawStar(canvas,paint,Color.BLACK,getHeight()/12,8,true);
        canvas.translate(0,getHeight()/6);

    }

    /**
     * 绘制彩色多边形或星形
     * @param canvas Canvas画布
     * @param paint Paint画笔
     * @param color 颜色
     * @param radius 外接圆半径
     * @param count 外顶点数
     * @param isStar 是否为星形
     */
    private void drawStar(Canvas canvas, Paint paint, @ColorInt int color, float radius,int count,boolean isStar){
        canvas.translate(radius,radius);
        if ((!isStar) && count < 3){
            canvas.translate(-radius,-radius);
            return;
        }
        if (isStar && count < 5){
            canvas.translate(-radius,-radius);
            return;
        }
        canvas.rotate(-90);
        if (paint == null){
            paint = new Paint();
        }else{
            paint.reset();
        }
        Path path = new Path();
        float inerRadius = count%2==0?
                (radius*(cos(360/count/2)-sin(360/count/2)*sin(90-360/count)/cos(90-360/count)))
                :(radius*sin(360/count/4)/sin(180-360/count/2-360/count/4));
        for (int i=0;i

效果如下:

Android Canvas绘制正多边形和正多角星_第1张图片

你可能感兴趣的:(Android Canvas绘制正多边形和正多角星)