android onTouch事件
2016-04-24 22:21
417 查看
android系统中的每个ViewGroup的子类都具有下面三个和TouchEvent处理密切相关的方法:
1)public boolean dispatchTouchEvent(MotionEvent ev) 这个方法用来分发TouchEvent
2)public boolean onInterceptTouchEvent(MotionEvent ev) 这个方法用来拦截TouchEvent
3)public boolean onTouchEvent(MotionEvent ev) 这个方法用来处理TouchEvent
当然并不是所有view 都有onInterceptTouchEvent,只有viewGrou 才有
说明: 白色为最外层,它占满整个屏幕;
红色为中间区域,属于白色中的一层;
黑色为中心区域,必于红色中的一层。
注意:他们本质上是:LinearLayout,而不是RelativeLayout或者其它布局。
1.由中心区域处理touch事件
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.kris.touch.widget.TouchView
android:id="@+id/view_out"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_mid"
android:layout_width="300px"
android:layout_height="400px"
android:background="#f00"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_center"
android:layout_width="150px"
android:layout_height="150px"
android:background="#000"
android:gravity="center"
android:clickable="true">
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</LinearLayout>
注意: android:clickable="true"
结合是上面的日志,我们可以看一下ACTION_DOWN事件处理流程:
说明:
首先触摸事件发生时(ACTION_DOWN),由系统调用Activity的dispatchTouchEvent方法,分发该事件。根据触摸事件的坐标,将此事件传递给out的dispatchTouchEvent处理,out则调用onInterceptTouchEvent 判断事件是由自己处理,还是继续分发给子View。此处由于out不处理Touch事件,故根据事件发生坐标,将事件传递给out的直接子View(即middle)。
Middle及Center中事件处理过程同上。但是由于Center组件是clickable 表示其能处理Touch事件,故center中的onInterceptTouchEvent方法将事件传递给center自己的onTouchEvent方法处理。至此,此Touch事件已被处理,不继续进行传递。
当clickable="true"
的时候,view中onTouchEvent 返回的是true(源码中),,则说明消耗掉了这个事件,这个时候onTouchEvent将不会再向上传递,则说明消耗掉了这个事件,返回值是
false ,则没有消耗掉,会继续传递下去。 下面会证明。
2.没有指定谁会处理touch事件
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.kris.touch.widget.TouchView
android:id="@+id/view_out"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_mid"
android:layout_width="300px"
android:layout_height="400px"
android:background="#f00"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_center"
android:layout_width="150px"
android:layout_height="150px"
android:background="#000"
android:gravity="center">
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</LinearLayout>
复制代码
注意:只是比上一次的布局少了android:clickable="true"
接下来我们看一下打印的日志
结合是上面的日志,我们可以看一下ACTION_DOWN事件处理流程:
说明:
事件处理流程大致同上,区别是此状态下,所有组件都不会处理事件,事件并不会被center的onTouchEvent方法“消费”,则事件会层层逆向传递回到Activity,若Activity也不对此事件进行处理,此事件相当于消失了(无效果)。
对于后续的move、up事件,由于第一个down事件已经确定由Activity处理事件,故up事有由Activity的dispatchTouchEvent直接分发给自己的onTouchEvent方法处理。
总结:
1) Touchevent 中,返回值是 true ,则说明消耗掉了这个事件,返回值是 false ,则没有消耗掉,会继续传递下去,这个是最基本的。2) 事件传递的两种方式:
隧道方式:从根元素依次往下传递直到最内层子元素或在中间某一元素中由于某一条件停止传递。
冒泡方式:从最内层子元素依次往外传递直到根元素或在中间某一元素中由于某一条件停止传递。 android对Touch Event的分发逻辑是View从上层分发到下层(dispatchTouchEvent函数)类似于隧道方式,然后下层优先开始处理Event(先mOnTouchListener,再onTouchEvent)并向上返回处理情况(boolean值),若返回true,则上层不再处理。类似于冒泡方式
1)public boolean dispatchTouchEvent(MotionEvent ev) 这个方法用来分发TouchEvent
2)public boolean onInterceptTouchEvent(MotionEvent ev) 这个方法用来拦截TouchEvent
3)public boolean onTouchEvent(MotionEvent ev) 这个方法用来处理TouchEvent
当然并不是所有view 都有onInterceptTouchEvent,只有viewGrou 才有
说明: 白色为最外层,它占满整个屏幕;
红色为中间区域,属于白色中的一层;
黑色为中心区域,必于红色中的一层。
注意:他们本质上是:LinearLayout,而不是RelativeLayout或者其它布局。
1.由中心区域处理touch事件
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.kris.touch.widget.TouchView
android:id="@+id/view_out"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_mid"
android:layout_width="300px"
android:layout_height="400px"
android:background="#f00"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_center"
android:layout_width="150px"
android:layout_height="150px"
android:background="#000"
android:gravity="center"
android:clickable="true">
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</LinearLayout>
注意: android:clickable="true"
结合是上面的日志,我们可以看一下ACTION_DOWN事件处理流程:
说明:
首先触摸事件发生时(ACTION_DOWN),由系统调用Activity的dispatchTouchEvent方法,分发该事件。根据触摸事件的坐标,将此事件传递给out的dispatchTouchEvent处理,out则调用onInterceptTouchEvent 判断事件是由自己处理,还是继续分发给子View。此处由于out不处理Touch事件,故根据事件发生坐标,将事件传递给out的直接子View(即middle)。
Middle及Center中事件处理过程同上。但是由于Center组件是clickable 表示其能处理Touch事件,故center中的onInterceptTouchEvent方法将事件传递给center自己的onTouchEvent方法处理。至此,此Touch事件已被处理,不继续进行传递。
当clickable="true"
的时候,view中onTouchEvent 返回的是true(源码中),,则说明消耗掉了这个事件,这个时候onTouchEvent将不会再向上传递,则说明消耗掉了这个事件,返回值是
false ,则没有消耗掉,会继续传递下去。 下面会证明。
2.没有指定谁会处理touch事件
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.kris.touch.widget.TouchView
android:id="@+id/view_out"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_mid"
android:layout_width="300px"
android:layout_height="400px"
android:background="#f00"
android:gravity="center">
<com.kris.touch.widget.TouchView
android:id="@+id/view_center"
android:layout_width="150px"
android:layout_height="150px"
android:background="#000"
android:gravity="center">
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</com.kris.touch.widget.TouchView>
</LinearLayout>
复制代码
注意:只是比上一次的布局少了android:clickable="true"
接下来我们看一下打印的日志
结合是上面的日志,我们可以看一下ACTION_DOWN事件处理流程:
说明:
事件处理流程大致同上,区别是此状态下,所有组件都不会处理事件,事件并不会被center的onTouchEvent方法“消费”,则事件会层层逆向传递回到Activity,若Activity也不对此事件进行处理,此事件相当于消失了(无效果)。
对于后续的move、up事件,由于第一个down事件已经确定由Activity处理事件,故up事有由Activity的dispatchTouchEvent直接分发给自己的onTouchEvent方法处理。
总结:
1) Touchevent 中,返回值是 true ,则说明消耗掉了这个事件,返回值是 false ,则没有消耗掉,会继续传递下去,这个是最基本的。2) 事件传递的两种方式:
隧道方式:从根元素依次往下传递直到最内层子元素或在中间某一元素中由于某一条件停止传递。
冒泡方式:从最内层子元素依次往外传递直到根元素或在中间某一元素中由于某一条件停止传递。 android对Touch Event的分发逻辑是View从上层分发到下层(dispatchTouchEvent函数)类似于隧道方式,然后下层优先开始处理Event(先mOnTouchListener,再onTouchEvent)并向上返回处理情况(boolean值),若返回true,则上层不再处理。类似于冒泡方式
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories