API 参考:PathMeasure
PathMeasure 用于计算路径长度,并获取路径上任意距离处的位置和切线方向。
概述
PathMeasure 对象通过 Path 初始化。它会遍历路径的各个轮廓。如果路径包含多个轮廓,可以使用 nextContour() 切换到下一个轮廓。
构造函数
new PathMeasure():创建一个空的PathMeasure。new PathMeasure(path):使用指定路径初始化。new PathMeasure(path, forceClosed):如果forceClosed为 true,即使路径未闭合也会按闭合路径处理。new PathMeasure(path, forceClosed, resScale):resScale控制测量精度(默认为 1.0)。
方法
状态管理
setPath(path, forceClosed):用新路径重置测量器。nextContour():移动到路径中的下一个轮廓。如果存在则返回true。isClosed():如果当前轮廓是闭合的则返回true。
测量功能
getLength():返回当前轮廓的总长度。getPosition(distance):返回路径上指定距离处的Point。getTangent(distance):返回指定距离处的切线方向(作为Point向量)。getRSXform(distance):返回指定距离处的RSXform。getMatrix(distance, getPosition, getTangent):返回表示该距离处位置和/或切线方向的Matrix33。
路径提取
getSegment(startD, endD, dst, startWithMoveTo):将路径在startD和endD之间的线段提取到提供的PathBuilder中。
示例
java
Path path = Path.makeCircle(100, 100, 50);
PathMeasure measure = new PathMeasure(path);
float length = measure.getLength();
Point pos = measure.getPosition(length / 2); // 获取半程点位置
Point tan = measure.getTangent(length / 2); // 获取该点的切线方向