【Android UI】Path 测量 PathMeasure ① ( PathMeasure API 简介 | 无参构造函数 | 带参构造函数 | Path 闭合设置 | getLength 函数)





一、PathMeasure API 简介



PathMeasure 官方文档 : https://developer.android.google.cn/reference/kotlin/android/graphics/PathMeasure


PathMeasurePath 的一个 工具类 , 字面意思就是 Path 的测量工具类 ,

为该 PathMeasure 设置一个 Path 对象 , 则可以对 Path 的 路径 , 轨迹 进行测量 , 可以精确的计算出 Path 的运动轨迹 ,

PathMeasure 提供了 7 7 7 个函数用于实现相关功能 ;


1、无参构造函数


PathMeasure 无参构造函数 : 创建一个空的 PathMeasure , 需要手动关联 Path ;

PathMeasure()

Create an empty PathMeasure object. 
To uses this to measure the length of a path, 
and/or to find the position and tangent along it, call setPath. 

Note that once a path is associated with the measure object, 
it is undefined if the path is subsequently modified and the the measure object is used. 
If the path is modified, you must call setPath with the path.

创建空的PathMeasure对象。
要使用它来测量路径的长度,
和/或要找到位置并沿其相切,请调用setPath。

请注意,一旦路径与度量对象关联,
如果随后修改路径并使用度量对象,则未定义该路径。
如果修改了路径,则必须使用该路径调用setPath。

2、带参构造函数


PathMeasure 带参构造函数 : 该构造函数可以指定将 Path 的 开始结尾 强制闭合 ;

PathMeasure(
    path: Path!, 
    forceClosed: Boolean)

Create a PathMeasure object associated with the specified path object (already created and specified). 
The measure object can now return the path's length, 
and the position and tangent of any position along the path. 

Note that once a path is associated with the measure object, 
it is undefined if the path is subsequently modified and the the measure object is used. 
If the path is modified, you must call setPath with the path.

创建与指定路径对象(已创建和指定)关联的PathMeasure对象。
度量对象现在可以返回路径的长度,
以及路径上任何位置的位置和切线。

请注意,一旦路径与度量对象关联,
如果随后修改路径并使用度量对象,则未定义该路径。
如果修改了路径,则必须使用该路径调用setPath。

forceClosed: Boolean 参数设置为 true , 其 Path 的长度肯定要大于等于 forceClosed: Boolean 设置为 false 的情况 ;

在 Path 是闭合曲线的情况下 , forceClosed: Boolean 设置 true 和 false 其长度相等 ;


注意 不管 forceClosed: Boolean 设置 true 还是 false , 都不会对原来的 Path 产生任何影响 , 只是对计算产生影响 ;

forceClosed: Boolean 参数设置为 true , 会先将 Path 进行模拟闭合 , 然后再进行计算 ;

如果 Path 是直线 , 无法闭合 , 则不会进行闭合操作 ;


如果设置一个元素沿着 Path 运动 , 假如设置了 闭合操作 , 则会沿着闭合的线运动 ;


3、getLength 函数


函数原型 : 返回当前 Path 轮廓的总长度,如果没有路径与此度量对象关联,则返回0。

open fun getLength(): Float
Return the total length of the current contour, or 0 if no path is associated with this measure object.

如果是圆 , 则返回圆的周长 ;

如果是矩形 , 则返回矩形的周长 ;

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/125102491