手势检测器的行为不符合预期

时间:2014-01-02 21:11:17

标签: java android swipe gesture-recognition

我正在尝试使用此处的手势检测器:https://stackoverflow.com/a/938657/1489990。这是我的代码:

public class FullScreenGallery extends Activity {
int plane;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
    setContentView(R.layout.full_screengallery);
    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    final ImageView image = (ImageView) findViewById(R.id.imagePlane);
    plane = sp.getInt("PLANE",0);
    // Gesture detection
    gestureDetector = new GestureDetector(this, new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };

    image.setOnTouchListener(gestureListener);
    Context context = this;



    }


}

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                Toast.makeText(FullScreenGallery.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                Log.d("NICK", "Left Swipe");
        switch(X){
        case 0:
            Log.d("NICK", "Left Swipe,1");
        break;

                }
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(FullScreenGallery.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                Log.d("NICK", "Right Swipe");
                switch(X){
        case 0:
            Log.d("NICK", "Right Swipe,1");
        break;
                }
            }
        } catch (Exception e) {
            // nothing
            Log.d("NICK", "Exception");
        }
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return true;    //To change body of overridden methods use File | Settings | File Templates.
    }
}
运行时

和我的日志输出:

01-02 16:02:28.059: DEBUG/NICK(454): Left Swipe
01-02 16:02:28.059: DEBUG/NICK(454): Left Swipe, 1
01-02 16:02:28.069: DEBUG/NICK(454): Exception
01-02 16:02:50.500: DEBUG/NICK(454): Left Swipe
01-02 16:02:50.500: DEBUG/NICK(454): Exception
01-02 16:02:52.362: DEBUG/NICK(454): Left Swipe
01-02 16:02:52.362: DEBUG/NICK(454): Exception
01-02 16:02:53.593: DEBUG/NICK(454): Right Swipe
01-02 16:02:53.593: DEBUG/NICK(454): Exception
01-02 16:02:54.715: DEBUG/NICK(454): Right Swipe
01-02 16:02:54.715: DEBUG/NICK(454): Exception
01-02 16:02:55.625: DEBUG/NICK(454): Right Swipe
01-02 16:02:55.625: DEBUG/NICK(454): Exception
01-02 16:02:56.336: DEBUG/NICK(454): Right Swipe
01-02 16:02:56.336: DEBUG/NICK(454): Right Swipe,1
01-02 16:02:56.336: DEBUG/NICK(454): Exception
01-02 16:02:57.177: DEBUG/NICK(454): Right Swipe
01-02 16:02:58.088: DEBUG/NICK(454): Right Swipe
01-02 16:02:58.959: DEBUG/NICK(454): Right Swipe

所以我明白我得到了一个例外,但我不知道为什么,或者如何解决它,以便除了简单的吐司之外我可以在滑动上做点什么。关于可能导致这种情况的任何建议?

0 个答案:

没有答案