package com.cooleshow.teacher.widgets;

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

import com.cooleshow.base.widgets.DensityUtil;


public class LineView extends View {

    private Paint mPaint; //画笔

    private int bgColor;
    private int mRadius; //圆形图片的半径
    private int width;
    private int size;
    private int height;


    public LineView(Context context) {
        super(context);
        mPaint = new Paint();
        mRadius = DensityUtil.dp2px(context, 3);
    }

    public LineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mRadius = DensityUtil.dp2px(context, 3);
    }

    public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mRadius = DensityUtil.dp2px(context, 3);

    }

    @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);
        RectF oval3 = new RectF(0, 0, width, height);// 设置个新的长方形
        canvas.drawRoundRect(oval3, mRadius / 2, mRadius / 2, mPaint);//第二个参数是x半径，第三个参数是y半径
    }

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

}