androidジェスチャー:GestureDetector


android.view.GestureDetector
Detects various gestures and events using the supplied MotionEvent s. The OnGestureListener callback will notify users when a particular motion event has occurred. This class should only be used with MotionEvent s reported via touch (don't use for trackball events). To use this class:
  • Create an instance of the GestureDetector for your View
  • In the View.onTouchEvent(MotionEvent) method ensure you call onTouchEvent(MotionEvent) . The methods defined in your callback will be executed when the events occur.

  • boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    @
    Override
    Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent . The calculated velocity is supplied along the x and y axis in pixels per second.
    Specified by:
    onFling(...)
    in
    OnGestureListener
    Parameters
    :
    e1
    The first down motion event that started the fling.
    e2
    The move motion event that triggered the current onFling.
    velocityX
    The velocity of this fling measured in pixels per second along the x axis.
    velocityY
    The velocity of this fling measured in pixels per second along the y axis.
    Returns
    :
    true if the event is consumed, else false
    GestureDetectorを作成すると、OnGestureListenerインタフェースのコールバックメソッドが実装されます.
    GestureDetector gestureDetector = new GestureDetector(getActivity(),
            new GestureDetector.OnGestureListener() {
                                                                                                                                       
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }
                                                                                                                                       
        @Override
        public void onShowPress(MotionEvent e) {
                                                                                                                                           
        }
                                                                                                                                       
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }
                                                                                                                                       
        @Override
        public void onLongPress(MotionEvent e) {
                                                                                                                                           
        }
                                                                                                                                       
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Logger.i(TAG, "++onFling++");
            Logger.i(TAG, "e1.getAction():"+e1.getAction()+", e1.getX():"+e1.getX()+", e1.getY():"+e1.getY());
            Logger.i(TAG, "e2.getAction():"+e2.getAction()+", e2.getX():"+e2.getX()+", e2.getY():"+e2.getY());
            Logger.i(TAG, "velocityX:"+velocityX+", velocityY:"+velocityY);
            return true; //     true
        }
                                                                                                                                       
        @Override
        public boolean onDown(MotionEvent e) {
            return false;
        }
    });

    onTouchEventメソッドでGestureDetectorを呼び出す.onTouchEventメソッド.
    class OnProgramTouchListener implements View.OnTouchListener {
                                                 
        float xDown;
        float xLast;
        float xCurrent;
        float xUp;
        float xDistance;
        int newLeftMargin;
        FrameLayout.LayoutParams params = null;
                                                 
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Logger.i(TAG, "++programTouchListener.onTouch++");
            gestureDetector.onTouchEvent(event);

    OnProgramTouchListenerのインスタンスオブジェクトを作成し、onActivity Createdメソッドで使用します.
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        programListFragment.getView().setOnTouchListener(programTouchListener);
    }
    View.OnTouchListener programTouchListener = new OnProgramTouchListener();

    onFlingメソッドで印刷するログでは、onFlingメソッドのgetAction()の値がMotionEventに等しいことがわかります.ACTION_UP時に呼び出されます.
    11-01 14:02:25.057: D/WatchTvFragment(20176): ++programTouchListener.onTouch++
    11-01 14:02:25.057: D/WatchTvFragment(20176): event.getAction():2
    11-01 14:02:25.057: D/WatchTvFragment(20176): leftMargin:337
    11-01 14:02:25.057: D/WatchTvFragment(20176): ++programTouchListener.onTouch++
    11-01 14:02:25.057: D/WatchTvFragment(20176): ++onFling++
    11-01 14:02:25.057: D/WatchTvFragment(20176): e1.getAction():0, e1.getX():155.7273, e1.getY():503.875
    11-01 14:02:25.057: D/WatchTvFragment(20176): e2.getAction():1, e2.getX():206.13757, e2.getY():487.01965
    11-01 14:02:25.067: D/WatchTvFragment(20176): velocityX:604.2696, velocityY:-86.63345
    11-01 14:02:25.067: D/WatchTvFragment(20176): event.getAction():1

    ログ内getAction()のオプション値は、ACTION_UP(1), ACTION_MOVE(2), ACTION_DOWN(0).