鸿蒙OS开发文档 鸿蒙OS VelocityDetector

2024-02-25 开发教程 鸿蒙OS开发文档 匿名 3

VelocityDetector

java.lang.Object
|---ohos.agp.components.VelocityDetector

public final class VelocityDetector
extends Object

用于检测触摸事件速度的助手,用于实现投掷和其他类似手势。

Since:

3

嵌套类摘要

修饰符和类型描述
protected static classVelocityDetector.VelocityDetectorCleaner释放原生 Style 对象的回调。

方法总结

修饰符和类型方法描述
voidaddEvent(TouchEvent event)将用户的移动添加到检测器。
voidcalculateCurrentVelocity(int units)根据已收集的点计算当前速度。
voidcalculateCurrentVelocity(int units, float maxVxVelocity, float maxVyVelocity)根据已收集的点计算当前速度,并指定计算的 X 和 Y 速度的上限。
voidclear()将 VelocityDetector 实例重置为初始状态。
floatgetHorizontalVelocity()获取最近计算的 X 速度。
float[]getVelocity()获取最近计算的速度。
floatgetVerticalVelocity()获取最近计算的 Y 速度。
static VelocityDetectorobtainInstance()获取一个新的 VelocityDetector 实例来检测运动的速度。
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

方法详情

obtainInstance

public static VelocityDetector obtainInstance()

获取一个新的 VelocityDetector 实例来检测运动的速度。

返回:

返回 VelocityDetector 实例。

Since:

4

clear

public void clear()

将 VelocityDetector 实例重置为初始状态。

addEvent

public void addEvent(TouchEvent event)

将用户的移动添加到检测器。

您可以调用此方法来检测任何运动。

参数:

参数名称参数描述
event表示您要检测的事件,它是一个 TouchEvent 实例。

Since:

4

calculateCurrentVelocity

public void calculateCurrentVelocity(int units)

根据已收集的点计算当前速度。

计算完成后,您可以使用 getHorizontalVelocity() 和 getVerticalVelocity() 来检索速度结果。

参数:

参数名称参数描述
units指示速度结果的单位。 值 1 表示每秒像素数,1000 表示每秒像素数,依此类推。

calculateCurrentVelocity

public void calculateCurrentVelocity(int units, float maxVxVelocity, float maxVyVelocity)

根据已收集的点计算当前速度,并指定计算的 X 和 Y 速度的上限。

计算完成后,您可以使用 getHorizontalVelocity() 和 getVerticalVelocity() 来检索速度结果。

参数:

参数名称参数描述
units表示速度结果。 值 1 表示每秒像素数,1000 表示每秒像素数,依此类推。
maxVxVelocity指示最大 X 速度。
maxVyVelocity指示最大 Y 速度。

getHorizontalVelocity

public float getHorizontalVelocity()

获取最近计算的 X 速度。

在调用此函数之前,您必须先调用 calculateCurrentVelocity(int)。

返回:

返回最近计算的 X 速度。

Since:

4

getVerticalVelocity

public float getVerticalVelocity()

获取最近计算的 Y 速度。

在调用此函数之前,您必须调用 calculateCurrentVelocity(int)。

返回:

返回最近计算的 Y 速度。

Since:

4

getVelocity

public float[] getVelocity()

获取最近计算的速度。

在调用此方法之前,您必须调用 calculateCurrentVelocity(int)。

返回:

返回最近计算的速度。

Since:

4