---恢复内容开始---

一、MotionEvent : ACTION_DOWN(下按事件)、ACTION_UP(松开事件)、ACTION_MOVE(移动事件)

二、三大函数

1.dispatchTouchEvent(Event event) == > 分发函数,由系统自动调用(返回值为true时,viewGroup可以向其子控件分发事件,view则分发给自身)

public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                Log.e("MyLinearLayout2", "dispatchTouchEvent-->ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("MyLinearLayout2","dispatchTouchEvent-->ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("MyLinearLayout2","dispatchTouchEvent-->ACTION_UP");
                break;
        }
        return super.dispatchTouchEvent(ev);

}

2.onInterceptTouchEvent(Event event) ==> 拦截函数(viewGroup有,view没有)

a.返回值为true时,viewGroup对象会将事件拦截,使得之后的UP和MOVE不会被任何控件相应;

b.返回值为false时,viewGroup不拦截事件,事件可以正常分发。

public boolean onInterceptTouchEvent(MotionEvent ev) {

        int action = ev.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_UP");
                break;
        }
        return false;
    }

3.onTouchEvent(Event event) == >触摸相应函数

a.事件逐级向下传递,直到某个view或viewGroup执行onTouchEvent,若返回值为false,则会通知其父viewGroup执行onTouchEvent,若返回值为true,则不通知其父viewGroup执行onTouchEvent;

b.当某个view或viewGroup的onTouchEvent()相应,并且返回值为true时,之后的MOVE和UP的响应事件都会直接传递给该空间,并由该控件的onTouchEvent()响应;

直接上代码:

MainActivity:

 1 package com.example.qjm3662.myapplication;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.support.design.widget.FloatingActionButton;
 7 import android.support.design.widget.Snackbar;
 8 import android.support.v7.app.AppCompatActivity;
 9 import android.support.v7.widget.Toolbar;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.LinearLayout;
 
 public class MainActivity extends Activity{
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
     }

25 }

MyLInearLayout:

 1 package com.example.qjm3662.myapplication;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.util.Log;
 6 import android.view.MotionEvent;
 7 import android.widget.LinearLayout;
 8 
 9 /**
  * Created by qjm3662 on 2016/4/10 0010.
  */
 public class MyLInearLayout extends LinearLayout {
     public MyLInearLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
     @Override
     public boolean dispatchTouchEvent(MotionEvent ev) {
         int action = ev.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("MyLInearLayout","dispatchTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("MyLInearLayout","dispatchTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("MyLInearLayout","dispatchTouchEvent-->ACTION_UP");
                 break;
         }
         return super.dispatchTouchEvent(ev);
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         int action = event.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("MyLInearLayout","onTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("MyLInearLayout","onTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("MyLInearLayout","onTouchEvent-->ACTION_UP");
                 break;
         }
         return false;
     }
 
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
 
         int action = ev.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_UP");
                 break;
         }
         return false;
     }
 

69 }

MyLinearLayout2:

 1 package com.example.qjm3662.myapplication;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.util.Log;
 6 import android.view.MotionEvent;
 7 import android.widget.LinearLayout;
 8 
 9 /**
  * Created by qjm3662 on 2016/4/10 0010.
  */
 public class MyLinearLayout2 extends LinearLayout{
     public MyLinearLayout2(Context context) {
         super(context);
     }
 
     public MyLinearLayout2(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
     public MyLinearLayout2(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
     }
 
     @Override
     public boolean dispatchTouchEvent(MotionEvent ev) {
         int action = ev.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("MyLinearLayout2", "dispatchTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("MyLinearLayout2","dispatchTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("MyLinearLayout2","dispatchTouchEvent-->ACTION_UP");
                 break;
         }
         return super.dispatchTouchEvent(ev);
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         int action = event.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("MyLinearLayout2","onTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("MyLinearLayout2","onTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("MyLinearLayout2","onTouchEvent-->ACTION_UP");
                 break;
         }
         return false;
     }
 
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
 
         int action = ev.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("MyLinearLayout2","onInterceptTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("MyLinearLayout2","onInterceptTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("MyLinearLayout2","onInterceptTouchEvent-->ACTION_UP");
                 break;
         }
         return true;
     }

76 }

