前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android UI】Path 测量 PathMeasure ① ( PathMeasure API 简介 | 无参构造函数 | 带参构造函数 | Path 闭合设置 | getLength 函数)

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

作者头像
韩曙亮
发布2023-03-30 15:58:52
3560
发布2023-03-30 15:58:52
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、PathMeasure API 简介


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

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

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

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

1、无参构造函数

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

代码语言:javascript
复制
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 的 开始 和 结尾 强制闭合 ;

代码语言:javascript
复制
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。

代码语言:javascript
复制
open fun getLength(): Float
Return the total length of the current contour, or 0 if no path is associated with this measure object.

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、PathMeasure API 简介
    • 1、无参构造函数
      • 2、带参构造函数
        • 3、getLength 函数
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档