- 浏览: 454931 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
wwwjiandan:
我现在遇到的问题是将Activity设置为非全屏,且andro ...
Android中软键盘弹出时底部菜单上移问题 -
zjhdreams:
求下载链接
Android 9Patch (NinePatch) -
wtjandjay:
受教了 .......
从程序员到项目经理 -
mr_zhang2011:
将博客搬至CSDN -
fyc0109:
为什么我传过去的值, 有的都不对呢, 顺序也是一样的.就是获得 ...
Android Parcelable序列化自定义类集合在Activity间传递
本文源地址:http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html#
onInterceptTouchEvent()用于处理事件并改变事件的传递方向。返回值为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)。
onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。
<?xml version="1.0" encoding="utf-8"?>
<com.touchstudy.LayoutView1 xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.touchstudy.LayoutView2
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<com.touchstudy.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="AB"
android:textSize="40sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#0000FF"/>
</com.touchstudy.LayoutView2>
</com.touchstudy.LayoutView1>
在没有重写onInterceptTouchEvent()和onTouchEvent()的情况下(他们的返回值都是false), 对上面这个布局,MotionEvent事件的传递顺序如下:
当某个控件的onInterceptTouchEvent()返回值为true时,就会发生截断,事件被传到当前控件的onTouchEvent()。如我们将LayoutView2的onInterceptTouchEvent()返回值为true,则传递流程变成:
如果我们同时将LayoutView2的onInterceptTouchEvent()和onTouchEvent()设置成true,那么LayoutView2将消费被传递的事件,同时后续事件(如跟着ACTION_DOWN的ACTION_MOVE或者ACTION_UP)会直接传给LayoutView2的onTouchEvent(),不传给其他任何控件的任何函数。同时传递给子控件一个ACTION_CANCEL事件。传递流程变成(图中没有画出ACTION_CANCEL事件):
LayoutView2.onTouchEvent().ACTION_DOWN
会调用LayoutView1.OntnterceptTouchEvent().ACTION_MOVE如下:
05-25 07:02:44.149: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_DOWN
05-25 07:02:44.149: DEBUG/LayoutView2(1820): onInterceptTouchEvent action:ACTION_DOWN
05-25 07:02:44.149: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_DOWN
05-25 07:02:44.179: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.179: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.210: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.219: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.239: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.249: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.269: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.279: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.299: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.299: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.329: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.329: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.359: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.359: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.379: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.389: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.389: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_UP
05-25 07:02:44.399: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_UP
附SDK给出的说明:
· You will receive the down event here.
· The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
· For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
· If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
来自http://hi.baidu.com/j_fo/blog/item/7321c91324203437dc54017d.html
总结:
onInterceptTouchEvent()说的是是否允许Touch事件继续向下(子控件)传递,一但返回True,则向下传递之路被截断(所有子控件将没有机会参与Touch事件);
onTouchEvent()说的是当前控件在处理完Touch事件后,是否还允许Touch事件继续向上(父控件)传递,一但返回True,则父控件不用操心,由自己来处理Touch事件。
实例见附件:
- InterceptTouchStudyActivity.zip (50.8 KB)
- 下载次数: 50
发表评论
-
android Log.isLoggable方法的使用
2014-06-06 15:35 2923android 动态控制logcat日志开关,通过Log. ... -
View not attached to window manager
2013-01-10 11:55 1036View not attached to window man ... -
代码设置android:icon,android:label
2013-01-05 15:26 7915requestWindowFeature( Window.F ... -
Android项目为一个apk设置多个ICON图标和执行入口
2013-01-05 12:13 3827Android开发中,一个工程对应一个AndroidManif ... -
Intent跳转到系统应用中的拨号界面、联系人界面、短信界面及其他
2012-12-04 12:15 01:调用系统发送短信界面 Uri smsT ... -
Android 源码目录结构详解
2012-11-22 17:20 1420这是Android2.1的源代码的 ... -
如何获取android源代码
2012-07-10 17:50 2704如何获取android源代码 研究 ... -
Android uses-permission大全
2012-06-12 17:33 1010android.permission.ACCESS_CHE ... -
Android 应用程序基础(Application Fundamentals)
2012-05-23 17:42 1186Android 应用程序基础(Application Fun ... -
转Android 音乐频谱实现
2012-02-18 23:35 0最近由于需要实现音乐频谱,所以今天就为大家普及一下。 这里实现 ... -
android VideoView本工程mp4文件
2012-01-09 18:42 2647最近在做一个demo,要求播放视频,记录一下。使用的是Vid ... -
关于android播放mp3与歌词同步问题收集
2012-01-09 17:57 1607关于android播放mp3与歌词同步问题收集,有时间再试。 ... -
android使用MediaPlayer播放音乐文件时遇到的问题
2012-01-09 17:38 18255把mp3文件放在Assets文件夹里,然后用MediaPlay ... -
android资源目录---assets与res/raw的不同
2012-01-05 11:49 2291android资源目录---assets与 ... -
Android 的cpu硬盘 内存 网络设置 系统信息 硬件信息
2012-01-04 10:56 1517转载 http://www.cnmsdn.com/html/2 ... -
使用 Android Compatibility Package 来简化开发工作
2012-01-04 10:12 1388转载 http://www.cnmsdn.com/html/2 ... -
Android 9Patch (NinePatch)
2011-12-26 16:06 44411:介绍 NinePatch图片以*.9.png结尾,和普通 ... -
Windows平台下Android源码的下载
2011-12-21 11:06 1177Windows平台下Android源码的下载 ... -
改变MenuItem默认背景
2011-12-07 19:58 1409@Override public boolean onCre ... -
Android横屏竖屏切换
2011-11-10 18:20 107转载 http://blog.csdn.net/leesido ...
相关推荐
onInterceptTouchEvent和onTouchEvent调用关系详解
关于onInterceptTouchEvent和onTouchEvent使用demo,很不错的一个demo,可以帮助Android的触摸事件。
onInterceptTouchEvent与OntouchEvent之间的事件传递机制的具体内涵关系...
onInterceptTouchEvent和onTouchEvent调用时序
NULL 博文链接:https://username2.iteye.com/blog/2193153
onInterceptTouchEvent和onTouchEvent调用时序详解 测试demo 详细介绍请移步:http://blog.csdn.net/yiranxinshou/article/details/9201833
本篇文章小编为大家介绍,Android onTouchEvent 与 onInterceptTouchEvent的区别详解。需要的朋友参考下
NULL 博文链接:https://yxwww.iteye.com/blog/1409461
安卓事件分发机制测试代码,事件传递从Activity-->ViewGroup-->View。dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent这三个函数的返回值不同,代表的事件传递的不同。
(之前上传的代码有bug,经过作者整理和修改,fix掉部分bug) ...关键技术是理解清楚“onInterceptTouchEvent和onTouchEvent”的关系,以及computeScroll和dispatchDraw等。 可以在百度中搜索关键词获取答案。
NULL 博文链接:https://geyubin.iteye.com/blog/1633134
onInterceptTouchEvent 和 onTouchEvent 研究
安卓Android事件分发处理 ...主要处理事件分发 和事件消费dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent 3个方法对事件的拦截与否。10分我都觉得少,下不起就别用这么牛逼的自定义,让产品经理换需求。
onInterceptTouchEvent ontouchevent 的一个执行顺序和关于返回值的小demo 希望有点用
在View中其间会调用onTouchEvent(),在ViewGroup中其间会调用onInterceptTouchEvent()和onTouchEvent()。 **onInterceptTouchEvent():**这个函数是事件拦截函数,是ViewGroup才有的函数。
android 事件传递在很多地方被描述的很复杂,难懂,其实只要自己通过代码跟一样程序的执行过程,看一下debug日志输出,就很容易理解了,在本代码里通过更改onInterceptTouchEvent和onTouchEvent的返回值,观查debug...
08-25Android触屏事件演示Demo,演示OnTouchListener、OnClickListener和OnLongClickListener的触发,2014-08-17Android触屏事件演示Demo,演示触屏事件在view的onInterceptTouchEvent和onTouchEvent方法中分发的...
通过代码实践总结dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent的调用流程、顺序、及返回值对事件处理的影响。
基于这个猜测,我自定义一个ViewPager,重写dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent,分别在这三个方法中打印log; 重写SlidingMenu的dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent,...