package com.usai.apex.KPI;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class KPIDot extends View {
    public KPIDot(Context context) {
        super(context);
    }

    public KPIDot(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public KPIDot(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int mWidth,mHeight;

    int dotColor = -1;
    public void setDotColor(int color) {
        dotColor = color;
        invalidate();
    }


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

        if (dotColor == -1) {
            super.onDraw(canvas);
            return;
        }

        setBackgroundColor(Color.WHITE);

        Paint brush = new Paint(Paint.ANTI_ALIAS_FLAG);
        brush.setAntiAlias(true); // 抗锯齿
        brush.setDither(true); // 防抖动
        brush.setStyle(Paint.Style.FILL);
        brush.setColor(dotColor);

        float r = Math.min(mWidth,mHeight) * 0.5f;
        float x = (mWidth - 2 * r) * 0.5f;
        float y = (mHeight - 2 * r) * 0.5f;
        RectF rect = new RectF(x,y,x + 2 * r, y + 2 * r);
        canvas.drawArc(rect,0,360,true,brush);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;

        invalidate();
    }
}
