Skip to content

Commit 003e657

Browse files
committed
Update
1 parent ffe114f commit 003e657

1 file changed

Lines changed: 40 additions & 16 deletions

File tree

‎CustomView/Advance/[8]Path_Play.md‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ startWithMoveTo | 起始点是否使用 moveTo | 用于保证截取
147147
代码:
148148

149149
``` java
150-
canvas.translate(mViewWidth / 2, mViewHeight / 2); // 平移坐标系
150+
canvas.translate(mViewWidth / 2, mViewHeight / 2); // 平移坐标系
151151

152-
Path path = new Path(); // 创建Path并添加了一个矩形
153-
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
152+
Path path = new Path(); // 创建Path并添加了一个矩形
153+
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
154154

155-
Path dst = new Path(); // 创建用于存储截取后内容的 Path
155+
Path dst = new Path(); // 创建用于存储截取后内容的 Path
156156

157-
PathMeasure measure = new PathMeasure(path, false); // 将 Path 与 PathMeasure 关联
157+
PathMeasure measure = new PathMeasure(path, false); // 将 Path 与 PathMeasure 关联
158158

159-
// 截取一部分存入dst中,并使用 moveTo 保持截取得到的 Path 第一个点的位置不变
160-
measure.getSegment(200, 600, dst, true);
159+
// 截取一部分存入dst中,并使用 moveTo 保持截取得到的 Path 第一个点的位置不变
160+
measure.getSegment(200, 600, dst, true);
161161

162-
canvas.drawPath(dst, mDeafultPaint); // 绘制 dst
162+
canvas.drawPath(dst, mDeafultPaint); // 绘制 dst
163163
```
164164

165165
结果如下:
@@ -169,25 +169,49 @@ startWithMoveTo | 起始点是否使用 moveTo | 用于保证截取
169169
从上图可以看到我们成功到将需要到片段截取了出来,然而当 dst 中有内容时会怎样呢?
170170

171171
``` java
172-
canvas.translate(mViewWidth / 2, mViewHeight / 2); // 平移坐标系
172+
canvas.translate(mViewWidth / 2, mViewHeight / 2); // 平移坐标系
173173

174-
Path path = new Path(); // 创建Path并添加了一个矩形
175-
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
174+
Path path = new Path(); // 创建Path并添加了一个矩形
175+
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
176176

177-
Path dst = new Path(); // 创建用于存储截取后内容的 Path
178-
dst.lineTo(-300, -300); // 在 dst 中添加一条线段
177+
Path dst = new Path(); // 创建用于存储截取后内容的 Path
178+
dst.lineTo(-300, -300); // <--- 在 dst 中添加一条线段
179179

180-
PathMeasure measure = new PathMeasure(path, false); // 将 Path 与 PathMeasure 关联
180+
PathMeasure measure = new PathMeasure(path, false); // 将 Path 与 PathMeasure 关联
181181

182-
measure.getSegment(200, 600, dst, true); // 截取一部分 并使用 moveTo 保持截取得到的 Path 第一个点的位置不变
182+
measure.getSegment(200, 600, dst, true); // 截取一部分 并使用 moveTo 保持截取得到的 Path 第一个点的位置不变
183183

184-
canvas.drawPath(dst, mDeafultPaint); // 绘制 Path
184+
canvas.drawPath(dst, mDeafultPaint); // 绘制 Path
185185
```
186186

187+
结果如下:
188+
189+
![](http://ww3.sinaimg.cn/large/005Xtdi2gw1f4cg8rl0wmj308c0et74b.jpg)
190+
187191
从上面的示例可以看到 dst 中的线段保留了下来,可以得到结论:**被截取的 Path 片段会添加到 dst 中,而不是替换 dst 中到内容。**
188192

189193
前面两个例子中 startWithMoveTo 均为 true, 如果设置为false会怎样呢?
190194

195+
``` java
196+
canvas.translate(mViewWidth / 2, mViewHeight / 2); // 平移坐标系
197+
198+
Path path = new Path(); // 创建Path并添加了一个矩形
199+
path.addRect(-200, -200, 200, 200, Path.Direction.CW);
200+
201+
Path dst = new Path(); // 创建用于存储截取后内容的 Path
202+
dst.lineTo(-300, -300); // 在 dst 中添加一条线段
203+
204+
PathMeasure measure = new PathMeasure(path, false); // 将 Path 与 PathMeasure 关联
205+
206+
measure.getSegment(200, 600, dst, false); // <--- 截取一部分 不使用 startMoveTo, 保持 dst 的连续性
207+
208+
canvas.drawPath(dst, mDeafultPaint); // 绘制 Path
209+
```
210+
211+
结果如下:
212+
213+
![](http://ww2.sinaimg.cn/large/005Xtdi2gw1f4cgdgc7etj308c0et3yk.jpg)
214+
191215
从该示例我们又可以得到一条结论:**如果 startWithMoveTo 为 true, 则被截取出来到Path片段保持原状,如果 startWithMoveTo 为 false,则会将截取出来的 Path 片段的起始点移动到 dst 的最后一个点,以保证 dst 的连续性。**
192216

193217
从而我们可以用以下规则来判断 startWithMoveTo 的取值:

0 commit comments

Comments
 (0)