package com.sheep.gamegroup.view.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by realicing on 2018/9/11.
 * realicing@sina.com
 */
public class PathMultiView extends View {
    private Paint paint;
    private Path path;
    private int width, height;

    public PathMultiView(Context context) {
        this(context, null);
    }

    public PathMultiView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    private void initPaint() {
        width = 400;
        height = 400;
        path = new Path();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawMultiShape(canvas, 6, width / 2);
    }

    /**
     * @param canvas 画布
     * @param count  绘制几边形
     * @param radius //外圆的半径
     */
    public void drawMultiShape(Canvas canvas, int count, float radius) {
        canvas.translate(radius, radius);//
        if (count < 5) {
            return;
        }
        for (int i = 0; i < count; i++) {
            if (i == 0) {
                path.moveTo(radius * cos(360 / count * i), radius * sin(360 / count * i));//绘制起点
            } else {
                path.lineTo(radius * cos(360 / count * i), radius * sin(360 / count * i));
            }
        }
        paint.setStrokeWidth(3);
        path.close();
        paint.setColor(Color.GREEN);
        canvas.drawPath(path, paint);
        //因为我下面不再绘制内容了 所以画布就不恢复了
    }

    float sin(int num) {
        return (float) Math.sin(num * Math.PI / 180);
    }

    float cos(int num) {
        return (float) Math.cos(num * Math.PI / 180);
    }
}