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 BackView extends View {

	private Paint paint;

	private int width;
	private int height;


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

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

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

	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public BackView(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();
			paint.setAntiAlias(true);
			paint.setStrokeWidth(5);
		}
	}

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

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawLine(width/2, height/3, width/4, height/2, paint);
		canvas.drawLine(width/4, height/2, width/2, height - height/3, paint);
	}
}
