给一个View添加OnTouchListener来判断点击的是上下左右

class MyOnTouchListener implements OnTouchListener{

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            //上是 1 左是0下是2右是3
            float x = event.getX() / v.getWidth();
            float y = event.getY() / v.getHeight();

            // Direction will be [0,1,2,3] depending on quadrant
            int direction = 0;
            direction = (x > y) ? 1 : 0;
            direction |= (x > 1 - y) ? 2 : 0;
//            showMsg(""+direction);
            return false;
        }
        
}
原文地址:https://www.cnblogs.com/mengxingxinqing/p/3022531.html