====手势的使用

来源:互联网 发布:dede上传网站源码 编辑:程序博客网 时间:2024/05/01 20:18
private GestureDetector detector;


this.detector = new GestureDetector(getContext(), new ViewGestureListener());
class ViewGestureListener implements GestureDetector.OnGestureListener {        // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发        public boolean onDown(MotionEvent e) {            return false;        }        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {            float minMove = 120;         //最小滑动距离            float minVelocity = 0;      //最小滑动速度            float beginX = e1.getX();            float endX = e2.getX();            float beginY = e1.getY();            float endY = e2.getY();            if(beginX-endX>minMove&&Math.abs(velocityX)>minVelocity){   //左滑            }else if(endX-beginX>minMove&&Math.abs(velocityX)>minVelocity){   //右滑            }else if(beginY-endY>minMove&&Math.abs(velocityY)>minVelocity){   //上滑//                InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                imm.hideSoftInputFromWindow(addNewMember.getWindowToken(),0);               /* InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                if(imm.isActive()&&getCurrentFocus()!=null){                    if (getCurrentFocus().getWindowToken()!=null) {                        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);                    }                }*/            }else if(endY-beginY>minMove&&Math.abs(velocityY)>minVelocity){   //下滑            }            return false;        }        // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发        public void onLongPress(MotionEvent e) {        }        // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {            return false;        }        // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发        // 注意和onDown()的区别,强调的是没有松开或者拖动的状态        public void onShowPress(MotionEvent e) {        }        // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发        public boolean onSingleTapUp(MotionEvent e) {            return false;        }    }// end class ViewGestureListener
listView.setOnTouchListener(new View.OnTouchListener() {    @Override    public boolean onTouch(View v, MotionEvent event) {        return detector.onTouchEvent(event);    }});

0 0