安卓触摸事件探究
参考,http://blog.csdn.net/chunqiuwei/article/details/41084921
/** * 第一步,dispatchTouchEvent, * false:结束 * true:到onInterceptTouchEvent * 第二步:到onInterceptTouchEvent, * true:到viewGroup的onTouchEvent * false:到View dispatchTouchEvent * 第三步:View dispatchTouchEvent * 第四步:View onTouchEvent * true:view消费touch事件,viewGroup不能消费 * false:viewGroup决定是否消费touch事件 * * * @param ev * @return */
例子原型
MyLinearLayout包含一个MyView
情况一,
MyLinearLayout,
dispatchTouchEvent返回false
onInterceptTouchEvent返回false
onTouchEvent返回false
MyView
dispatchTouchEvent返回false
onTouchEvent返回false
结果
03-18 15:26:00.726 6884-6884/com.example.myapp D/zyr﹕ A DIS ACTION_DOWN 03-18 15:26:00.726 6884-6884/com.example.myapp D/zyr﹕ A INT ACTION_DOWN 03-18 15:26:00.726 6884-6884/com.example.myapp E/zyr﹕ B DIS ACTION_DOWN 03-18 15:26:00.726 6884-6884/com.example.myapp E/zyr﹕ B TOU ACTION_DOWN 03-18 15:26:00.726 6884-6884/com.example.myapp D/zyr﹕ A TOU ACTION_DOWN 03-18 15:26:05.396 6884-6884/com.example.myapp D/zyr﹕ A DIS ACTION_DOWN 03-18 15:26:05.396 6884-6884/com.example.myapp D/zyr﹕ A INT ACTION_DOWN 03-18 15:26:05.396 6884-6884/com.example.myapp E/zyr﹕ B DIS ACTION_DOWN 03-18 15:26:05.396 6884-6884/com.example.myapp E/zyr﹕ B TOU ACTION_DOWN 03-18 15:26:05.396 6884-6884/com.example.myapp D/zyr﹕ A TOU ACTION_DOWN
结论
由图,没有view消费本次触摸事件,Action_Move事件根本传递不进来
情况二,
MyLinearLayout,
dispatchTouchEvent返回false
onInterceptTouchEvent返回false
onTouchEvent返回false
MyView
dispatchTouchEvent返回false
onTouchEvent返回true
结果
03-18 15:36:33.641 10292-10292/com.example.myapp D/zyr﹕ A DIS ACTION_DOWN 03-18 15:36:33.641 10292-10292/com.example.myapp D/zyr﹕ A INT ACTION_DOWN 03-18 15:36:33.641 10292-10292/com.example.myapp E/zyr﹕ B DIS ACTION_DOWN 03-18 15:36:33.641 10292-10292/com.example.myapp E/zyr﹕ B TOU ACTION_DOWN 03-18 15:36:33.676 10292-10292/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:36:33.676 10292-10292/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:36:33.676 10292-10292/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:36:33.676 10292-10292/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:36:33.691 10292-10292/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:36:33.691 10292-10292/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:36:33.691 10292-10292/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:36:33.691 10292-10292/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:36:33.711 10292-10292/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:36:33.711 10292-10292/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:36:33.711 10292-10292/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:36:33.711 10292-10292/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:36:33.721 10292-10292/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:36:33.721 10292-10292/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:36:33.721 10292-10292/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:36:33.721 10292-10292/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:36:33.721 10292-10292/com.example.myapp D/zyr﹕ A DIS ACTION_UP 03-18 15:36:33.721 10292-10292/com.example.myapp D/zyr﹕ A INT ACTION_UP 03-18 15:36:33.721 10292-10292/com.example.myapp E/zyr﹕ B DIS ACTION_UP 03-18 15:36:33.721 10292-10292/com.example.myapp E/zyr﹕ B TOU ACTION_UP
结论
MyView消费ACTION_DOWN,之后ACTION_MOVE传递进来,消费ACTION_MOVE,ACTION_UP同理
情况三,
MyLinearLayout,
dispatchTouchEvent返回false
onInterceptTouchEvent返回false
onTouchEvent返回true
MyView
dispatchTouchEvent返回false
onTouchEvent返回true
结果
03-18 15:40:18.056 29163-29163/com.example.myapp D/zyr﹕ A DIS ACTION_DOWN 03-18 15:40:18.056 29163-29163/com.example.myapp D/zyr﹕ A INT ACTION_DOWN 03-18 15:40:18.056 29163-29163/com.example.myapp E/zyr﹕ B DIS ACTION_DOWN 03-18 15:40:18.056 29163-29163/com.example.myapp E/zyr﹕ B TOU ACTION_DOWN 03-18 15:40:18.076 29163-29163/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:40:18.076 29163-29163/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:40:18.076 29163-29163/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:40:18.076 29163-29163/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:40:18.096 29163-29163/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:40:18.096 29163-29163/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:40:18.096 29163-29163/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:40:18.096 29163-29163/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:40:18.111 29163-29163/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:40:18.111 29163-29163/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:40:18.111 29163-29163/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:40:18.111 29163-29163/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:40:18.126 29163-29163/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 15:40:18.126 29163-29163/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 15:40:18.126 29163-29163/com.example.myapp E/zyr﹕ B DIS ACTION_MOVE 03-18 15:40:18.126 29163-29163/com.example.myapp E/zyr﹕ B TOU ACTION_MOVE 03-18 15:40:18.131 29163-29163/com.example.myapp D/zyr﹕ A DIS ACTION_UP 03-18 15:40:18.131 29163-29163/com.example.myapp D/zyr﹕ A INT ACTION_UP 03-18 15:40:18.131 29163-29163/com.example.myapp E/zyr﹕ B DIS ACTION_UP 03-18 15:40:18.131 29163-29163/com.example.myapp E/zyr﹕ B TOU ACTION_UP
结论
虽然MyLinearLayout的onTouch返回true,但是MyTouch也是返回true,由于安卓触摸事件传递是由上而下,但是消费触摸事件是由下而上的,所以处于下面的MyView消费掉了触摸事件,MyLinearLayout无法消费
情况四,
研究onInterceptTouchEvent
MyLinearLayout,
dispatchTouchEvent返回false
onInterceptTouchEvent返回true
onTouchEvent返回false
MyView
dispatchTouchEvent返回false
onTouchEvent返回true
结果
03-18 15:44:37.701 20477-20477/com.example.myapp D/zyr﹕ A DIS ACTION_DOWN 03-18 15:44:37.701 20477-20477/com.example.myapp D/zyr﹕ A INT ACTION_DOWN 03-18 15:44:37.701 20477-20477/com.example.myapp D/zyr﹕ A TOU ACTION_DOWN
结论
虽然MyView的onTouch返回true,但是被MyLinearLayout拦截下来了(MyLinearLayout的onInterceptTouchEvent返回true),但是MyLinearLayout的onTouch返回false,即它并没有消费此次触摸事件,so,,,此次触摸事件就此结束,当然Action_Move是没有机会传递拉.
情况五,
MyLinearLayout, dispatchTouchEvent返回false onInterceptTouchEvent ACTION_DOWN,false ACTION_MOVE,true ACTION_UP,false onTouchEvent ACTION_DOWN,false ACTION_MOVE,true ACTION_UP,false MyView dispatchTouchEvent返回false onTouchEvent返回true
结果
03-18 16:09:36.241 8257-8257/com.example.myapp D/zyr﹕ A DIS ACTION_DOWN 03-18 16:09:36.241 8257-8257/com.example.myapp D/zyr﹕ A INT ACTION_DOWN 03-18 16:09:36.241 8257-8257/com.example.myapp E/zyr﹕ B DIS ACTION_DOWN 03-18 16:09:36.241 8257-8257/com.example.myapp E/zyr﹕ B TOU ACTION_DOWN 03-18 16:09:36.271 8257-8257/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 16:09:36.271 8257-8257/com.example.myapp D/zyr﹕ A INT ACTION_MOVE 03-18 16:09:36.296 8257-8257/com.example.myapp D/zyr﹕ A DIS ACTION_MOVE 03-18 16:09:36.296 8257-8257/com.example.myapp D/zyr﹕ A TOU ACTION_MOVE 03-18 16:09:36.296 8257-8257/com.example.myapp D/zyr﹕ A DIS ACTION_UP 03-18 16:09:36.296 8257-8257/com.example.myapp D/zyr﹕ A TOU ACTION_UP
结论
试着自己分析把.