android onTouchEvent处理机制总结(必看)

android onTouchEvent处理机制总结(必看),第1张

概述项目中总会用到一些触摸事件,每次使用都是百度各种资料,看各种大神的分析笔记。这次我自己总结下关于触摸事件的一些知识点。一来可以让自己对触摸事件印象更加深刻,也给以后的项目做一个参考。最难理解的其实是on

项目中总会用到一些触摸事件,每次使用都是百度各种资料,看各种大神的分析笔记。这次我自己总结下关于触摸事件的一些知识点。一来可以让自己对触摸事件印象更加深刻,也给以后的项目做一个参考。最难理解的其实是ontouchEvent方法。

一、 概述

1.只有vIEw,VIEwGroup,Activity 具有事件分发和消费的功能。

2.Activity因为上最先接触到触摸事件,因此Activity没有事件拦截方法。即没有dispatchtouchEvent方法。

3.对于不能添加子控件的vIEw,不能对事件进行分发和拦截,它只有ontouchEvent事件。

二、三个方法

1.public boolean dispatchtouchEvent(MotionEvent ev)

当触摸事件发生的时候,首先会被当前的activity进行分发,即当前activity的dispatchtouchEvent方法会被执行。

这个时候,该方法有三种返回的情况:

return false: 表明事件不会被进行分发。事件会以冒泡的方式被传递给上层的vIEw或activity的ontouchEvent方法进行消费掉。

return true:表明该时间已经被处理。事件会被当前vIEw或activity的dispatchtouchEvent给消费掉。不会再进行传递,事件到此结束。

return super.dispatchtouchEvent(ev):表明该事件将会被分发。此时当前VIEw的onInterceptertouchEvent方法会捕获该事件,判断需不需要进行事件的拦截。

2.public boolean onIntercepttouchEvent(MotionEvent ev)    

该方法用户拦截被传递过来的事件,用于判断被传递过来的事件是否需要被当前的vIEw进行处理。

return false :不对事件进行拦截,放行该事件。事件会被传递到当前vIEw的子控件中,由子控件中的dispatchtouchEvent方法进行分发处理。

return true : 拦截该事件,将该事件交给当前vIEw的ontouchEvent方法进行处理。

return super.inIntercepttouchEvent(ev):默认拦截方式,和return true一样。该事件会被拦截,将该事件交给当前vIEw的ontouchEvent方法进行处理。(这里需要有一点说明,当有两个vIEw。A vIEw中有一个B vIEw.点击A.A中如果onIntercepttouchEvent()返回super.intercepttouchEvent(ev),则事件将会被A进行拦截,交给A的ontouchEvent()进行处理,如果点击的是B,A中如果onIntercepttouchEvent()返回super.intercepttouchEvent(ev),则事件将不会被拦截,会被分发到子控件中) 

3.public boolean ontouchEvent(MotionEvent event)

当前的vIEw把事件进行了拦截,则事件则会被传递到该方法中

return false:表明没有消费该事件,事件将会以冒泡的方式一直被传递到上层的vIEw或Activity中的ontouchEvent事件处理。如果最上层的vIEw或Activity中的ontouchEvent还是返回false。则该事件将消失。接下来来的一系列事件都将会直接被上层的ontouchEvent方法捕获

return true: 表明消费了该事件,事件到此结束。

return super.ontouchEvent(event):默认情况,和return false一样。

验证:

MainActivity FatherVIEw ChildVIEw中几个方法都返回super.****touchEvent(ev)

分析:

1、当点击屏幕。MainActivity 中的dispatchtouchEvent方法先执行,打印MainActivity-dispatchtouchEvent-->ACTION_DOWN

2、因为返回的是super.dispatchtouchEvent(ev),所以事件ev将会被分发,但是MainActivity中没有onIntercepttouchEvent()方法,所以事件被传递到FatherVIEw中的dispatchtouchEvent方法.打印FatherVIEw-dispatchtouchEvent-->ACTION_DOWN

3、在FatherVIEw中dispatchtouchEvent返回的是super.dispatchtouchEvent(ev),所有事件会被分发。FatherVIEw中的onIntercepttouchEven()中的方法被执行。打印FatherVIEw-onIntercepttouchEven-->ACTION_DOWN

4、FatherVIEw中的onIntercepttouchEven()返回的是super.onIntercepttouchEvent(ev)。在这里,(1)如果点击的是屏幕中的ChildVIEw。事件将不会被拦截,会被传递到ChildVIEw中的dispatchtouchEvent方法中。(2)如果点击的值FatherVIEw则事件将会被拦截。FatherVIEw中的ontouchEvent()方法将被执行。以(1)为例,将打印ChildVIEw-dispatchtouchEvent-->ACTION_DOWN。

5、ChildVIEw中dispatchtouchEvent返回的是super.dispatchtouchEvent(ev),所有事件会被分发。打印ChildVIEw-onIntercepttouchEvent-->ACTION_DOWN。

