方法1:
private boolean isOutOfBounds(Context context, MotionEvent event) {
final int x = (int) event.getX(); final int y = (int) event.getY();
final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
final View decorView = getWindow().getDecorView();
return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop)) || (y > (decorView.getHeight() + slop));
}
decorView 是指弹出来的黑色背景弹窗的居中弹窗布局
int getScaledWindowTouchSlop ()
Distance in pixels a touch must be outside the bounds of a window for it to be counted as outside the window for purposes of dismissing that window。
被认为是window与window外边界区域之间的最小距离。
方法2:
public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom() } if(event.getAction() == MotionEvent.ACTION_MOVE){ if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){ } } return false;}