android触摸事件分发机制--锁屏部_第1页
android触摸事件分发机制--锁屏部_第2页
android触摸事件分发机制--锁屏部_第3页
android触摸事件分发机制--锁屏部_第4页
android触摸事件分发机制--锁屏部_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一触摸事件涉及的类:触摸事件涉及到的两个重要主角就是viewgroup和view,其中viewgroup继承自view。 所有的界面控件都是最终继承口 viewgroup或者view。二触摸事件涉及的方法说明:涉及的方法:dispatchtouchevent、ontouchevent> onlntercepttouchevent、onclicklistener onlongclicklistener、ontouchlistener。作用分别是:dispatchtouchevent 事件分发onlntercepttouchevent 事件拦截ontouchevent 事件处理。onclic

2、klistener 点击事件onlongclicklistener 长按事件ontouchlistener触摸事件监听view 类里有两个方法 dispatchtouchevent, ontoucheventoviewgroup 类里新增了 onlntercepttouchevent 力法。由于viewgroup触摸事件可以分发给子view处理,如果不想让子view处理就可以拦截 事件处理,但是view没有子view,所以处理事件吋不需要做拦截处理,所以就没有 onlntercepttouchevent 接口。它们之间的调丿ij关系如何将在如下的源码分析中充分说明三触摸事件源码分析说明:个to

