Java 检测视图上的滑动手势和触摸事件
Android Studio,JavaJava 检测视图上的滑动手势和触摸事件,java,android,android-studio,touch,swipe,Java,Android,Android Studio,Touch,Swipe,Android Studio,Java public class OnSwipeTouchListener implements View.OnTouchListener { private final GestureDetector gestureDetector; public OnSwipeTouchListener(Context context) { gestureDetector = new GestureDetector(context, new
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
是否有一种方法可以在单视图上检测触碰、触碰释放和滑动手势?我有一个按钮,我需要能够在上面收听所有这些内容。
我正在寻找一种解决方案,侦听器/检测器最终会调用这些方法,这样我就可以轻松地使用它们:
public void buttonPressedDown() {
}
public void buttonReleased() {
}
public void swipeUp() {
}
public void swipeRight() {
}
public void swipeDown() {
}
public void swipeLeft() {
}
在开发ios应用程序时,在xcode中很容易做到这一点,但我似乎找不到在android上实现这一点的方法
额外信息:
这就是我尝试过的:
timerButton.setOnTouchListener(new View.OnTouchListener() { //Detecting a press down
@Override
public boolean onTouch(View v, MotionEvent event) {
timerDown();
return false;
}
});
timerButton.setOnTouchListener(new OnSwipeTouchListener(timerActivity.this) {//Detecting swipes
public void onSwipeTop() {
showStats();
}
public void onSwipeBottom() {
showType();
}
public void onSwipeRight() {
showMenu();
}
public void onSwipeLeft() {
showTimes();
}
});
.
.
.
public void timerButtonPressed(View view) {//Detecting press releases
timerUp();
}
这看起来像是我要找的,只是没用。在同一视图上设置两个OnTouchListener时,只调用其中一个侦听器。当我测试它时,要么只有刷卡检测有效,要么只有媒体检测有效
救命强>
编辑:
我试过这个:
onswitTouchListener.java
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
在我的活动中:
timerButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(timerActivity.this);
boolean result = onSwipeTouchListener.onTouch(v, event);
// check if swipe was detected:
if (result) return true;
// Otherwise, detect other types of motion event
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
timerDown();
break;
case MotionEvent.ACTION_UP:
// touch released
timerUp();
break;
}
return false;
}
});
但是我如何从这里检测到刷卡?而且,当我运行应用程序时,它甚至不再检测触摸
我也在我的活动中尝试了这一点:
timerButton.setOnTouchListener(new OnSwipeTouchListener(timerActivity.this) {
public void onSwipeLeft() {
Log.i("LEFT", "LEFT");
}
public void onSwipeRight() {
Log.i("RIGHT", "RIGHT");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
timerDown();
break;
case MotionEvent.ACTION_UP:
// touch released
timerUp();
break;
}
return false;
}
});
现在,它可以检测到触摸,但不会检测到刷卡。如果我删除了上面的@override-onTouch方法,但仍然有onswip方法,那么就会检测到滑动。这怎么这么复杂?为什么它不能同时检测到刷卡和触摸,我该如何解决呢?第二次调用
setOnTouchListener
会覆盖以前的侦听器。您可以通过以下方式检测触摸和手势:
timerButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// deliver motion event to your swipe listener
boolean result = mySwipeListener.onTouch(event);
// check if swipe was detected:
if (result) return true;
// Otherwise, detect other types of motion event
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
break;
case MotionEvent.ACTION_UP:
// touch released
break;
// etc... see this link for more motion events https://developer.android.com/reference/android/view/MotionEvent
}
return false;
}
});
下面是一个在swipe listener上实现的示例:我找到了一个解决方案
在OnSwipeTouchListener.java中:
public class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void onSwipeBottom() {
}
public void onSwipeTop() {
}
public void onDownTouch() {
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
onDownTouch();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
else if (Math.abs(diffY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
}
在活动中:
timerButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(timerActivity.this);
boolean result = onSwipeTouchListener.onTouch(v, event);
// check if swipe was detected:
if (result) return true;
// Otherwise, detect other types of motion event
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
timerDown();
break;
case MotionEvent.ACTION_UP:
// touch released
timerUp();
break;
}
return false;
}
});
要检测按钮释放,请按:(单击时)
要检测滑动并按下,请执行以下操作:
timerButton.setOnTouchListener(new OnSwipeTouchListener(timerActivity.this) {
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public void onSwipeBottom() {
}
public void onSwipeTop() {
}
public void onDownTouch() {
}
});
好主意试过了,但没有真正起作用。我需要更多的细节。主要是关于如何检测刷卡,因为现在代码只检查是否有刷卡。