package com.cooleshow.teacher.widgets;

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

public class RoundView extends View {

    private Paint mPaint; //画笔

    private int bgColor;
    private int width;

    private int height;


    public RoundView(Context context) {
        super(context);
        mPaint = new Paint();
    }

    public RoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
    }

    public RoundView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //因为是圆形图片，所以应该让宽高保持一致
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int center = width / 2;
        drawCircle(canvas, center, center); // 绘制进度圆弧
    }

    /**
     * 绘制进度圆弧
     *
     * @param canvas 画布对象
     * @param center 圆心的x和y坐标
     * @param radius 圆的半径
     */
    private void drawCircle(Canvas canvas, int center, int radius) {
        mPaint.setStyle(Paint.Style.FILL); // 设置绘制的圆为空心
        mPaint.setColor(bgColor);
        canvas.drawCircle(center, center, radius, mPaint); // 画底部的空心圆

    }

    /**
     * 设置圆环的底色，默认为亮灰色LTGRAY
     *
     * @param color
     */
    public void setBgColor(int color) {
        this.bgColor = color;
        invalidate();
    }


}