关于GestureDetector.OnGestureListener的onScroll参数distance问题

时间:2022-08-28 18:45:12

关于GestureDetector.OnGestureListener类的onScroll方法参数distanceX和distanceY问题

看到有文章上说onScroll方法中distanceX和distanceY是指“distanceX,是前后两次call的X距离,不是e2与e1的水平距离; 是前后两次call的Y距离,不是e2与e1的垂直距离”

然后怎么也没理解出来这两个参数是什么意思

于是去实践了一下:

以下是谷歌官方API说明:


public abstract boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

Added in API level 1

Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent. The distance in x and y is also supplied for convenience.

Parameters

e1

The first down motion event that started the   scrolling.

e2

The move motion event that triggered the current onScroll.

distanceX

The distance along the X axis that has been scrolled   since the last call to onScroll. This is NOT the distance between e1 and e2.

distanceY

The distance along the Y axis that has been scrolled   since the last call to onScroll. This is NOT the distance between e1 and e2.

Returns

true if the event is consumed, else false


“The distance along the X axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.”

这句话按我自己翻译出来的意思是“在上一次(最后一次)调用onScroll方法沿着X轴所滑动的距离。不是e1和e2之间的距离”

我的理解是:distanceX是滑动起点和终点的水平距离,而不是起点和终点的直线距离。

写了个例子在LogCat中输出了一下它们的值:

发现

distanceX的值等于e1的X值减去e2的X值,计算结果带正负号。

distanceY的值等于e1的Y值减去e2的Y值,计算结果带正负号。

关于GestureDetector.OnGestureListener的onScroll参数distance问题

不知道这理解正不正确,如有错误望指正。