给一个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;
        }
        
}

 

posted @ 2013-04-15 17:32  梦醒心晴  Views(367)  Comments(0Edit  收藏  举报