I created a simple [View.OnTouchListener][1]
implementation in my simple Android app.
My goal right now is to make it detect downward swiping. For example, if I swipe downward, I'd like it to detect that I've done so.
It does this succesfully, however, for each swipe downward, it also gets called a whole bunch of other times. I can see this because of the default behaviour in the onTouch
methods switch statement:
This is 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;
}
}
}
}
And this is how I'm using it in my MainActivity
:
Button topUp = (Button) findViewById(R.id.a);
topUp.setOnTouchListener(new MySwipeListener(this));
When I physically swipe on my Android screen, I get results like:
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!
when I'm just expecting a few
I/MotionEvent﹕ Action Down!
The log in the default
is being printed because when you swipe on the screen ACTION_MOVE
and probably ACTION_UP
are called.
If you want to detect a downward swipe you need to get the y
co-ordinate on ACTION_DOWN
and compare it to the y
co-ordinate you get in ACTION_UP
.
If the y
in ACTION_UP
is greater than ACTION_UP
then there is a downward swipe.
MotionEvent.ACTION_MOVE
is called every time you move your finger an it is an continuous task. this will be called every pixel it moves. so you may want to exclude MotionEvent.ACTION_MOVE
from this class to decrease the noise
You can try a method as seen in this tutorial to handle swipes.
Ex:
// 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.