如何获取覆盖窗口上的触摸事件?
我有以下代码,基本上在窗口顶部绘制一个透明矩形。 我想拦截该矩形的触摸事件,而不是将事件发送到下面的窗口。 我尝试过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
焦点
设置为您想要处理触摸的视图
。try setting the
focus
to theview
where you want to handle the touch.这是AOSP的PhoneWindowManager.java中的代码
所以基本上,如果您的Window类型是TYPE_SYSTEM_OVERLAY,系统将自动添加标志WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE。因此,您的窗口将不会收到任何触摸事件。
Here is the code in AOSP's PhoneWindowManager.java
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.