6、此时ChildVIEw中onIntercepttouchEvent返回的是super.onIntercepttouchEvent(ev),,而且已经没有子控件了,所以事件将被拦截。打印ChildVIEw-ontouchEvent-->ACTION_DOWN。

7、在childVIEw中ontouchEvent()返回额是super.ontouchEvent(ev)。事件将不会被消耗,将以冒泡的方式传递到上层空间中的ontouchEvent(),此处上层空间中的ontouchEvent返回的都是super.ontouchEvent(ev)。所以讲一次打印 Father-ontouchEvent-->ACTION_DOWN。 MainActivty-ontouchEvent-->ACTION_DOWN。

8、之后的事件动作,将不再被MainActivity分发到子vIEw,直接被MainActivty中的ontouchEvent处理消耗。打印MainActivity-dispatchtouchEvent-->ACTION_UP,MainActivty-ontouchEvent-->ACTION_UP

MainActivity-dispatchtouchEvent-->ACTION_DOWNFatherVIEw-dispatchtouchEvent-->ACTION_DOWNFatherVIEw-onIntercepttouchEven-->ACTION_DOWNChildVIEw-dispatchtouchEvent-->ACTION_DOWNChildVIEw-onIntercepttouchEvent-->ACTION_DOWN。ChildVIEw-ontouchEvent-->ACTION_DOWNFather-ontouchEvent-->ACTION_DOWN。 MainActivty-ontouchEvent-->ACTION_DOWNMainActivity-dispatchtouchEvent-->ACTION_UP,MainActivty-ontouchEvent-->ACTION_UP

代码

MainActivity.java

public class MainActivity extends Activity {   private static final String TAG = "MainActivity";   @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate( savedInstanceState);   setContentVIEw(R.layout. activity_main);  }   @OverrIDe  public boolean ontouchEvent(MotionEvent event) {   // Todo auto-generated method stub   Log. i(TAG,"activity-ontouchEvent-->" + touchEventUtil.gettouchAction(event.getAction()));   return super.ontouchEvent( event);  }   @OverrIDe  public boolean dispatchtouchEvent(MotionEvent ev) {   Log. i(TAG,"activity-dispatchtouchEvent-->" + touchEventUtil.gettouchAction(ev.getAction()));   return super.dispatchtouchEvent( ev);  }
FatherVIEw.java
public class FatherVIEw extends linearLayout {  private static final String TAG = "MainActivity";   public FatherVIEw(Context context) {   super( context);   }   @OverrIDe  public boolean dispatchtouchEvent(MotionEvent ev) {   Log. i(TAG,"Father-dispatchtouchEvent-->" + touchEventUtil.gettouchAction(ev.getAction()));   return super.dispatchtouchEvent( ev);  }   @OverrIDe  public boolean onIntercepttouchEvent(MotionEvent ev) {   Log. i(TAG,"Father-onIntercepttouchEvent-->" + touchEventUtil.gettouchAction(ev.getAction()));   return super.onIntercepttouchEvent( ev);  }   @OverrIDe  public boolean ontouchEvent(MotionEvent event ) {   Log. i(TAG,"Father-ontouchEvent-->" + touchEventUtil.gettouchAction(event.getAction()));   return super.ontouchEvent( event);  } }
ChildVIEw.java
public class ChildVIEw extends linearLayout {   private static final String TAG = "MainActivity";   public ChildVIEw(Context context) {   super( context);  }   @OverrIDe  public boolean dispatchtouchEvent(MotionEvent ev) {   Log. i(TAG,"Child-dispatchtouchEvent-->" + touchEventUtil.gettouchAction(ev.getAction()));   return super.dispatchtouchEvent( ev);  }   @OverrIDe  public boolean onIntercepttouchEvent(MotionEvent ev) {   Log. i(TAG,"Child-onIntercepttouchEvent-->" + touchEventUtil.gettouchAction(ev.getAction()));   return super.onIntercepttouchEvent( ev);  }   @OverrIDe  public boolean ontouchEvent(MotionEvent event ) {   Log. i(TAG,"Child-ontouchEvent-->" + touchEventUtil.gettouchAction(event.getAction()));   return super.ontouchEvent( event);  } } 

activity_main.xml

<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="${relativePackage}.${activityClass}" >  <com.ethanlbb.toucheventtest.FatherVIEw  androID:layout_wIDth= "match_parent"  androID:layout_height= "match_parent"  androID:background= "@androID:color/holo_blue_dark"  androID:gravity= "center" >   <com.ethanlbb.toucheventtest.ChildVIEw   androID:layout_wIDth= "200dp"   androID:layout_height= "200dp"   androID:layout_gravity= "center"   androID:background= "#0000ff" >  </com.ethanlbb.toucheventtest.ChildVIEw > </com.ethanlbb.toucheventtest.FatherVIEw > </relativeLayout>

以上这篇androID ontouchEvent处理机制总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android onTouchEvent处理机制总结(必看)全部内容,希望文章能够帮你解决android onTouchEvent处理机制总结(必看)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146380.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存