【问题标题】:How to receive events from OnGestureListener如何从 OnGestureListener 接收事件
【发布时间】:2014-10-08 01:09:01
【问题描述】:

我创建了一个实现 GestureDetector.OnGestureListener 的类。但是,我不知道如何调用这些方法。是否有类似 view.setOnTouchListener() 的 GestureListener 允许您接收事件?提前谢谢你。这是我的课

公共类 GestureHandler 实现 GestureDetector.OnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int direction = -1;

@Override
public boolean onDown(MotionEvent e) {
    return true;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@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 event1, MotionEvent event2,
                       float velocityX, float velocityY) {
    //Left Swipe
    if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE &&
            Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        direction = 270;
        Log.d("Swiped: ", direction + "");
        return true;
        //Right Swipe
    } else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE &&
            Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        direction = 90;
        Log.d("Swiped: ", direction + "");
        return true;
    }
    //Up Swipe
    if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE &&
            Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
        direction = 0;
        Log.d("Swiped: ", direction + "");
        return true;
        //Down Swipe
    } else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE &&
            Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
        direction = 180;
        Log.d("Swiped: ", direction + "");
        return true;
    }
    direction = -1;
    Log.d("Swiped: ", direction + "");
    return false;
}

}

【问题讨论】:

    标签: java android ontouchlistener onfling


    【解决方案1】:

    这里非常简单的解决方案。 :)

    这是我在项目中使用 Google Glass 时的做法 -

    我实际上并没有扩展 GestureDetector。我有这样的事情:

    public class EXAMPLE {
        private GestureDetector gestureDetector;
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            gestureDetector = createGestureDetector(this);
    
        }
        private GestureDetector createGestureDetector(Context context) {
            GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
                @Override
                public boolean onDown(MotionEvent motionEvent) {
                    return false;
                }
    
                @Override
                public void onShowPress(MotionEvent motionEvent) {
                   return false;
                }
    
                @Override
                public boolean onSingleTapUp(MotionEvent motionEvent) {
                   return false;
                }
                @Override
                public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
                    return false;
                }
                @Override
                public void onLongPress(MotionEvent motionEvent) {
                }
                @Override
                public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 
                 return false;
                }
            });
            return gestureDetectorTemp;
        }
    
        @Override
        public boolean onGenericMotionEvent(MotionEvent event) {
            if (gestureDetector != null) {
                return gestureDetector.onTouchEvent(event);
            }
            return false;
        }
    }
    

    最后一部分非常重要。在任何通用运动事件上​​,如果gestureDetector 不为空,您将通过gestureDetector 发送事件以进行处理。

    请记住,您需要了解返回 false 的内容;并返回真;事情的意思。如果您返回 false,那么这意味着该事件没有被消费。如果返回 true,则事件被消耗。换句话说,如果您返回 true,则不会激活其他任何内容,因为该事件会被“吞噬”,但如果您返回 false,这会将事件发送到其他函数,这些函数可能会在执行操作时执行某些操作。

    我希望这会有所帮助。祝你好运:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 2015-12-11
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多