如何在叠加窗口上获取触摸事件?

时间:2011-12-26 21:39:00

标签: android events window touch overlay

我有以下代码,基本上在窗口上绘制一个透明矩形。 我想截取此矩形的触摸事件,而不是将事件发送到下面的窗口。 我已经尝试使用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) {
    }

}

2 个答案:

答案 0 :(得分:0)

尝试将focus设置为您要处理触摸的view

答案 1 :(得分:0)

这是AOSP的PhoneWindowManager.java中的代码

<div class="form-group">
    <label class="col-md-3 control-label" for="folder_name">Folder-Name:</label>
    <div class="col-md-9">
        <input type="text" class="form-control" id="folder_name" name="folder_name" pattern="[^\\/:\"\*\?<>|]+" required>
    </div>                    
</div>

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