Android - 拦截并传递所有触摸事件 [英] Android - Intercept and pass on all touch events

查看:1149
本文介绍了Android - 拦截并传递所有触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重叠的ViewGroup,它是当用户与应用程序交互时显示效果的屏幕大小,但仍然将onTouch事件传递给任何基础视图。

I have an overlay ViewGroup that is the size of the screen which I want to use to show an effect when the user interacts with the app, but still passes the onTouch event to any underlying views.

我在所有的MotionEvents(不仅仅是DOWN)中置疑,所以onInterceptTouchEvent()不会在这里应用,就好像我返回true我的overlay将消耗所有事件,如果false只会收到DOWN事件(同样适用于onTouch)。

I am intrested in all MotionEvents (not just DOWN), so onInterceptTouchEvent() does not apply here as if i return true my overlay will consume all events, and if false will only receive the DOWN events (the same applys to onTouch).

我以为我可以覆盖Activitys dispatchTouchEvent(MotionEvent ev)并在覆盖中调用自定义触摸事件,但是这样做的效果是不会根据我的观点的位置来翻译输入坐标(例如,所有的事件都会通过,因为没有考虑到系统栏,所以会发生在实际触摸之下的20个或更少的px)。 / p>

I thought I could override the Activitys dispatchTouchEvent(MotionEvent ev) and call a custom touch event in my overlay, but this has the effect of not translating the input coords depending on the position of my view (for example all events will pass appear to be happening 20 or so px below the actual touch as the system bar is not taken into account).

推荐答案

我用非完美的解决方案解决了这个问题,但在这种情况下工作。

I solved this with a non perfect solution but works in this situation.

我创建了另一个 ViewGroup ,其中包含一个可点击的小孩,其宽度和高度设置为 fill_parent (这是我的在添加效果之前的要求),并覆盖 onIntercept ...() 。在 onIntercept ... 我将事件传递到我的重叠视图到自定义的 onDoubleTouch(MotionEvent)方法,并做相关处理,而不会中断输入事件的平台路由,并让基础视图组正常。

I created another ViewGroup which contains a clickable child which has width and height set to fill_parent (this was my requirement before adding the effects) and have overridden onIntercept...(). In onIntercept... I pass the event on to my overlay view to a custom onDoubleTouch(MotionEvent) method and do the relevant processing there, without interrupting the platforms routing of input events and letting the underlying viewgroup act as normal.

简单!

这篇关于Android - 拦截并传递所有触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