概述:

在上回,从原理上讲解了android中触摸机制的运行模式,那么这次我们通过代码来验证一下这个结论。是驴子是马拿出来遛一遛吧。另外,所学知识有限,如果有任何问题欢迎拍砖和交流。

补充一下,其实我写的(一)中,存在一些问题,对于三个与触摸机制息息相关的方法,并不是上文说的那么容统,细分而言。只有ViewGroup才完全拥有这三个方法(onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent),而对于View并没有onInterceptTouchEvent,其实理由很简单,我已经是最小单位了,我还需要拦截么?我下面已经木有东东可以接收了~

案例驱动:

MainActivity.class
这里抛出一个问题,有兴趣可以回答一下:activity和View是什么关系呢?是不是感觉两者好像都是界面?还是另有隐情呢?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: System.out.println( "MainActivity---dispatchTouchEvent---ACTION_DOWN" ); break ; case MotionEvent.ACTION_UP: System.out.println( "MainActivity---dispatchTouchEvent---ACTION_UP" ); break ; default : break ; } boolean result = super .dispatchTouchEvent(event); System.out.println( "MainActivity---dispatchTouchEvent---result:" +result); return result; } }

自定义Button,作为最小View单位,是没有onInterceptTouchEvent方法可以复写的。其中只复写了其他两个方法。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class MyButton extends Button { public MyButton(Context context, AttributeSet attrs) { super (context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyButton---onTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyButton---onTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyButton---onTouchEvent---ACTION_UP" ); return true ; } @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyButton---dispatchTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyButton---dispatchTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyButton---dispatchTouchEvent---ACTION_UP" ); boolean result = super .dispatchTouchEvent(event); System.out.println( "MyButton---dispatchTouchEvent---result:" +result); return result; } }

