自定义一个类继承View,在onDraw方法中画虚线,虚线用DashPathEffect实现
public class DashedView extends View {
private Paint paint;
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
// 方向
private int oritation;
// 颜色
private int dashedColor;
// 线段长度
private float lineLength;
// 间隔长度
private float spaceLength;
// 线粗
private float dashedWidth;
private int mWidth;
private int mHeight;
private Path path;
public DashedView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.DashedView);
dashedColor = typedArray.getColor(R.styleable.DashedView_dashedColor,
0x000000);
oritation = typedArray.getInt(R.styleable.DashedView_dashedOritation,
HORIZONTAL);
lineLength = typedArray.getDimension(R.styleable.DashedView_lineLength,
10.0f);
spaceLength = typedArray.getDimension(
R.styleable.DashedView_spaceLength, 5.0f);
dashedWidth = typedArray.getDimension(
R.styleable.DashedView_dashedWidth, 2.0f);
paint = new Paint();
paint.reset();
paint.setColor(dashedColor);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(dashedWidth);
DashPathEffect effects = new DashPathEffect(new float[] { lineLength,
spaceLength }, 0);
paint.setPathEffect(effects);
path = new Path();
}
public DashedView(Context context) {
this(context, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
//居中画
if (oritation == VERTICAL) {
path.moveTo((mWidth - dashedWidth) / 2, 0);
path.lineTo((mWidth - dashedWidth) / 2, mHeight);
} else {
path.moveTo(0, (mHeight - dashedWidth) / 2);
path.lineTo(mWidth, (mWidth - dashedWidth) / 2);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
}
再附上attrs文件
<declare-styleable name="DashedView">
<!-- 虚线颜色 -->
<attr name="dashedColor" format="color" />
<!-- 方向 -->
<attr name="dashedOritation" >
<enum name="vertical" value="0" />
<enum name="horizontal" value="1" />
</attr>
<!-- 每段线长 -->
<attr name="lineLength" format="dimension"/>
<!-- 间隔长度 -->
<attr name="spaceLength" format="dimension"/>
<!-- 虚线粗 -->
<attr name="dashedWidth" format="dimension"/>
</declare-styleable>