3、uch事件敲先到达的是根节点,即acitivty的viewgroup时,它会依次下发,所 以我们需要从第一个收到触摸事件的控件viewgroup来跟踪触摸事件的分发处理流程。対于上层应用來说触摸事件笫一入口是viewgroup的dispatchtouchevent:publicbooleandispatchtouchevent(motioneventev) /*对mfirsttouchtarget解释:是第一个找到的接收处理触摸事件的子view, down 的时候如果子view的dispatchtouchevent返回true (般子view不复写此方法,所以只要注 册的ontouchlist

4、er接口或ontouchevent方法返回true ) mfirsttouchtarget就不是null。* 如果了 view 的 down-di 件返elfalse 那么 m firsttouchtarget 就是 null, z 后 *move、up事件就不给子view /* /if (actionmasked = motionevent.>4ctio/v_doiv/v| | mfirsttouchtarget != null) if (!disallowlntexcept) intercepted = onlntercepttouchevent(ev); else intercep

5、ted = false; else / there are no touch targets and this action is not an initial down/ so this view group continues to intercept ercepted = true;/如果自己的onlntercepttouchevent拦截返冋true或者down的时候子view没有处理触摸 件/就不在执行如下逻辑,即不在调用子view的触摸事件if (!canceled&& !intercepted) if (newtouchtarget = nu

6、ll&&childrencount != 0) for (inti = childrencount - 1; i>= 0; i-) /调用子view的触摸方法,如果子view接收触摸事件并处理(子view的dispatchtouch或者 ontouch 返 idtrue)if (dispatchtransformedtouchevent(ev, false, child.idbitstoassign) / child wants to receive touch within its/ bounds,给mfirsttouchtarget赋值newtouchtarget =

7、 addtouchtarget(child, idbitstoassign);alreadydispatchedtonewtouchtarget = true;break;/ dispatch to touch targets.if (mfirsttouchta“egt = null) / no touch targets so treat this as an ordinary/ view.调用父view自己的touchhandied = dispatchtransformedtouchevent(ev, cancelednull,touchtarget.all pointer ids);

8、else / dispatch to touch targets, excluding the new touch target if we/ already/ dispatched to it. cancel touch targets if necessary.touchtarget target = mfirsttouchtarget;finaltouchtarget next = target.next;/此逻辑保证down的时候了 view的触摸爭件down不会被调用两次if (alreadydispatchedtonewtouchtarget&& target =

9、newtouchtarget) handied = true;else finalbooleancancelchild = resetcancelnextupflag(target.child) | | intercepted;/调用了view的touch方法,如果父view的onlntercepttouchevent的down没右拦截但是 move的时候拦截了就会调用子view的cancelif (dispatchtransformedtouchevent(ev, cancelchild,targetchild, targetpointerldbits) handied = true;ret

10、urnhandled;通过源码分析看dispatchtouchevent方法里主耍负责触摸事件分发,且在触摸事件中占有 重要角色。在dispatchtouchevent方法开始处会先调用onlntercepttouchevent方法,但是 viewgroup的此方法里是直接返回false,就是默认不拦截处理,一般我们口己的控件在处理 触摸事件时可以复写此方法加入自己的逻辑。在onlntercepttouchevent没冇拦截处理吋就会继续调用遍历所冇的了 view的触摸事件, 遍历的规则的是从上层view到底层view «遍历的时候涉及到的方法 dispatchtransformed

11、touchevent 此方法主要逻辑如 fprivateboolea ndispatchtra nsformedtoucheve nt(moti on eve nt eve nt, boolea n can cel,view child, intdesiredpointerldbits) finalboolean handled;/ perform any necessary transformations and dispatch.如果没有子view或者没有找到在触摸点内的子viewif (child = null) han died = super.dispatchtouchevent(t

12、ra nsformedeve nt); else han died = child.dispatchtoucheve nt(tra nsformedevent);如果没有找到要处理触摸事件的了 view , 就会调用 super.dispatchtouchevent(transformedevent);由于 viewgroup 继承自 view,所以实你执行的是 view 的 dispatchtouchevent,具体逻辑如下:publicboolea ndispatchtoucheve nt(moti on event eve nt) if (onfiltertoucheventforsec

13、urity(event) n oinspectio nsimplifiablelfstateme ntlistenerlnfo li = mlistenerlnfo;if (li != null &&li.montouchlistener != null&& (mviewflags& enabled_mask) = enabled&&li.mo ntouchliste ner.on touch(this, eve nt) result = true;if ('result &&ontouchevent(event)

14、 result = true;return result;通过上面的代码看出如果view自己注册监听了 ontouchlistener就优先调用此接口,如果 此接口返回true就不会继续调用ontouchevent,如果没有注册监听或者返回false,就会继 续调用自己的ontouchevent方法publicboolea non toucheve nt(ivioti on eve nt eve nt) switch (action) casemotioneve ntaction_up:removelongpresscallback();if (mperformclick = null) mp

15、erformclick = new performclick();if (!post(mperformclick) ? onclickonlongclick 的关系performclickf);break;casemoti oneven t.action_down:setpressed(true, x, y);checkforlongclick(o);break;casemotionevent.action_move:/ be lenient about moving outside of buttonsif (!pointlnview(x, y, mtouchslop) / outside

16、butt onremovetapcallback();讦(mprivateflags& pflag_pressed) != 0) / remove any future long press/tap checks removel on gpresscallback();setpressed(false);break;return true;return false;如上标黄色部分分别是调用onlongclicklistener接口和onclick接口处理点击和长按事件, 所以点击和长按是在ontouch里面处理,在down后通过post 一个延迟的消息决定调用 onlongclick,

17、在up或者move的时候可能会移除此次消息,在up的时候会调用onclicko四 从源码分析看viewgroup和view的区别从 viewgroup 和 view 的源码看,viewgroup 重写的 dispatchtouchevent 并且多了 onlntercepttouchevent, viewgroup里这两个方法的逻辑导致viewgroup冇权决定是否将触 摸事件分发给子view执行,view只有是否接收处理分发过來的触摸事件的权利。(1) viewgroup 的触摸事件 dispatchtouchevent 会先调用自己的 onlntercepttouchevent, 此方法的

18、返回值决定是循环调用子view的触摸事件查找接收触摸事件的日标view,还是调 用自己的触摸ontouch事件。如果onlntercepttouchevent的down返回false,且子view处 理了触摸事件,move时onlntercepttouchevent返回true,就会调用子view的cancel,后续 会调用父view自己的toucho(2) view的dispatchtouchevent里面会先判断是否调用口己的ontouchlistener的接口冋调, 如果调用了且返回为true,就不在调用自己的ontouchevent逻辑,如果没有调用或者返回 false就会继续调用on

19、touchevent逻辑。所以可以看出ontouchlistener接口回调和 ontouchevent的关系。在ontouchevent里血会有决定调用onclick和onlongclick接口的逻辑。所以如果一个view实现ontouchlistener接口月.返回值是true,他的onclick, onlongclick 和ontouchevent逻辑就不再起作用。五事例说明textviewl如上图,vg2是vg1的孩子,textviewl是vg2的孩子,触摸点击到小红点所在位置: 如果 vg2 和 vg1 没有复写 onlntercepttouchevent 拦截事件,textviewl 的 ontouchevent的down返冋true,触摸事件传递逻辑如下:如果 vg2 和 vg1 没有复写 onintercepttouchevent 拦截事件,textviewl 的 ontouchevent 的down返回false, vg2的ontouchevent的down返回true,触摸事件传递逻辑如下,且后续move和up就不在会执彳亍textviewl的触摸事件:vg1 ithmribv

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论