Android onTouchEvent和onInterceptTouchEvent事件分发详解(一)

转载请标明出处   Android源码详解目录

在讲解之前,先看一下demo

package com.example.androiddemo;

import com.example.androiddemo.dispatch.LinearLayoutChild;
import com.example.androiddemo.dispatch.LinearLayoutParent;
import com.example.androiddemo.dispatch.TextView_Child;
import com.example.androiddemo.utils.Util;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

public class DispatchActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(initView());
	}

	private View initView() {
		TextView_Child mTextView_Child = new TextView_Child(this);
		ViewGroup.LayoutParams params0 = new LayoutParams(getWindowManager()
				.getDefaultDisplay().getWidth() / 2, getWindowManager()
				.getDefaultDisplay().getHeight() / 4);
		mTextView_Child.setLayoutParams(params0);
		mTextView_Child.setBackgroundColor(getResources().getColor(
				android.R.color.holo_orange_dark));

		LinearLayoutChild mLinearLayoutChild = new LinearLayoutChild(this);
		mLinearLayoutChild.addView(mTextView_Child);
		mLinearLayoutChild.setBackgroundColor(getResources().getColor(
				android.R.color.holo_red_dark));
		ViewGroup.LayoutParams params1 = new LayoutParams(
				LayoutParams.WRAP_CONTENT, getWindowManager()
						.getDefaultDisplay().getHeight() / 2);
		mLinearLayoutChild.setLayoutParams(params1);
		mLinearLayoutChild.setGravity(Gravity.CENTER);

		LinearLayoutParent mLinearLayoutParent = new LinearLayoutParent(this);
		mLinearLayoutParent.addView(mLinearLayoutChild);
		mLinearLayoutParent.setBackgroundColor(getResources().getColor(
				android.R.color.holo_green_dark));
		mLinearLayoutParent.setGravity(Gravity.CENTER);
		return mLinearLayoutParent;
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		boolean touch = super.dispatchTouchEvent(ev);
		Log.d("wld", "DispatchActivity_dispatchTouchEvent" + "_" + touch);
		Util.printEventLog("DispatchActivity", "dispatchTouchEvent", ev);
		return touch;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean touch = super.onTouchEvent(event);
		Log.d("wld", "DispatchActivity_onTouchEvent" + "_" + touch);
		Util.printEventLog("DispatchActivity", "onTouchEvent", event);
		return touch;
	}
}
package com.example.androiddemo.utils;

import android.util.Log;
import android.view.MotionEvent;

public class Util {

	public static void printEventLog(String className, String methodName,
			MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.d("wld", className + "_" + methodName + "_" + "ACTION_DOWN");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.d("wld", className + "_" + methodName + "_" + "ACTION_MOVE");
			break;
		case MotionEvent.ACTION_UP:
			Log.d("wld", className + "_" + methodName + "_" + "ACTION_UP");
			break;
		case MotionEvent.ACTION_CANCEL:
			Log.d("wld", className + "_" + methodName + "_" + "ACTION_CANCEL");
			break;
		default:
			break;
		}
	}
}
package com.example.androiddemo.dispatch;

import com.example.androiddemo.utils.Util;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class LinearLayoutParent extends LinearLayout {

	public LinearLayoutParent(Context context) {
		super(context);
	}

	public LinearLayoutParent(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public LinearLayoutParent(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		boolean touch = super.dispatchTouchEvent(ev);
		Log.d("wld", "LinearLayoutParent_dispatchTouchEvent" + "_" + touch);
		Util.printEventLog("LinearLayoutParent", "dispatchTouchEvent", ev);
		return touch;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean touch = super.onTouchEvent(event);
		Log.d("wld", "LinearLayoutParent_onTouchEvent" + "_" + touch);
		Util.printEventLog("LinearLayoutParent", "onTouchEvent", event);
		return touch;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		boolean touch = super.onInterceptTouchEvent(ev);
		Log.d("wld", "LinearLayoutParent_onInterceptTouchEvent" + "_" + touch);
		Util.printEventLog("LinearLayoutParent", "onInterceptTouchEvent", ev);
		return touch;
	}

}
package com.example.androiddemo.dispatch;

import com.example.androiddemo.utils.Util;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class LinearLayoutChild extends LinearLayout {

	public LinearLayoutChild(Context context) {
		super(context);
	}

	public LinearLayoutChild(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public LinearLayoutChild(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		boolean touch = super.dispatchTouchEvent(ev);
		Log.d("wld", "LinearLayoutChild_dispatchTouchEvent" + "_" + touch);
		Util.printEventLog("LinearLayoutChild", "dispatchTouchEvent", ev);
		return touch;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean touch = super.onTouchEvent(event);
		Log.d("wld", "LinearLayoutChild_onTouchEvent" + "_" + touch);
		Util.printEventLog("LinearLayoutChild", "onTouchEvent", event);
		return touch;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		boolean touch = super.onInterceptTouchEvent(ev);
		Log.d("wld", "LinearLayoutChild_onInterceptTouchEvent" + "_" + touch);
		Util.printEventLog("LinearLayoutChild", "onInterceptTouchEvent", ev);
		return touch;
	}
}
package com.example.androiddemo.dispatch;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.androiddemo.utils.Util;

public class TextView_Child extends TextView {

	public TextView_Child(Context context) {
		super(context);
	}

	public TextView_Child(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TextView_Child(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		boolean touch = super.dispatchTouchEvent(ev);
		Log.d("wld", "TextViewChild_dispatchTouchEvent" + "_" + touch);
		Util.printEventLog("TextViewChild", "dispatchTouchEvent", ev);
		return touch;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean touch = super.onTouchEvent(event);
		Log.d("wld", "TextViewChild_onTouchEvent" + "_" + touch);
		Util.printEventLog("TextViewChild", "onTouchEvent", event);
		return touch;
	}

}
上面所有的返回值都是默认的,我们来看一下运行结果



我们点击中间的橘色,再来看一下打印的log



我们看到,默认情况下,全部返回false,并且DispatchActivity的onTouchEvent方法执行了DOWN,MOVE,UP方法,也就说是默认情况下,事件全部被DispatchActivity的onTouchEvent方法给处理了。上面仅仅是一个演示,其实用处并不是很大,只是作为一个了解,但对我们研究Android的事件分发机制有一定的意义,因为我们如果拦截或者处理事件机制的时候一般是在View及其子类中处理。接下来我们会把DispatchActivity中的两个方法dispatchTouchEvent和onTouchEvent注释掉,我们只研究View及其子类中的Event方法。我们知道除了Button及其ImageButton的onTouchEvent方法默认返回true以外,其他的都是返回false,因为Button和ImageButton默认情况下是消耗事件的,刚才上面我们看到TextView的onTouchEvent方法默认情况下返回的是false,就是事件没有被他消耗掉,下面我们把TextView换成Button在看一看,

public class TextView_Child extends Button {
顺便注释掉DispatchActivity中的dispatchTouchEvent和onTouchEvent方法,我们看一下log





我们可以看到默认情况下Button的onTouchEvent返回的是true,因为点击的时候事件默认被他消耗掉了。好了,这里只是给大家简单的开个头,接下来就会为大家讲解一下View事件的拦截及分发机制,最后在给大家分析源码。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据结构和算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值