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

本文详细解析了Android中GestureDetector.OnGestureListener接口的onScroll方法参数distanceX和distanceY的具体含义,通过实践验证了这两个参数的计算方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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


看到有文章上说onScroll方法中distanceX和distanceY是指:

distanceX,是前后两次callX距离,不是e2e1的水平距离;是前后两次callY距离,不是e2e1的垂直距离”。

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

 

于是去实践了一下:

 

以下是谷歌官方API说明:


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

Added in API level 1

Notified when a scroll occurs with theinitial on down MotionEvent and the current move MotionEvent. The distance in x and y is alsosupplied 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 eventis consumed, else false

 

 

“The distance along the X axis that has been scrolledsince 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值,计算结果带正负号。


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


 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值