package com.gizwits.amap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Jason on 2018/10/16.
 */

public class CenterCircleView extends View {

	private Paint paint;
	private int cX; //中心坐标X
	private int cY; //中心坐标Y
	private Context mContext;

	private int width;
	private int height;


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

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

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

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public CenterCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		width = right - left;
		height = bottom - top;
		init();
	}

	private void init() {
		if(paint == null) {
			paint = new Paint();
		}
		cX = width-2 >> 1;
		cY =  height/2 +(height - width)/2;
	}

	public void setColor(int color){
		if(paint == null) {
			paint = new Paint();
		}
		paint.setColor(color);
		postInvalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setStrokeWidth(3);
		paint.setAntiAlias(true);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		canvas.drawCircle(cX, cY, width/4, paint);
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawCircle(cX, cY, width/2 - 5, paint);

	}
}
