如何获取覆盖窗口上的触摸事件?

发布于 2024-12-23 10:18:07 字数 3154 浏览 0 评论 0原文

我有以下代码,基本上在窗口顶部绘制一个透明矩形。 我想拦截该矩形的触摸事件,而不是将事件发送到下面的窗口。 我尝试过 onInterceptTouchEvent、onTouchEvent,但是当我在矩形内部单击时,矩形后面的窗口会接收到触摸。

public class TestService extends Service {
    HUDView mView;
    int ScreenHeight;
    int ScreenWidth;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        try {
        super.onCreate();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        ScreenHeight=wm.getDefaultDisplay().getHeight();
        ScreenWidth=wm.getDefaultDisplay().getWidth();

        mView = new HUDView(this,ScreenHeight,ScreenWidth);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,

              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
           // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
              |  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                      | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 
                      | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR 
                      ,
                PixelFormat.TRANSLUCENT);
        //params.gravity = Gravity.RIGHT | Gravity.TOP;
        wm.addView(mView, params);

        }
        catch (Exception e) {
        e.printStackTrace();
        }
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
        if(mView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
            mView = null;
        }
    }
}

class HUDView extends ViewGroup {
    private Paint mLoadPaint;
    private int ScreenHeight;
    private int ScreenWidth;


    public HUDView(Context context, int WH, int WW) {
        super(context);
        ScreenHeight=WH;
        ScreenWidth=WW;
      //  Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show();

        mLoadPaint = new Paint();
        mLoadPaint.setARGB(100, 0, 0, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(0,0, 100, 100, mLoadPaint);
        //canvas.drawText("Hello World", 5, 15, mLoadPaint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
//if (event.getAction() == MotionEvent.ACTION_DOWN) {
    // Toast.makeText(getContext(), "onInterceptTouchEvent : " + event.getAction(),Toast.LENGTH_LONG);
 //}
        Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
        return true;

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        //super.onInterceptTouchEvent(event);
        Toast.makeText(getContext(), "onInterceptTouchEvent : " + event.getAction(),Toast.LENGTH_LONG);
        return true;
    }


    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }

}

I have the folowing code, that basically draws a transparent rectangle on top of windows.
I want to intercept the touch event for this rectangle instead of sending the event to the below window.
I've tried with onInterceptTouchEvent, onTouchEvent but when I click inside the rectangle the touch is received by the window behind the rectangle.

public class TestService extends Service {
    HUDView mView;
    int ScreenHeight;
    int ScreenWidth;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        try {
        super.onCreate();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        ScreenHeight=wm.getDefaultDisplay().getHeight();
        ScreenWidth=wm.getDefaultDisplay().getWidth();

        mView = new HUDView(this,ScreenHeight,ScreenWidth);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,

              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
           // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
              |  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                      | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 
                      | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR 
                      ,
                PixelFormat.TRANSLUCENT);
        //params.gravity = Gravity.RIGHT | Gravity.TOP;
        wm.addView(mView, params);

        }
        catch (Exception e) {
        e.printStackTrace();
        }
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
        if(mView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
            mView = null;
        }
    }
}

class HUDView extends ViewGroup {
    private Paint mLoadPaint;
    private int ScreenHeight;
    private int ScreenWidth;


    public HUDView(Context context, int WH, int WW) {
        super(context);
        ScreenHeight=WH;
        ScreenWidth=WW;
      //  Toast.makeText(getContext(),"HUDView", Toast.LENGTH_LONG).show();

        mLoadPaint = new Paint();
        mLoadPaint.setARGB(100, 0, 0, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(0,0, 100, 100, mLoadPaint);
        //canvas.drawText("Hello World", 5, 15, mLoadPaint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
//if (event.getAction() == MotionEvent.ACTION_DOWN) {
    // Toast.makeText(getContext(), "onInterceptTouchEvent : " + event.getAction(),Toast.LENGTH_LONG);
 //}
        Log.d("bThere", "X: " + (int)event.getX() + " Y: " + (int)event.getY());
        return true;

    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        //super.onInterceptTouchEvent(event);
        Toast.makeText(getContext(), "onInterceptTouchEvent : " + event.getAction(),Toast.LENGTH_LONG);
        return true;
    }


    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }

}

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

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

发布评论

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

评论(2

花间憩 2024-12-30 10:18:07

尝试将焦点设置为您想要处理触摸的视图

try setting the focus to the view where you want to handle the touch.

何以心动 2024-12-30 10:18:07

这是AOSP的PhoneWindowManager.java中的代码

@Override
public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) {
    switch (attrs.type) {
        case TYPE_SYSTEM_OVERLAY:
        case TYPE_SECURE_SYSTEM_OVERLAY:
            // These types of windows can't receive input events.
            attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
            attrs.flags &= ~WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
            break;
....
}

所以基本上,如果您的Window类型是TYPE_SYSTEM_OVERLAY,系统将自动添加标志WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE。因此,您的窗口将不会收到任何触摸事件。

Here is the code in AOSP's PhoneWindowManager.java

@Override
public void adjustWindowParamsLw(WindowManager.LayoutParams attrs) {
    switch (attrs.type) {
        case TYPE_SYSTEM_OVERLAY:
        case TYPE_SECURE_SYSTEM_OVERLAY:
            // These types of windows can't receive input events.
            attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
            attrs.flags &= ~WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
            break;
....
}

So basically, if your Window's type is TYPE_SYSTEM_OVERLAY, the system will automatically add the flag WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE. Therefore, your Window will not receive any touch event.

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