view:

 1 package com.example.qjm3662.myapplication;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.util.Log;
 6 import android.view.MotionEvent;
 7 import android.widget.TextView;
 8 
 9 /**
  * Created by qjm3662 on 2016/4/10 0010.
  */
 public class view extends TextView{
 
 
     public view(Context context) {
         super(context);
     }
 
     public view(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
     public view(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
     }
 
 
     @Override
     public boolean dispatchTouchEvent(MotionEvent ev) {
         int action = ev.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("view","dispatchTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("view","dispatchTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("view","dispatchTouchEvent-->ACTION_UP");
                 break;
         }
         return super.dispatchTouchEvent(ev);
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         int action = event.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("view","onTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("view","onTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("view","onTouchEvent-->ACTION_UP");
                 break;
         }
         return true;
     }
 
 
 }

view2:

 1 package com.example.qjm3662.myapplication;
 2 
 3 import android.content.Context;
 4 import android.util.AttributeSet;
 5 import android.util.Log;
 6 import android.view.MotionEvent;
 7 import android.widget.ImageView;
 8 
 9 /**
  * Created by qjm3662 on 2016/4/10 0010.
  */
 public class view2 extends ImageView{
     public view2(Context context) {
         super(context);
     }
 
     public view2(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
 
     public view2(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
     }
 
     @Override
     public boolean dispatchTouchEvent(MotionEvent event) {
         int action = event.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("view2", "dispatchTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("view2","dispatchTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("view2","dispatchTouchEvent-->ACTION_UP");
                 break;
         }
         return super.dispatchTouchEvent(event);
     }
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
         int action = event.getAction();
         switch(action){
             case MotionEvent.ACTION_DOWN:
                 Log.e("view2","onTouchEvent-->ACTION_DOWN");
                 break;
             case MotionEvent.ACTION_MOVE:
                 Log.e("view2","onTouchEvent-->ACTION_MOVE");
                 break;
             case MotionEvent.ACTION_UP:
                 Log.e("view2","onTouchEvent-->ACTION_UP");
                 break;
         }
         return false;
     }

58 }

布局:

<?xml version="1.0" encoding="utf-8"?>
<com.example.qjm3662.myapplication.MyLInearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">     <com.example.qjm3662.myapplication.view
        android:text="dsvsdvsvsdv"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <com.example.qjm3662.myapplication.MyLinearLayout2
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:orientation="vertical"
        >
        <com.example.qjm3662.myapplication.view2
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.example.qjm3662.myapplication.MyLinearLayout2>

</com.example.qjm3662.myapplication.MyLInearLayout>

运行结果:

点击上面的TextView后松开:

04-10 06:42:32.234 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: dispatchTouchEvent-->ACTION_DOWN
04-10 06:42:32.234 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: onInterceptTouchEvent-->ACTION_DOWN
04-10 06:42:32.234 2414-2414/com.example.qjm3662.myapplication E/view: dispatchTouchEvent-->ACTION_DOWN
04-10 06:42:32.234 2414-2414/com.example.qjm3662.myapplication E/view: onTouchEvent-->ACTION_DOWN
04-10 06:42:32.474 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: dispatchTouchEvent-->ACTION_UP
04-10 06:42:32.474 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: onInterceptTouchEvent-->ACTION_UP
04-10 06:42:32.474 2414-2414/com.example.qjm3662.myapplication E/view: dispatchTouchEvent-->ACTION_UP
04-10 06:42:32.474 2414-2414/com.example.qjm3662.myapplication E/view: onTouchEvent-->ACTION_UP

点击下面的ImagView后松开:

04-10 06:43:58.322 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: dispatchTouchEvent-->ACTION_DOWN
04-10 06:43:58.322 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: onInterceptTouchEvent-->ACTION_DOWN
04-10 06:43:58.322 2414-2414/com.example.qjm3662.myapplication E/MyLinearLayout2: dispatchTouchEvent-->ACTION_DOWN
04-10 06:43:58.322 2414-2414/com.example.qjm3662.myapplication E/MyLinearLayout2: onInterceptTouchEvent-->ACTION_DOWN
04-10 06:43:58.322 2414-2414/com.example.qjm3662.myapplication E/MyLinearLayout2: onTouchEvent-->ACTION_DOWN

04-10 06:43:58.322 2414-2414/com.example.qjm3662.myapplication E/MyLInearLayout: onTouchEvent-->ACTION_DOWN

Android触摸事件的分发机制的更多相关文章

  1. Android触摸事件的应用

    前言 上一篇讲了Android触摸事件的传递机制,具体可以看这里 初识Android触摸事件传递机制.既然知道Android中触摸事件的传递分发,那么它能解决什么样的问题,在我们实际开发中如何应用,这 ...

  2. android自定义控件(9)-Android触摸事件分发机制

    触摸事件的传递机制:   首先是最外层的viewgroup接收到事件,然后调用会调用自己的dispatchTouchEvent方法.如果在ACTION_DOWN的时候dispatchTouchEven ...

  3. 初识Android触摸事件传递机制

    前言 今天总结的一个知识点是Andorid中View事件传递机制,也是核心知识点,相信很多开发者在面对这个问题时候会觉得困惑,另外,View的另外一个难题滑动冲突,比如在ScrollView中嵌套Li ...

  4. 一个demo让你彻底理解Android中触摸事件的分发

    注:本文涉及的demo的地址:https://github.com/absfree/TouchDispatch 1. 触摸动作及事件序列 (1)触摸事件的动作 触摸动作一共有三种:ACTION_DOW ...

  5. Android触摸事件传递机制

    简单梳理一下Android触摸事件传递机制的知识点. 一.View与ViewGroup的关系 View和ViewGroup二者的继承关系如下图所示: View是Android中最基本的一种UI组件,它 ...

  6. 【知识梳理1】Android触摸事件机制

    前言 随着科学技术的发展,智能手机早已成为我们当代人身边不可缺少的"伙伴"之中的一个,堪比对象女友.每天我们对着手机反复的做着点击.滑动操作,而手机则随着我们的操作给我们展示她的精 ...

  7. 一个demo让你彻底理解Android触摸事件的并发

    注:本文涉及的demo的地址:https://github.com/absfree/TouchDispatch 1. 触摸动作及事件序列 (1)触摸事件的动作 触摸动作一共有三种:ACTION_DOW ...

  8. View,ViewGroup的Touch事件的分发机制

    原帖地址:http://blog.csdn.net/xiaanming/article/details/21696315 ViewGroup的事件分发机制 我们用手指去触摸Android手机屏幕,就会 ...

  9. Andriod 从源码的角度详解View,ViewGroup的Touch事件的分发机制

    转自:xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/21696315) 今天这篇文章主要分析的是Android的事件分发机制, ...

