无法获取TYPE_SYSTEM_OVERLAY的Touch事件

我喜欢开发一个类似于Swapps的应用程序。我试图在任何屏幕上显示一个button,这应该只发生在一个滑动action.I尝试与下面的代码

WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.type =WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; params.format =PixelFormat.TRANSLUCENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; parentlay = new LinearLayout(this); mybt = new Button(getApplicationContext()); bt.setText("Click to Go"); parentlay.setBackgroundColor(Color.TRANSPARENT); parentlay.setHapticFeedbackEnabled(true); parentlay.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // event.getAction() prints 4 and not able to get up or down event } }); windowmanager.addView(parentlay, params); 

我试图显示button,当用户从左到右滑动通过捕获该事件和做

  parentlay.addView(bt); 

并在用户从右向左滑动时删除视图

  parentlay.removeView(bt); 

如上所述,我没有得到运动事件,除非我改变types为TYPE_SYSTEM_ALERT,但是inturn冻结了所有的屏幕,不能点击返回或主页button,是否有任何替代方法来做到这一点。

我刚刚尝试了下面的修改

  params.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; params.height = WindowManager.LayoutParams.MATCH_PARENT; params.width = WindowManager.LayoutParams.MATCH_PARENT; 

但我不能点击下面的button,可能是由于MATCH_PARTENT,但如果我把它设置为wrap_content,那么我不能得到点击事件。

要隐藏视图,你需要调用mWindowManager.removeView(mOverlay);

 private RelativeLayout mOverlay; private WindowManager mWindowManager; WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP; LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mOverlay = (RelativeLayout) inflater .inflate(R.layout.phasechange, null); mWindowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); mWindowManager.addView(mOverlay, params); Button b = (Button) mOverlay.findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mWindowManager.removeView(mOverlay); } }); } 

从“守则”来看,总是很忙碌,有点难以pipe理。 所以也可以在布局中添加button,并根据您的滑动使其可见和不可见。

并使用下面的代码来获取手势

 public class MyGestureDetector implements OnGestureListener { @Override public boolean onDown(MotionEvent arg0) { return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float arg3) { return setCurrentSwipeTab(e1, e2, velocityX, arg3); } @Override public void onLongPress(MotionEvent arg0) { } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } } public boolean setCurrentSwipeTab(MotionEvent e1, MotionEvent e2, float velocityX, float arg3) { try { if (Math.abs(e1.getY() - e2.getY()) > Util.SWIPE_MAX_OFF_PATH) return false; // Left to right swipe of the user detected if (e1.getX() - e2.getX() > Util.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > Util.SWIPE_THRESHOLD_VELOCITY) { } // Nothing to do since you are at the Last tab!! it's task // of the left swipe now } } else if (e2.getX() - e1.getX() > Util.SWIPE_MIN_DISTANCE && Math.abs(velocityX) > Util.SWIPE_THRESHOLD_VELOCITY) { } catch (Exception e) { e.printStackTrace(); } return false; } 

你的这个任务的方法是非常不寻常的。 简单地添加一个新的视图就可以处理好任务。

添加以下代码:

 RelativeLayout layout = new RelativeLayout(this); Button b = new Button(this); b.setBackgroundColor(Color.LTGRAY); b.setText("Click to Go"); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_IN_PARENT); b.setLayoutParams(params); layout.addView(b); //get a reference to your main layout: ViewGroup myLayout = findViewById(R.id.whateverMayLayoutIdIs); layout.addView(layout); 

要隐藏视图,您可以将其从布局中删除,或更改其可见性。

你正在处理的方法非常棘手,而其他处理触摸问题。 示例包括Android:Multi touch和TYPE_SYSTEM_OVERLAY以及TYPE_SYSTEM_OVERLAY中的GestureDetector 。 试试这个谷歌search更多。

使用窗口typesTYPE_PHONE代替TYPE_SYSTEM_OVERLAY ,同时使用FLAG_NOT_FOCUSABLE标志。