首先看一个自定义view实现的实线效果图,
实现很简单,就是用paint与path结合使用就可以做到了,由于代码过于简单,直接贴源码
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hrules.charter.demo.Main2Activity">
<com.hrules.charter.demo.widget.PathDemo
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
PathDemo的源码
/**
* 根据path画不连贯线(虚线 破折号线)
* Created by 黄海 on 2017/4/17.
*/
public class PathDemo extends View {
Paint paint;
Path path;
int width, height;
public PathDemo(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PathDemo(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);//画线条,线条有宽度
paint.setColor(getResources().getColor(R.color.lightBlue500));
paint.setStrokeWidth(4);//线条宽度
// paint.setPathEffect(new DashPathEffect(new float[]{20f, 5f, 10f}, 0));//线的显示效果:破折号格式
path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
width = getMeasuredWidth();
height = getMeasuredHeight();
//画实现加号
path.moveTo(width / 6, height / 2);
path.lineTo(width / 6 * 5, height / 2);
path.moveTo(width / 2, height / 6);
path.lineTo(width / 2, height / 6 * 5);
canvas.drawPath(path, paint);
}
}
下面简单的变换下,给Paint设置PathEffect,
paint.setPathEffect(new DashPathEffect(new float[]{20f,10f}, 0));
效果变成
再变换下,看清楚DashPathEffect的参数(上下两行分别对应左右效果图)
paint.setPathEffect(new DashPathEffect(new float[]{20f,10f,5f}, 0));//线的显示效果:破折号格式
paint.setPathEffect(new DashPathEffect(new float[]{20f,15f,10f,5f}, 0));//线的显示效果:破折号格式
ok,看到这里也许大家都明白了:DashPathEffect的效果就是虚实交替,首先是实,其次是虚,再是实...DashPathEffect的第一个参数是数组,且它的长度必须>=2, 数组的数字就是控制虚实虚实虚实... 长度。
下面看看改变DashPathEffect的第二个参数(上下两行分别对应左右效果图)
paint.setPathEffect(new DashPathEffect(new float[]{20f,10f}, 0));//线的显示效果:破折号格式
paint.setPathEffect(new DashPathEffect(new float[]{20f,10f}, 15));//线的显示效果:破折号格式
可以看出DashPathEffect的第二个参数就是控制第一条线(实线)的偏移量,是向左偏移。下面给出DashPathEffect的源码说明
public class DashPathEffect extends PathEffect {
/**
* The intervals array must contain an even number of entries (>=2), with
* the even indices specifying the "on" intervals, and the odd indices
* specifying the "off" intervals. phase is an offset into the intervals
* array (mod the sum of all of the intervals). The intervals array
* controls the length of the dashes. The paint's strokeWidth controls the
* thickness of the dashes.
* Note: this patheffect only affects drawing with the paint's style is set
* to STROKE or FILL_AND_STROKE. It is ignored if the drawing is done with
* style == FILL.
* @param intervals array of ON and OFF distances
* @param phase offset into the intervals array
*/
public DashPathEffect(float intervals[], float phase) {
if (intervals.length < 2) {
throw new ArrayIndexOutOfBoundsException();
}
native_instance = nativeCreate(intervals, phase);
}
private static native long nativeCreate(float intervals[], float phase);
}
ok,本片到此结束,比较简单请勿吐槽