android中Touch相关事件解析

 参考1:http://www.oschina.net/question/163910_27289


1. 默认情况输出结果。

LinearLayout代码

public class Theory1_MyLinearLayout extends LinearLayout {

	public Theory1_MyLinearLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Theory1_MyLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public Theory1_MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_DOWN");
			break;

		case MotionEvent.ACTION_MOVE:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_MOVE");
			break;

		case MotionEvent.ACTION_UP:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_UP");
			break;

		case MotionEvent.ACTION_CANCEL:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_CANCEL");
			break;
		}

		return super.onTouchEvent(event);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		int action = ev.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onInterceptTouchEvent--ACTION_DOWN");
			break;

		case MotionEvent.ACTION_MOVE:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onInterceptTouchEvent--ACTION_MOVE");
			break;

		case MotionEvent.ACTION_UP:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onInterceptTouchEvent--ACTION_UP");
			break;

		case MotionEvent.ACTION_CANCEL:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onInterceptTouchEvent--ACTION_CANCEL");
			break;
		}
		
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		int action = ev.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--dispatchTouchEvent--ACTION_DOWN");
			break;

		case MotionEvent.ACTION_MOVE:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--dispatchTouchEvent--ACTION_MOVE");
			break;
		case MotionEvent.ACTION_UP:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--dispatchTouchEvent--ACTION_UP");
			break;

		case MotionEvent.ACTION_CANCEL:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--dispatchTouchEvent--ACTION_CANCEL");
			break;
		}

		return super.dispatchTouchEvent(ev);
	}
}

再看下MyTextView代码

public class Theory1_MyTextView extends TextView {
	public Theory1_MyTextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public Theory1_MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public Theory1_MyTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.d(Constant.DEBUG_TAG, "MyTextView--onTouchEvent--ACTION_DOWN");
			break;

		case MotionEvent.ACTION_MOVE:
			Log.d(Constant.DEBUG_TAG, "MyTextView--onTouchEvent--ACTION_MOVE");
			break;

		case MotionEvent.ACTION_UP:
			Log.d(Constant.DEBUG_TAG, "MyTextView--onTouchEvent--ACTION_UP");
			break;

		case MotionEvent.ACTION_CANCEL:
			Log.d(Constant.DEBUG_TAG, "MyTextView--onTouchEvent--ACTION_CANCEL");
			break;
		}

		return super.onTouchEvent(event);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		int action = ev.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.d(Constant.DEBUG_TAG, "MyTextView--dispatchTouchEvent--ACTION_DOWN");
			break;

		case MotionEvent.ACTION_MOVE:
			Log.d(Constant.DEBUG_TAG, "MyTextView--dispatchTouchEvent--ACTION_MOVE");
			break;
		case MotionEvent.ACTION_UP:
			Log.d(Constant.DEBUG_TAG, "MyTextView--dispatchTouchEvent--ACTION_UP");
			break;

		case MotionEvent.ACTION_CANCEL:
			Log.d(Constant.DEBUG_TAG, "MyTextView--dispatchTouchEvent--ACTION_CANCEL");
			break;
		}

		return super.dispatchTouchEvent(ev);
	}


}

最后看下布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<hh.theory.Theory1_MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <hh.theory.Theory1_MyTextView
        android:id="@+id/textView1"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="@color/orange"
        android:text="TextView" />

</hh.theory.Theory1_MyLinearLayout>

显示的图片为:


对比:

1. 在黑色部分Touch的输出结果


2. 在橙色部分Touch的输出结果


结论:

默认的情况下是MyLinearLayout的dispatchTouchEvent最先接收到ACTION_DOWN,然后分发给MyTextView(其子View)。

一个问题,这里网速买只有DOWN,而没有MOVE和UP呢?

很简单,请看Theory1_MyLinearLayout.onTouchEvent中的action为ACTION_DOWN的返回值,是super.onTouchEvent(event);

经过本人的测试,发现LinearLayout的super.onTouchEvent(event);返回false;

这是什么意思?难过

注释部分是:* @return True if the event was handled, false otherwise.“

其实就是表示我们的MyLinearLayout没有处理这个ACTION_DOWN,因此后面的MOVE, UP都不接收了。


2. 仅将Theory1_MyLinearLayout.onTouchEvent中的action为ACTION_DOWN的返回值为true的情况

经过修改后的MyLinearLayout.onTouchEvent的代码为:

@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_DOWN");
			
			return true;
			//break;

		case MotionEvent.ACTION_MOVE:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_MOVE");
			break;

		case MotionEvent.ACTION_UP:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_UP");
			break;

		case MotionEvent.ACTION_CANCEL:
			Log.d(Constant.DEBUG_TAG, "MyLinearLayout--onTouchEvent--ACTION_CANCEL");
			break;
		}

		return super.onTouchEvent(event);
	}


2.1 在黑色部分轻扫的结果


可以看到dispatchTouchEvent和onTouchEvent是一一对应关系。。。

2.2 在MyTextView所占的橙色部分轻扫的结果


可见,MyTextView因为没有在Action为DOWN的时候返回true,表明这个MyTextView不想接收MOVE以及UP,所以该MyTextView没有接收到MOVE和UP消息。


为了印证我的结果,我将MyTextView中onTouchEvent的ACTION_DOWN返回true,然后在橙色部分轻扫,下面是结果:


这下真相大白了吧,因为MyTextView也接收了ACTION_DWON,那么此后的MyLinearLayout的onTouchEvent就接收不到MOVE和UP了。



// End up with 2013/5/31 == 18:35

///




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值