3 Star 44 Fork 10

Ryan Zhu / OhosExtension

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
TouchEventCompact.md 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
朱帆 提交于 2021-07-23 11:05 . update readme

TouchEventCompact

由于在旧版SDK中,通过TouchEvent.getPointerPosition获取的坐标,在MOVE事件中会偏移,造成计算偏差(API5已修复)。并且VelocityDetector计算速度是基于TouchEvent.getPointerPosition函数的,偏移会造成速度计算错误,比如速度过快、过慢、速度正负值错误等。因此需要使用此类纠正MOVE事件中的偏移。

并且TouchEvent.getPointerPosition函数获取的坐标是相对于偏移位置的:

Obtains the x and y coordinates of a pointer index relative to the offset position during touch control or trajectory tracking in an event.

也就是说,如果该控件通过getPointerPosition获取的坐标在做位移,那么下一次获取的坐标会是加上手指的位移与控件的位移,导致异常。并且VelocityDetector计算速度也会异常。

因此,不建议通过getPointerPosition获取坐标,而使用getPointerScreenPosition来获取。但是如果需要通过VelocityDetector来计算速度,就需要通过TouchEventCompact来纠正getPointerPosition的坐标并与getPointerScreenPosition的坐标同步。

注意:事件分发中的DispatchHelper类中默认使用了TouchEventCompact,并同步了getPointerPosition的坐标与getPointerScreenPosition的坐标。

使用

  1. 创建实例,注意不要重复调用构造函数:
    /**
     * 不要反复构造对象
     */
    public TouchEventCompact()
  1. 如果需要使用VelocityDetector来计算速度,需要将getPointerPosition与getPointerScreenPosition同步
touchEventCompact.setAlignToScreenPosition(true);

// 或者使用下面的构造函数:
public TouchEventCompact(boolean alignToScreenPosition)
  1. 调用correct函数:
public void correct(TouchEvent ev)
  1. 如果需要获取getPointerPosition的坐标,在调用correct函数后,并且下一次correct之前,调用:
public float[] getOrigin()
Java
1
https://gitee.com/thoseyears/ohos-extension.git
git@gitee.com:thoseyears/ohos-extension.git
thoseyears
ohos-extension
OhosExtension
master

搜索帮助