package com.sfbest.mapp.mipad.common.view;
import com.sfbest.mapp.mipad.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by liu on 15/4/22.
*
*/
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████ ┃+
* ┃ ┃ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃
* ┃ ┃ + + + +
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ + 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃ +
* ┃ ┗━━━┓ + +
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
public class LineView extends View {
private static final int DEFAULT_LINE_COLOR = Color.BLACK;
private static final boolean DEFAULT_LINE_STYLE = true;
private int lineColor = DEFAULT_LINE_COLOR;
/**
* 水平虚线,还是垂直虚线,true为水平,false为垂直,默认为true(水平)
*/
private boolean lineStyle = DEFAULT_LINE_STYLE;
private int top;
private int left;
private int right;
private int bottom;
public LineView(Context context) {
super(context);
}
public LineView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LineView, defStyleAttr, 0);
lineColor = a.getColor(R.styleable.LineView_line_color,
DEFAULT_LINE_COLOR);
lineStyle = a.getBoolean(R.styleable.LineView_line_style, DEFAULT_LINE_STYLE);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(lineColor);
Path path = new Path();
if (lineStyle) {
path.moveTo(0, getHeight() / 2);
path.lineTo(getWidth(), getHeight() / 2);
} else {
path.moveTo(getWidth() / 2, 0);
path.lineTo(getWidth() / 2, getHeight());
}
PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
paint.setPathEffect(effects);
canvas.drawPath(path, paint);
}
}
代码链接