我有以下代碼,基本上在窗口上繪製一個透明的矩形。 我想攔截這個矩形的觸摸事件,而不是將事件發送到下面的窗口。 我嘗試過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) {
}
}
你最後做了什麼? – Anand 2015-09-16 18:01:28