Android:透明视图无法正确管理触摸事件

发布于 2024-09-29 09:45:21 字数 797 浏览 2 评论 0原文

我希望在我的应用程序上方有一个透明的视图。
我只想知道当前的活动来做到这一点。
我找到了一种方法,通过 windowManager 添加新的框架布局 我正在这样做:

public static void AddViewAbove(Activity activity) {
    FrameLayout newLayout = new FrameLayout(activity);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
    WindowManager.LayoutParams.TYPE_APPLICATION,
    // if I let WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE   
    // then touchevent are passed to the application behind, but I cant handle touch in
    // my new frameLayout
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
    PixelFormat.TRANSLUCENT);
    activity.getWindowManager().addView(newLayout , lp);

}

我现在的主要问题是我无法在新的 FrameLayout 中以及同时在视图后面的应用程序中正确接收 touchevent。

I want to have a transparent view, above my application.
I want to do this just knowing the current activity.
I found a way of doing this by adding a new framelayout via the windowManager
I m doing this :

public static void AddViewAbove(Activity activity) {
    FrameLayout newLayout = new FrameLayout(activity);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
    WindowManager.LayoutParams.TYPE_APPLICATION,
    // if I let WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE   
    // then touchevent are passed to the application behind, but I cant handle touch in
    // my new frameLayout
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
    PixelFormat.TRANSLUCENT);
    activity.getWindowManager().addView(newLayout , lp);

}

My main problem now is that i can't receive touchevent correctly in my new FrameLayout and in the same time in application behind my view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2024-10-06 09:45:21

还没有测试过。但想法是您需要向下传递触摸事件。由于假设您的顶级(实际上是顶部)视图处理了该事件,因此您可能需要将透明框架布局设置为处理触摸事件(以及其他事件,如果适用)。

==更新==

您应该继承FrameLayout并创建您自己的透明布局。然后指示它监视并绕过触摸事件。 参考

实现该方法可以拦截所有触摸屏运动事件。这使您可以在事件发送给您的孩子时观看它们,并随时掌握当前手势的所有权。使用此函数需要小心,因为它与 View.onTouchEvent(MotionEvent) 有相当复杂的交互,并且使用它需要以正确的方式实现该方法以及该方法。事件将按以下顺序接收:

  1. 您将在此处收到 down 事件。
  2. 按下事件将由该视图组的子视图组处理,或者交给您自己的 onTouchEvent() 方法来处理;这意味着您应该实现 onTouchEvent() 以返回 true,这样您将继续看到手势的其余部分(而不是寻找父视图来处理它)。另外,通过从 onTouchEvent() 返回 true,您将不会在 onInterceptTouchEvent() 中收到任何后续事件,并且所有触摸处理都必须像平常一样在 onTouchEvent() 中发生。
  3. 只要您从此函数返回 false,接下来的每个事件(直到并包括最终的 up)都将首先传递到此处,然后传递到目标的 onTouchEvent()。
  4. 如果从这里返回 true,您将不会收到任何以下事件:目标视图将收到相同的事件,但操作为 ACTION_CANCEL,并且所有其他事件将传递到您的 onTouchEvent() 方法,并且不再出现在此处.

==更新==

顺便说一句,您应该使用自定义框架布局(或其他适合您的布局)来“容纳”另一个孩子。我建议您可以使用它作为根布局。

Haven't tested yet. But the idea is you need to pass-thru the touch event down. Since it is assumed that your top level (and actually top) view handled the event, you might need to set your transparent framelayout to NOT handle the touch event (and other events, if applies).

==Update==

You should inherit the FrameLayout and create your own transparent layout. then instruct it to monitor and by-pass the touch event. Reference

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point. Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1. You will receive the down event here.
  2. 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.
  3. 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().
  4. 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.

==UPDATE==

BTW, you should use your custom framelayout (or other layout that suits you) to "hold" the other child. I suggest you can use that as your root layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文