概念:路径测量,一个用来测量Path,计算Path的工具类
常用api
/**
* @param path 路径
* @param forceClosed 路径是否闭合
*/
public void setPath(Path path, boolean forceClosed)
/**
* 是否闭合
*/
public boolean isClosed()
/**
* 获取path的长度
*/
public float getLength()
/**
* 跳转到下一个路径
*/
public boolean nextContour()
/**
* 截取片段
* @param startD 开始长度位置
* @param stopD 结束长度位置
* @param dst 目标路径
* @param startWithMoveTo 是否从开始位置进行,如果false,则从0,0位置开始
* @return
*/
public boolean getSegment(float startD, float stopD, Path dst, boolean startWithMoveTo)
/**
* 获取指定长度位置坐标及该点切线值
* @param distance 开始位置的长度,下笔点是从pain所画的形状开始,根据paint的方向来确定
* @param pos 指定长度终点那个点 是个x,y的数组点值
* @param tan 切线值
*/
public boolean getPosTan(float distance, float pos[], float tan[])
/**
* 获取指定长度位置矩阵
* @param distance 指定长度
* @param matrix 矩阵
* @param flags 指定应该在矩阵中返回哪些方面 正切,点的坐标
* public static final int POSITION_MATRIX_FLAG = 0x01; // must match flags in SkPathMeasure.h
* public static final int TANGENT_MATRIX_FLAG = 0x02;
*/
public boolean getMatrix(float distance, Matrix matrix, int flags)
- setPath 设置路径
private void forceClosed(Canvas canvas) {
Path path = new Path();
path.lineTo(0, 200);
path.lineTo(200, 200);
path.lineTo(200, 0);
canvas.drawPath(path, defaultPatin);
//如果路径进行闭合,则measure.getLength获得是最后点到起点闭合的长度
//没闭合,则是已画路径的长度
PathMeasure measure = new PathMeasure();
measure.setPath(path, false);
Log.e(TAG, "forceClosed false:" + measure.getLength());
Log.e(TAG, "path isClosed:" + measure.isClosed());
measure.setPath(path, true);
Log.e(TAG, "forceClosed true:" + measure.getLength());
Log.e(TAG, "path isClosed:" + measure.isClosed());
}
image.png2019-11-04 11:36:55.585 9252-9252/com.wx.paint E/PathMeasureView: forceClosed false:600.0
2019-11-04 11:36:55.585 9252-9252/com.wx.paint E/PathMeasureView: path isClosed:false
2019-11-04 11:36:55.585 9252-9252/com.wx.paint E/PathMeasureView: forceClosed true:800.0
2019-11-04 11:36:55.585 9252-9252/com.wx.paint E/PathMeasureView: path isClosed:true
Path path = new Path();
Path path1 = new Path();
Path path2 = new Path();
//Path.Direction.CW 顺时针方向
//画一个小矩形
path1.addRect(-100, -100, 100, 100, Path.Direction.CW);
PathMeasure measure1 = new PathMeasure(path1, false);
Log.e(TAG, "path1 length: " + measure1.getLength());
//画一个大矩形
path2.addRect(-200, -200, 200, 200, Path.Direction.CW);
PathMeasure measure2 = new PathMeasure(path2, false);
Log.e(TAG, "path2 length: " + measure2.getLength());
/**
* 对2个path进行op path1 path2 XOR保留不共同的部分
*/
path.op(path1, path2, Path.Op.XOR);
canvas.drawPath(path, defaultPatin);
PathMeasure measure = new PathMeasure(path, false);
Log.e(TAG, "path length: " + measure.getLength());
// 跳转到下一条路径
measure.nextContour(); // 注释 1
Log.e(TAG, "path length: " + measure.getLength());
float[] pos = new float[2];
float[] tan = new float[2];
/**
* distance 开始位置的长度,下笔点是从pain所画的形状开始,根据paint的方向来确定
* 如果进行了op,则下笔点会改变
*/
measure2.getPosTan(50, pos, tan); // 注释 3
Log.e(TAG, "pos[0]: " + pos[0] + " pos[1]: " + pos[1]);
Log.e(TAG, "tan[0]: " + tan[0] + " tan[1]: " + tan[1]);
canvas.drawLine(pos[0], pos[1], tan[0], tan[1], defaultPatin);
measure.nextContour(); // 注释 2
Log.e(TAG, "path length: " + measure.getLength());
1、nextContour方法会让当前path 获取它里面的下一个path,上段代码中path是由path1,path2进行取不共同部分获得,所获的长度是path2的1600,当执行了注释1,则将路径转化成了800那个path1,再进行注释2的下一个路径,由于没有别的路径了,就转换成0
2、getPosTan方法 开始位置的长度,下笔点是从pain所画的形状开始,根据paint的方向来确定,参数是2个空数组,当执行完成后就会有值
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: path1 length: 800.0
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: path2 length: 1600.0
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: path length: 1600.0
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: path length: 800.0
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: pos[0]: -150.0 pos[1]: -200.0
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: tan[0]: 1.0 tan[1]: 0.0
2019-11-04 12:10:19.199 10016-10016/com.wx.paint E/PathMeasureView: path length: 0.0
- getSegment
Path path = new Path();
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
PathMeasure measure = new PathMeasure(path, false);
Path dst = new Path();
measure.getSegment(100,600,dst,true); //注释1
canvas.drawPath(path,paint);
canvas.drawPath(dst,defaultPatin);
注释1处 截取长度,第4个参数,如果是true,则开始位置第一个参数起作用,也就是从下笔点开始,到目标距离的点,如图1。如果是false,则从0,0位置开始,到截取长度那个点,如图2。
或者说true 保留下笔点的起点位置,false不保留起点位置
image.png
图1
image.png图2
网友评论