自定义ViewGroup的子类,MyLinearLayout,可以看到它是存在onInterceptTouchEvent方法,对于其中的子View,它可以进行拦截或者不拦截。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 public class MyLinearLayout extends LinearLayout { public MyLinearLayout(Context context, AttributeSet attrs) { super (context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyLinearLayout---onTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyLinearLayout---onTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyLinearLayout---onTouchEvent---ACTION_UP" ); boolean result = super .onTouchEvent(event); System.out.println( "MyLinearLayout---onTouchEvent---result:" +result); return result; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyLinearLayout---onInterceptTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyLinearLayout---onInterceptTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyLinearLayout---onInterceptTouchEvent---ACTION_UP" ); boolean result = super .onInterceptTouchEvent(event); result = true ; System.out.println( "MyLinearLayout---onInterceptTouchEvent---result:" +result); return result; } @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyLinearLayout---dispatchTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyLinearLayout---dispatchTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyLinearLayout---dispatchTouchEvent---ACTION_UP" ); boolean result = super .dispatchTouchEvent(event); System.out.println( "MyLinearLayout---dispatchTouchEvent---result:" +result); return result; } }

自定义ImageView,和自定义Button相同,作为最小View单位,不存在onInterceptTouchEvent

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class MyImageView extends ImageView { public MyImageView(Context context, AttributeSet attrs) { super (context, attrs); } @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyImageView---dispatchTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyImageView---dispatchTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyImageView---dispatchTouchEvent---ACTION_UP" ); boolean result = super .dispatchTouchEvent(event); System.out.println( "MyImageView---dispatchTouchEvent---result:" +result); return result; } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println( "MyImageView---onTouchEvent---ACTION_DOWN" ); if (action == MotionEvent.ACTION_MOVE) System.out.println( "MyImageView---onTouchEvent---ACTION_MOVE" ); if (action == MotionEvent.ACTION_UP) System.out.println( "MyImageView---onTouchEvent---ACTION_UP" ); return true ; } }

最后看一下布局文件,很简单,这里不赘述了。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context= ".MainActivity" > <TextView android:id= "@+id/title" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerHorizontal= "true" android:layout_marginTop= "10dp" android:textStyle= "bold" android:textColor= "@color/blue" android:text= "@string/touch" /> <com.example.touchevent.MyButton android:id= "@+id/button1" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_below= "@id/title" android:layout_marginTop= "10dp" android:text= "@string/mybutton1" /> <com.example.touchevent.MyLinearLayout android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_below= "@id/button1" android:layout_marginTop= "50dp" android:background= "@color/blue" android:gravity= "center" > <com.example.touchevent.MyRelativeLayout android:layout_width= "200dp" android:layout_height= "200dp" android:gravity= "center" android:background= "@color/dark_gray" > <com.example.touchevent.MyImageView android:layout_width= "100dp" android:layout_height= "100dp" android:scaleType= "fitCenter" android:src= "@drawable/jacky" /> </com.example.touchevent.MyRelativeLayout> </com.example.touchevent.MyLinearLayout> </RelativeLayout>

最终的效果图如下

当我手指按在图中的1号区域(即屏幕空白处),并且稍微移动一下后再抬起,输出结果如下:

1 2 3 4 5 6 7 8 9 10 11 12 03 - 20 21 : 00 : 59.428 : I/System.out( 3429 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 00 : 59.428 : I/System.out( 3429 ): MainActivity---onTouchEvent---ACTION_DOWN 03 - 20 21 : 00 : 59.428 : I/System.out( 3429 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 00 : 59.428 : I/System.out( 3429 ): MainActivity---dispatchTouchEvent---result: false 03 - 20 21 : 00 : 59.438 : I/System.out( 3429 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 00 : 59.438 : I/System.out( 3429 ): MainActivity---onTouchEvent---ACTION_MOVE 03 - 20 21 : 00 : 59.438 : I/System.out( 3429 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 00 : 59.438 : I/System.out( 3429 ): MainActivity---dispatchTouchEvent---result: false 03 - 20 21 : 00 : 59.588 : I/System.out( 3429 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 00 : 59.588 : I/System.out( 3429 ): MainActivity---onTouchEvent---ACTION_UP 03 - 20 21 : 00 : 59.588 : I/System.out( 3429 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 00 : 59.588 : I/System.out( 3429 ): MainActivity---dispatchTouchEvent---result: false

这里你可能有疑问,为什么在执行了dispatchTouchEvent后,没有输出任何信息,而是先执行了onTouchEvent呢?这是因为在dispatchTouchEvent中执行了super.dispatchTouchEvent(event),而在这里面执行了onTouchEvent方法,所以dispatchTouchEvent输出比onTouchEvent输出要慢一步。

当手指在Button上按下并移动后离开的输出结果如下

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---onTouchEvent---ACTION_DOWN 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---onTouchEvent---result: true 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---dispatchTouchEvent---result: true 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MainActivity---dispatchTouchEvent---result: true 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---onTouchEvent---ACTION_MOVE 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---onTouchEvent---result: true 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MyButton---dispatchTouchEvent---result: true 03 - 20 21 : 07 : 29.947 : I/System.out( 5054 ): MainActivity---dispatchTouchEvent---result: true 03 - 20 21 : 07 : 29.957 : I/System.out( 5054 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 07 : 29.957 : I/System.out( 5054 ): MyButton---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 07 : 29.957 : I/System.out( 5054 ): MyButton---onTouchEvent---ACTION_UP 03 - 20 21 : 07 : 29.957 : I/System.out( 5054 ): MyButton---onTouchEvent---result: true 03 - 20 21 : 07 : 29.957 : I/System.out( 5054 ): MyButton---dispatchTouchEvent---result: true 03 - 20 21 : 07 : 29.957 : I/System.out( 5054 ): MainActivity---dispatchTouchEvent---result: true

从上面两个输出结果,可以判断Activity其实是是你进入整个屏幕的第一道关卡,如果说activity在dispatchTouchEvent这里返回false的话,并且不执行super.dispatchTouchEvent(event)的话,那么整个屏幕中其他的所有ViewGroup或者View都将失去任何触摸效果。输入结果如下输出所示,每三个组成一组,因为触摸的区域不同,每次只触发了MainActivity的中的分发方法,但是显然被拦截了。

1 2 3 4 5 6 7 8 9 10 11 12 03 - 20 20 : 48 : 49.709 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 20 : 48 : 49.739 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 20 : 48 : 49.749 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 20 : 48 : 50.300 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 20 : 48 : 50.320 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 20 : 48 : 50.330 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 20 : 48 : 50.820 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 20 : 48 : 50.850 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 20 : 48 : 50.870 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 20 : 48 : 51.741 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 20 : 48 : 51.771 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 20 : 48 : 51.771 : I/System.out( 2335 ): MainActivity---dispatchTouchEvent---ACTION_UP

那么这里首先就有一个疑问产生了,super.dispatchTouchEvent中到底发生了什么事情,那么我们进去看一看源码。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /** * Called to process touch screen events. You can override this to * intercept all touch screen events before they are dispatched to the * window. Be sure to call this implementation for touch screen events * that should be handled normally. * * @param ev The touch screen event. * * @return boolean Return true if this event was consumed. */ public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { onUserInteraction(); } if (getWindow().superDispatchTouchEvent(ev)) { return true ; } return onTouchEvent(ev); }

其中调用了onUserInteraction方法,可以看到

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 /** * Called whenever a key, touch, or trackball event is dispatched to the * activity. Implement this method if you wish to know that the user has * interacted with the device in some way while your activity is running. * This callback and {@link #onUserLeaveHint} are intended to help * activities manage status bar notifications intelligently; specifically, * for helping activities determine the proper time to cancel a notfication. * * All calls to your activity's {@link #onUserLeaveHint} callback will * be accompanied by calls to {@link #onUserInteraction}. This * ensures that your activity will be told of relevant user activity such * as pulling down the notification pane and touching an item there. * * Note that this callback will be invoked for the touch down action * that begins a touch gesture, but may not be invoked for the touch-moved * and touch-up actions that follow. * * @see #onUserLeaveHint() */ public void onUserInteraction() { }

从源码中可以看到,dispatchTouchEvent方法只处理了ACTION_DOWN事件,所有的事件都是以按下为起点的,所以,Android认为当ACTION_DOWN事件没有执行时,后面的事件都是没有意义的,所以这里首先判断ACTION_DOWN事件。如果事件成立,则调用了onUserInteraction方法,该方法可以在Activity中被重写,在事件被分发前会调用该方法。该方法的返回值是void型,其中的方法体式空的,并不会对事件造成任何影响,并且接着会判断getWindow().superDispatchTouchEvent(ev)的执行结果,看看它的源码:

1 2 3 4 5 6 7 /** * Used by custom windows, such as Dialog, to pass the touch screen event * further down the view hierarchy. Application developers should * not need to implement or call this. * */ public abstract boolean superDispatchTouchEvent(MotionEvent event);

首先是个抽象方法,注释中解释到该方法用于自定义的Window,例如自定义Dialog从而传递触摸事件,并且提到开发者不需要去实现或调用该方法,系统会完成,如果我们在MainActivity中将dispatchTouchEvent方法的返回值设为true,那么这里的执行结果就为true,从而不会返回执行onTouchEvent(ev),如果这里返回false,那么最终会返回执行onTouchEvent方法,由此可知,接下来要调用的就是onTouchEvent方法了。

但是这里需要注意一点,如果你不执行super.dispatchTouchEvent()方法,那么无论你选择返回false还是true,该触摸事件都将不会传递下去,一般没有人会这么做的,因为Activity你没有必要不传递下去吧。

这次的点击区域是中间的图片,我已经在他的父容器的InterceptouchEvent方法中设置了返回值为true,因此将会拦截事件,也就是说在向下传递的过程中,传递到他的外层MyLinearLaytout就将停止传递,触发这一层的onTouch事件,然后返回向外传递。输出结果如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MyLinearLayout---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MyLinearLayout---onInterceptTouchEvent---ACTION_DOWN 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MyLinearLayout---onInterceptTouchEvent---result: true 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MyLinearLayout---onTouchEvent---ACTION_DOWN 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MyLinearLayout---onTouchEvent---result: false 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MyLinearLayout---dispatchTouchEvent---result: false 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MainActivity---onTouchEvent---ACTION_DOWN 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 20 : 41.231 : I/System.out( 7788 ): MainActivity---dispatchTouchEvent---result: false 03 - 20 21 : 20 : 41.241 : I/System.out( 7788 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 20 : 41.241 : I/System.out( 7788 ): MainActivity---onTouchEvent---ACTION_MOVE 03 - 20 21 : 20 : 41.251 : I/System.out( 7788 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 20 : 41.251 : I/System.out( 7788 ): MainActivity---dispatchTouchEvent---result: false 03 - 20 21 : 20 : 41.261 : I/System.out( 7788 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 20 : 41.261 : I/System.out( 7788 ): MainActivity---onTouchEvent---ACTION_UP 03 - 20 21 : 20 : 41.261 : I/System.out( 7788 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 20 : 41.261 : I/System.out( 7788 ): MainActivity---dispatchTouchEvent---result: false

如果不进行拦截,输出如下,可以发现,图片可以被触发,并且在MyLinearLayout中的直接子容器MyRelativeLayout也成功触发

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MainActivity---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyLinearLayout---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyLinearLayout---onInterceptTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyLinearLayout---onInterceptTouchEvent---result: false 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyRelativeLayout---onInterceptTouchEvent--> false 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---onTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyRelativeLayout---dispatchTouchEvent--> true 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyRelativeLayout---onInterceptTouchEvent--> false 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---onTouchEvent---ACTION_DOWN 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MyLinearLayout---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.947 : I/System.out( 8805 ): MainActivity---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MainActivity---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyLinearLayout---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyLinearLayout---onInterceptTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyLinearLayout---onInterceptTouchEvent---result: false 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyRelativeLayout---onInterceptTouchEvent--> false 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyImageView---onTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyRelativeLayout---dispatchTouchEvent--> true 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyRelativeLayout---onInterceptTouchEvent--> false 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyImageView---onTouchEvent---ACTION_MOVE 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MyLinearLayout---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.957 : I/System.out( 8805 ): MainActivity---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MainActivity---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyLinearLayout---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyLinearLayout---onInterceptTouchEvent---ACTION_UP 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyLinearLayout---onInterceptTouchEvent---result: false 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyRelativeLayout---onInterceptTouchEvent--> false 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---ACTION_UP 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyImageView---onTouchEvent---ACTION_UP 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyImageView---dispatchTouchEvent---result: true 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyRelativeLayout---dispatchTouchEvent--> true 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyRelativeLayout---onTouchEvent--> false 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MyLinearLayout---dispatchTouchEvent---result: false 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MainActivity---onTouchEvent---ACTION_UP 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MainActivity---onTouchEvent---result: false 03 - 20 21 : 25 : 46.977 : I/System.out( 8805 ): MainActivity---dispatchTouchEvent---result: false

注意上述输出,如果要看的顺利一些,分类看,比如你要看分发的先后触发顺序,就只看有dispatchTouchEvent的输出,不过的确有点乱,下次再整理一下。

总结:

1.Android的事件传递机制,是从最外层到最内曾再到最外层的传递过程,换句话说就是从Activity到ViewGroup最后到View的过程,当被其中某一层拦截的时候,将会在该层触发onTouchEvent事件,并且不会继续向下级传递,而是返回向上传递。
2.事件传递方法包括dispatchTouchEvent、onTouchEvent、onInterceptTouchEvent,其中前两个是View和ViewGroup都有的,最后一个是只有ViewGroup才有的方法。这三个方法的作用分别是负责事件分发、事件处理、事件拦截。

最后说一下Activity和View的关系:

  1. activity相当于控制部分,view相当于显示部分。两者之间是多对多的关系,所有东西必须用view来显示。viewGroup继承自view,实现了ViewManager,ViewParent接口,主要用作layout方面。
  2. Activity中加载相应的view才能显示出画面来,view是具体的画面布局(layout),由wegit控件组成。好比view是jsp实现前台画面,activity是java程序,处理具体业务逻辑
  3. 基本上每个activity都有对应的view,activity用于控制view中的响应,如button的点击事件等可以在activity中实现,但是你得把这个button给用户看到啊,所以就用view现实了~
  4. activity就是一个容器,view只能在这个container里才能正常工作。
  5. Activity主要是控制部分;View主要负责显示界面

对完整工程有兴趣的朋友,欢迎前往本文首发地址 http://jackyonline.org 留言交流,也可在这里留言~~非常感谢!

更多相关文章

  1. 老罗的Android视频教程整理之入门介绍
  2. 你真的懂Android(安卓)Handler吗?(二)
  3. mac下Android(安卓)Studio常用的一些快捷键
  4. Android(安卓)--- App列表之分组ListView
  5. android Activity状态以及生命周期
  6. 老生常谈Android(安卓)HapticFeedback(震动反馈)
  7. Android(安卓)UI事件处理
  8. Android中的WebView的使用
  9. Android中使用SurfaceView的方法

随机推荐

  1. MySQL千万级大数据SQL查询优化知识点总结
  2. MySQL密码正确却无法本地登录-1045
  3. mysql存储过程之错误处理实例详解
  4. 阿里云ESC 安装 MYSQL8.0的教程
  5. MySQL多表查询实例详解【链接查询、子查
  6. MySQL单表查询操作实例详解【语法、约束
  7. 解析MySQL隐式转换问题
  8. mysql慢查询操作实例分析【开启、测试、
  9. mysql存储过程原理与使用方法详解
  10. 教你如何使用MySQL8递归的方法