How can I interact with elements behind a translucent Android app?

后端 未结 3 849
星月不相逢
星月不相逢 2020-12-03 00:22

I have found how to create a translucent background for my android app, but so far I haven\'t found how to interact with what\'s behind it (the home screen for example).

相关标签:
3条回答
  • 2020-12-03 00:33

    FLAG_NOT_TOUCH_MODAL will do what you want with the least code.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        ...
        setContentView(R.layout.my_activity_view);
        ...
    }
    

    public static final int FLAG_NOT_TOUCH_MODAL

    Window flag: Even when this window is focusable (its {@link FLAG_NOT_FOCUSABLE is not set), allow any pointer events outside of the window to be sent to the windows behind it. Otherwise it will consume all pointer events itself, regardless of whether they are inside of the window.

    0 讨论(0)
  • 2020-12-03 00:34

    I got the answer. Adding line getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); in onCreate in second top activity(which is transparent did the trick).

    Above solution has a problem. After using above whole window passes the touch to background. So if you want full control then better extend a layout e.g. Framelayout and override 'onTouchEvent' and get the touch location using event.getX() and event.getY() methods and return false where you want to pass the touch event. It will be passed to parent view.

    0 讨论(0)
  • 2020-12-03 00:41

    A very simple way of understanding stacking of views is to take a book (any book). Think of each page as a view.

    Visibility

    • When you open the book you can see a page (Main View - VISIBLE)
    • When you turn the current page you can see the next page (INVISIBLE - Main View to show the next view. Remember you are only hiding the view, if you set the visibility to GONE this is equivalent to tearing of the current page to view the next page.)

    Touch scenarios

    • Assume your first page is a wax page (translucent page similar to your translucent view) so you can see the underlying page
    • When you try to touch a figure on the second page, you are touching the first page although you can see the figure on the second page. It is impossible to touch the figure on the second page through the first page.

    Don't be disheartened, since it is a view you will be dealing with and not a paper you can still do what you want to do :)

    • Implement View.OnTouchListener for both your views
    • For translucent view any touch events you get, return FALSE in the method onTouch
    • Android will pass on the touch event to your underlying view.
    0 讨论(0)
提交回复
热议问题