我在我的简单安卓应用程序中创建了一个简单的[View.OnTouchListener][1]实现。
我现在的目标是让它探测到向下滑动。例如,如果我向下滑动,我希望它检测到我已经这样做了。
它成功地做到了这一点,然而,每次向下滑动,它也会被称为一堆其他的时间。我可以看到这一点,因为onTouch方法开关语句中的默认行为:
我是MySwipeListener
public class MySwipeListener implements View.OnTouchListener{
private Activity activity;
public MySwipeListener(Activity activity){
this.activity = activity;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN: {
Log.i("MotionEvent", "Action Down!");
Toast.makeText(activity, "Motion Down", Toast.LENGTH_LONG);
return true;
}
default: {
Log.i("MotionEvent", "Other Action!");
Toast.makeText(activity, "Other Action", Toast.LENGTH_LONG);
return true;
}
}
}
}这就是我在我的MainActivity中使用它的方式
Button topUp = (Button) findViewById(R.id.a);
topUp.setOnTouchListener(new MySwipeListener(this));当我在Android屏幕上实际滑动时,我得到的结果如下:
09-16 16:39:48.276 26748-26748/mobi.corp.proj I/MotionEvent﹕ Action Down!
09-16 16:39:48.316 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.326 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.346 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.361 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.376 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.396 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.411 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.426 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.446 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.461 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.476 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.496 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.511 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.526 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.546 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.561 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.576 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.596 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.611 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.626 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.631 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.886 26748-26748/mobi.corp.proj I/MotionEvent﹕ Action Down!
09-16 16:39:48.911 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.926 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.946 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.961 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.976 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:48.996 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.011 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.026 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.046 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.061 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.076 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.096 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.111 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.126 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.146 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.161 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.176 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.196 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.201 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!
09-16 16:39:49.206 26748-26748/mobi.corp.proj I/MotionEvent﹕ Other Action!当我期待着几个
I/MotionEvent﹕ Action Down!发布于 2014-09-16 13:55:44
default中的日志正在打印,因为当您在屏幕上滑动时,调用了ACTION_MOVE,可能还有ACTION_UP。
如果你想检测到向下滑动,你需要得到y在ACTION_DOWN上的协调,并将它与你在ACTION_UP中得到的y协调进行比较。
如果ACTION_UP中的ACTION_UP大于ACTION_UP,则存在向下滑动。
发布于 2014-09-16 13:52:39
每次移动手指时都会调用MotionEvent.ACTION_MOVE,这是一个连续的任务。这将被称为它移动的每一个像素。因此,您可能希望将MotionEvent.ACTION_MOVE排除在这个类之外,以减少噪声。
发布于 2014-09-16 13:54:03
您可以尝试一个方法,如在本教程中看到的,以处理刷。
例如:
// Private class for gestures
private class SwipeGestureDetector
extends SimpleOnGestureListener {
// Swipe properties, you can change it to make the swipe
// longer or shorter and speed
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
float diffAbs = Math.abs(e1.getY() - e2.getY());
float diff = e1.getX() - e2.getX();
if (diffAbs > SWIPE_MAX_OFF_PATH)
return false;
// Left swipe
if (diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
YourActivity.this.onLeftSwipe();
// Right swipe
} else if (-diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
YourActivity.this.onRightSwipe();
}
} catch (Exception e) {
Log.e("YourActivity", "Error on gestures");
}
return false;
}
}
private void onLeftSwipe() {
// Do something
}
private void onRightSwipe() {
// Do something
}https://stackoverflow.com/questions/25870543
复制相似问题