import android.annotation.SuppressLint;
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;
public class DashedLineView extends View{
private Paint paint = null;
private Path path = null;
private PathEffect effects = null;
private int orientation;
private int lineColor;
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
private static final int DEFAULT_COLOR_THEME = Color
.parseColor("#ff000000");
public DashedLineView(Context context) {
super(context, null);
// TODO Auto-generated constructor stub
}
public DashedLineView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
// TODO Auto-generated constructor stub
setCustomAttributes(attrs);
}
public DashedLineView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
setCustomAttributes(attrs);
}
private void setCustomAttributes(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.dashedline);
lineColor = a.getColor(R.styleable.dashedline_lineColor,
DEFAULT_COLOR_THEME);
orientation = a.getInt(R.styleable.dashedline_orientation, HORIZONTAL);
paint = new Paint();
path = new Path();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(lineColor);
paint.setAntiAlias(true);
a.recycle();
}
@SuppressLint("DrawAllocation")
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// path.moveTo(0, 0);
// if (orientation == VERTICAL) {
// path.lineTo(0, this.getHeight());
// } else {
// path.lineTo(this.getWidth(), 0);
// }
// // PathEffect是用来控制绘制轮廓(线条)的方式
// // 代码中的float数组,必须是偶数长度,且>=2,指定了多少长度的实线之后再画多少长度的空白.如本代码中,绘制长度5的实线,再绘制长度5的空白,再绘制长度5的实线,再绘制长度5的空白,依次重复.1是偏移量,可以不用理会.
//
effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
// effects = new DashPathEffect(new float[]{8, 10, 8, 10}, 0);
// paint.setPathEffect(effects);
// canvas.drawPath(path, paint);
float startY = getHeight();
float startX = getWidth() / 2;
DashPathEffect dashPathEffect =
new DashPathEffect(new float[]{8, 10, 8, 10}, 0);
paint.setPathEffect(dashPathEffect);
Path path = new Path();
path.moveTo(startX,0);
path.lineTo(startX,startY);
canvas.drawPath(path,paint);
}
}
布局中应用
<com.xxx.xxxx.widgets.DashedLineView
android:layout_marginLeft="8dp"
android:id="@+id/id_dash"
android:layout_width="4dp"
android:layout_height="match_parent"
app:lineColor="@color/white"
app:orientation="vertical"/>
自定义属性
<declare-styleable name="dashedline">
<attr name="lineColor" format="color"/>
<attr name="orientation">
<enum name="horizontal" value="0"/>
<enum name="vertical" value="1"/>
</attr>
</declare-styleable>