Android触摸事件的分发机制

it2022-05-12  44

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

一、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; 10  import android.util.AttributeSet; 11  import android.util.Log; 12  import android.view.MotionEvent; 13  import android.view.View; 14  import android.view.Menu; 15  import android.view.MenuItem; 16  import android.widget.LinearLayout; 17  18  public  class MainActivity  extends Activity{ 19  20     @Override 21      protected  void onCreate(Bundle savedInstanceState) { 22          super.onCreate(savedInstanceState); 23         setContentView(R.layout.activity_main); 24     }

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  /** 10   * Created by qjm3662 on 2016/4/10 0010. 11    */ 12  public  class MyLInearLayout  extends LinearLayout { 13      public MyLInearLayout(Context context, AttributeSet attrs) { 14          super(context, attrs); 15     } 16  17     @Override 18      public  boolean dispatchTouchEvent(MotionEvent ev) { 19          int action = ev.getAction(); 20          switch(action){ 21              case MotionEvent.ACTION_DOWN: 22                 Log.e("MyLInearLayout","dispatchTouchEvent-->ACTION_DOWN"); 23                  break; 24              case MotionEvent.ACTION_MOVE: 25                 Log.e("MyLInearLayout","dispatchTouchEvent-->ACTION_MOVE"); 26                  break; 27              case MotionEvent.ACTION_UP: 28                 Log.e("MyLInearLayout","dispatchTouchEvent-->ACTION_UP"); 29                  break; 30         } 31          return  super.dispatchTouchEvent(ev); 32     } 33  34     @Override 35      public  boolean onTouchEvent(MotionEvent event) { 36          int action = event.getAction(); 37          switch(action){ 38              case MotionEvent.ACTION_DOWN: 39                 Log.e("MyLInearLayout","onTouchEvent-->ACTION_DOWN"); 40                  break; 41              case MotionEvent.ACTION_MOVE: 42                 Log.e("MyLInearLayout","onTouchEvent-->ACTION_MOVE"); 43                  break; 44              case MotionEvent.ACTION_UP: 45                 Log.e("MyLInearLayout","onTouchEvent-->ACTION_UP"); 46                  break; 47         } 48          return  false; 49     } 50  51     @Override 52      public  boolean onInterceptTouchEvent(MotionEvent ev) { 53  54          int action = ev.getAction(); 55          switch(action){ 56              case MotionEvent.ACTION_DOWN: 57                 Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_DOWN"); 58                  break; 59              case MotionEvent.ACTION_MOVE: 60                 Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_MOVE"); 61                  break; 62              case MotionEvent.ACTION_UP: 63                 Log.e("MyLInearLayout","onInterceptTouchEvent-->ACTION_UP"); 64                  break; 65         } 66          return  false; 67     } 68 

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  /** 10   * Created by qjm3662 on 2016/4/10 0010. 11    */ 12  public  class MyLinearLayout2  extends LinearLayout{ 13      public MyLinearLayout2(Context context) { 14          super(context); 15     } 16  17      public MyLinearLayout2(Context context, AttributeSet attrs) { 18          super(context, attrs); 19     } 20  21      public MyLinearLayout2(Context context, AttributeSet attrs,  int defStyleAttr) { 22          super(context, attrs, defStyleAttr); 23     } 24  25     @Override 26      public  boolean dispatchTouchEvent(MotionEvent ev) { 27          int action = ev.getAction(); 28          switch(action){ 29              case MotionEvent.ACTION_DOWN: 30                 Log.e("MyLinearLayout2", "dispatchTouchEvent-->ACTION_DOWN"); 31                  break; 32              case MotionEvent.ACTION_MOVE: 33                 Log.e("MyLinearLayout2","dispatchTouchEvent-->ACTION_MOVE"); 34                  break; 35              case MotionEvent.ACTION_UP: 36                 Log.e("MyLinearLayout2","dispatchTouchEvent-->ACTION_UP"); 37                  break; 38         } 39          return  super.dispatchTouchEvent(ev); 40     } 41  42     @Override 43      public  boolean onTouchEvent(MotionEvent event) { 44          int action = event.getAction(); 45          switch(action){ 46              case MotionEvent.ACTION_DOWN: 47                 Log.e("MyLinearLayout2","onTouchEvent-->ACTION_DOWN"); 48                  break; 49              case MotionEvent.ACTION_MOVE: 50                 Log.e("MyLinearLayout2","onTouchEvent-->ACTION_MOVE"); 51                  break; 52              case MotionEvent.ACTION_UP: 53                 Log.e("MyLinearLayout2","onTouchEvent-->ACTION_UP"); 54                  break; 55         } 56          return  false; 57     } 58  59     @Override 60      public  boolean onInterceptTouchEvent(MotionEvent ev) { 61  62          int action = ev.getAction(); 63          switch(action){ 64              case MotionEvent.ACTION_DOWN: 65                 Log.e("MyLinearLayout2","onInterceptTouchEvent-->ACTION_DOWN"); 66                  break; 67              case MotionEvent.ACTION_MOVE: 68                 Log.e("MyLinearLayout2","onInterceptTouchEvent-->ACTION_MOVE"); 69                  break; 70              case MotionEvent.ACTION_UP: 71                 Log.e("MyLinearLayout2","onInterceptTouchEvent-->ACTION_UP"); 72                  break; 73         } 74          return  true; 75     }

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  /** 10   * Created by qjm3662 on 2016/4/10 0010. 11    */ 12  public  class view  extends TextView{ 13  14  15      public view(Context context) { 16          super(context); 17     } 18  19      public view(Context context, AttributeSet attrs) { 20          super(context, attrs); 21     } 22  23      public view(Context context, AttributeSet attrs,  int defStyleAttr) { 24          super(context, attrs, defStyleAttr); 25     } 26  27  28     @Override 29      public  boolean dispatchTouchEvent(MotionEvent ev) { 30          int action = ev.getAction(); 31          switch(action){ 32              case MotionEvent.ACTION_DOWN: 33                 Log.e("view","dispatchTouchEvent-->ACTION_DOWN"); 34                  break; 35              case MotionEvent.ACTION_MOVE: 36                 Log.e("view","dispatchTouchEvent-->ACTION_MOVE"); 37                  break; 38              case MotionEvent.ACTION_UP: 39                 Log.e("view","dispatchTouchEvent-->ACTION_UP"); 40                  break; 41         } 42          return  super.dispatchTouchEvent(ev); 43     } 44  45     @Override 46      public  boolean onTouchEvent(MotionEvent event) { 47          int action = event.getAction(); 48          switch(action){ 49              case MotionEvent.ACTION_DOWN: 50                 Log.e("view","onTouchEvent-->ACTION_DOWN"); 51                  break; 52              case MotionEvent.ACTION_MOVE: 53                 Log.e("view","onTouchEvent-->ACTION_MOVE"); 54                  break; 55              case MotionEvent.ACTION_UP: 56                 Log.e("view","onTouchEvent-->ACTION_UP"); 57                  break; 58         } 59          return  true; 60     } 61  62  63 }

 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  /** 10   * Created by qjm3662 on 2016/4/10 0010. 11    */ 12  public  class view2  extends ImageView{ 13      public view2(Context context) { 14          super(context); 15     } 16  17      public view2(Context context, AttributeSet attrs) { 18          super(context, attrs); 19     } 20  21      public view2(Context context, AttributeSet attrs,  int defStyleAttr) { 22          super(context, attrs, defStyleAttr); 23     } 24  25     @Override 26      public  boolean dispatchTouchEvent(MotionEvent event) { 27          int action = event.getAction(); 28          switch(action){ 29              case MotionEvent.ACTION_DOWN: 30                 Log.e("view2", "dispatchTouchEvent-->ACTION_DOWN"); 31                  break; 32              case MotionEvent.ACTION_MOVE: 33                 Log.e("view2","dispatchTouchEvent-->ACTION_MOVE"); 34                  break; 35              case MotionEvent.ACTION_UP: 36                 Log.e("view2","dispatchTouchEvent-->ACTION_UP"); 37                  break; 38         } 39          return  super.dispatchTouchEvent(event); 40     } 41  42     @Override 43      public  boolean onTouchEvent(MotionEvent event) { 44          int action = event.getAction(); 45          switch(action){ 46              case MotionEvent.ACTION_DOWN: 47                 Log.e("view2","onTouchEvent-->ACTION_DOWN"); 48                  break; 49              case MotionEvent.ACTION_MOVE: 50                 Log.e("view2","onTouchEvent-->ACTION_MOVE"); 51                  break; 52              case MotionEvent.ACTION_UP: 53                 Log.e("view2","onTouchEvent-->ACTION_UP"); 54                  break; 55         } 56          return  false; 57     }

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 

 

转载于:https://www.cnblogs.com/qjm253/p/5374882.html

相关资源:数据结构—成绩单生成器

最新回复(0)