Android 让人又爱又恨的触摸机制(二)

概述:

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

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

案例驱动:

MainActivity.class
这里抛出一个问题,有兴趣能够回答一下:activity和View是什么关系呢?是否是感受二者好像都是界面?仍是另有隐情呢?git

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方法能够复写的。其中只复写了其余两个方法。windows

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,它能够进行拦截或者不拦截。jsp

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单位,不存在onInterceptTouchEventide

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>

最终的效果图以下
touchthis

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

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输出要慢一步。code

当手指在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  留言交流,也可在这里留言~~很是感谢!

相关文章
相关标签/搜索