在下面发布的代码中,我尝试使用onDown
和onFling
检测滑动但是当我运行应用时,什么都没有出现。
当我按住触摸屏幕并快速将手指拖过它时,我预计会看到来自onFling
的输出,但我什么也没收到。另外,当我按下屏幕时,我希望看到输出来自onDown
,但同样没有任何显示。
请让我知道我遗失的内容或者我在onFling
上误解了onDown
的功能?!
JavaCode:
super.onCreate(savedInstanceState);
setContentView(R.layout.swipe_gesture_activivty);
mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());
mViewGestureDetector = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
};
final TextView view = (TextView) findViewById(R.id.accXLabel);
}
private class MySwipeGestureDetector extends SimpleOnGestureListener implements OnTouchListener {
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
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(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
}
}
答案 0 :(得分:1)
GestureDetector
通过将多个MotionEvent转换为特定手势来工作。为此,它必须从视图的触摸事件中接收输入。
你只是缺少
view.setOnTouchListener(mViewGestureDetector);
用于特定视图(您想要捕捉到的视图)。
答案 1 :(得分:1)
首先,我假设一个例子,layout.xml文件中的所有视图都包含在framelayout
内,并且您希望在整个framelayout
中可以检测到滑动。如果是,请将framelayout
注册到GestureDetector
类型的对象,如下面的代码所示:
FrameLayout mFL = (FrameLayout) findViewById(R.id.framelayoutID);
mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector());
mFL.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
});
关于simpleOnGestureListener
,以下是如何实施onFling
。它对我有用。:
//define these variables globally:
/* private static final int MIN_DIST = 100;
private static final int MAX_OFF_PATH = 200;
private static final int THRESHOLD_VELOC = 200;*/
...
...
...
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
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) {
Log.i("SwipeGesture", "Left Swipe");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("SwipeGesture", "Right Swipe");
}
} catch (Exception e) {
// nothing
}
return false;
}
}