随机推荐

  1. c语言实现一个链表

    一.基础研究 我们在这里要理解和实现一种最基本的数据结构:链表.首先看看实现的程序代码: List .h: 事实上我们观察list.h发现前面一部分是数据结构的定义和函数的声明,后面一部分是函数的实现 ...

  2. 给 chorme Developer Tool F12 开发者工具.加入更酷的代码着色

    地址:https://github.com/mauricecruz/chrome-devtools-zerodarkmatrix-theme 默认样式 替换目录为: mac ~/Library/App ...

  3. Android 4.0 新增的显示数据集的桌面控件

    setRemoteAdapter (int viewId, Intent intent):该方法可以使用 Intent 更新 RemoteViews 中viewId 对应的组件. 上面方法的 Inte ...

  4. h.264 mvp求解过程

    h.264标准中由于分为宏块分割块(8x8),子宏块分割块(4x4),所以各种各样的求解过程比较繁琐 下面整理出标准中mvp的求解过程 8.4.1.3 已知条件有当前块的属性:位置.块类型需要得到当前 ...

  5. [flex & bison]编译器杂谈

    flex与bison是编译器设计工具.这里的编译器为广义,其中包括一般的编译器.脚本解析器等,需要进行语言结构解析来得出意义的程序. 当我们需要用一个语言来设计一款编译器时,需要考虑太多设计重心外的东 ...

  6. Delphi GDI或图像处理的博客

    http://blog.csdn.net/w1028489713/article/category/1918251

  7. 驱动里执行应用层代码之KeUserModeCallBack(WOW64是由三个动态库wow64.dll wow64win.dll wow64cpu.dll来实现)

    在驱动层(ring0)里执行应用层(ring3)代码,这是个老生常谈的技术,而且方法也挺多. 这种技术的本质:其实就是想方设法在驱动层里把应用层代码弄到应用层去执行. 比如在APC异步调用中,KeIn ...

  8. HDOJ(HDU) 1859 最小长方形(水题、、)

    Problem Description 给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内.长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内 ...

  9. MySQL如何利用索引优化ORDER BY排序语句 【转载】

    本文转载自:http://blog.csdn.net/ryb7899/article/details/5580624  .感谢相关作者. MySQL索引通常是被用于提高WHERE条件的数据行匹配或者执 ...

  10. [置顶] Putty管理私钥文件

    openssh中,ssh_keygen产生的私钥,id_rsa这种密钥putty是不认识的,必须先把它转换成ppk格式, Windows上如果你安装了git,它里面bin目录下就有ssh_keygen ...