PathEffect有6个子类,想看更多内容,详见:
http://blog.csdn.net/aigestudio/article/details/41447349
之前为了自定义绘制虚线,研究了一下DashPathEffect,详见
http://blog.csdn.net/u014620028/article/details/73380396
为了修改自定义虚线的样式,今天学习了一下PathDashPathEffect。这个可以和之前的自定义虚线结合,扩展虚线样式。
效果图:
PathEffectView
package com.chen.demo2;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.PathDashPathEffect;
import android.util.AttributeSet;
import android.view.View;
public class PathEffectView extends View {
// 画笔对象
//线段类型虚线
private Paint mPaint;
private Paint line_paint;
// 路径对象
private Path mPath_1;
private Path mPath_2;
private Path mPath_3;
//默认类型虚线
private DashPathEffect dashPathEffect;
//圆点类型虚线
private PathDashPathEffect pathDashPathEffect_1;
//矩形类型虚线
private PathDashPathEffect pathDashPathEffect_2;
public PathEffectView(Context context) {
this(context, null);
}
public PathEffectView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public PathEffectView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
/*
* 实例化画笔并设置属性,并打开抗锯齿
*/
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(Color.BLACK);
line_paint = new Paint();
line_paint.setColor(Color.RED);
line_paint.setStyle(Paint.Style.STROKE);
line_paint.setStrokeWidth(1);
//设置虚线路径起始点
mPath_1 = new Path();
//100,100为起点
mPath_1.moveTo(100, 100);
//400,100作为终点
mPath_1.lineTo(800, 100);
//设置虚线路径起始点
mPath_2 = new Path();
mPath_2.moveTo(100, 300);
mPath_2.lineTo(800, 300);
//设置虚线路径起始点
mPath_3 = new Path();
mPath_3.moveTo(100, 500);
mPath_3.lineTo(800, 500);
//普通虚线
dashPathEffect = new DashPathEffect(new float[]{20, 10, 50, 5, 100, 30, 10, 5}, 0);
//圆点类型虚线相关
Path path_1 = new Path();
//direction:方向
//CW:顺时针
//CCW:逆时针
path_1.addCircle(0, 0, 10, Direction.CCW);
/**
* Dash破碎 the drawn绘图 path by stamping冲压、冲击 it with the specified指定 shape. This only
* applies适用 to drawings when the paint's style is STROKE or STROKE_AND_FILL.
* If the paint's style is FILL, then this effect is ignored忽略. The paint's
* strokeWidth does not affect影响 the results.
* @param shape The path to stamp标记 along沿着
* @param advance spacing between each stamp of shape
* @param phase amount to offset before the first shape is stamped
* @param style how to transform改变、变换 the shape at each position as it is stamped
*
* 最后一个参数:
* TRANSLATE:translate the shape to each position
* ROTATE:rotate the shape about its center
* MORPH:transform each point, and turn lines into curves
*/
pathDashPathEffect_1 = new PathDashPathEffect(path_1, 50, 0, PathDashPathEffect.Style.ROTATE);
//矩形类型虚线相关
Path path_2 = new Path();
path_2.addRect(0, 0, 10, 10, Direction.CCW);
pathDashPathEffect_2 = new PathDashPathEffect(path_2, 50, 0, PathDashPathEffect.Style.ROTATE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setPathEffect(dashPathEffect);
canvas.drawPath(mPath_1, mPaint);
mPaint.setPathEffect(pathDashPathEffect_1);
canvas.drawPath(mPath_2, mPaint);
mPaint.setPathEffect(pathDashPathEffect_2);
canvas.drawPath(mPath_3, mPaint);
}
}