Android onTouch() 方法不起作用(滑动检测)
Android onTouch() method doesn't work (swipe detection)
我正在 Android 使用 Android Studio 2.3.2
编写 Snake
为了移动蛇,我制作了一个 onTouchListener 来检测用户是否在滑动以及朝哪个方向滑动(北、南、东、西)
下面是View.onTouchListener的onTouch方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = event.getX();
prevY = event.getY();
break;
case MotionEvent.ACTION_UP:
float newX = event.getX();
float newY = event.getY();
//Calculates where we swiped
if (Math.abs(newX - prevX) > Math.abs(newY - prevY)) {
//LEFT - RiGHT Direction
if( newX > prevX) {
//RIGHT
gameEngine.updateDirection(Direction.East);
} else {
//LEFT
gameEngine.updateDirection(Direction.West);
}
} else {
// UP-DOWN Direction
if (newY > prevY) {
//DOWN
gameEngine.updateDirection(Direction.South);
} else {
//UP
gameEngine.updateDirection(Direction.North);
}
}
break;
}
return false;
}
问题是它只检测左和右(所以东和西)。
而且我不知道为什么没有检测到 UP 和 DOWN。
您必须在 ACTION_DOWN 之后 return "True"。这告诉 Android 监视 ACTION_UP 事件。
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = event.getX();
prevY = event.getY();
return true;
试试下面的代码。它对我有用。
Java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MainSnake extends AppCompatActivity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_snake);
findViewById(R.id.main_view).setOnTouchListener(this);
}
float prevX, prevY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = event.getX();
prevY = event.getY();
return true;
case MotionEvent.ACTION_UP:
float newX = event.getX();
float newY = event.getY();
//Calculates where we swiped
if (Math.abs(newX - prevX) > Math.abs(newY - prevY)) {
//LEFT - RiGHT Direction
if( newX > prevX) {
//RIGHT
Log.i("TOUCH INFO", "Right");
} else {
//LEFT
Log.i("TOUCH INFO", "Left");
}
} else {
// UP-DOWN Direction
if (newY > prevY) {
//DOWN
Log.i("TOUCH INFO", "Down");
} else {
//UP
Log.i("TOUCH INFO", "Up");
}
}
break;
}
return false;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_view">
</android.support.constraint.ConstraintLayout>
我正在 Android 使用 Android Studio 2.3.2
编写 Snake为了移动蛇,我制作了一个 onTouchListener 来检测用户是否在滑动以及朝哪个方向滑动(北、南、东、西)
下面是View.onTouchListener的onTouch方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = event.getX();
prevY = event.getY();
break;
case MotionEvent.ACTION_UP:
float newX = event.getX();
float newY = event.getY();
//Calculates where we swiped
if (Math.abs(newX - prevX) > Math.abs(newY - prevY)) {
//LEFT - RiGHT Direction
if( newX > prevX) {
//RIGHT
gameEngine.updateDirection(Direction.East);
} else {
//LEFT
gameEngine.updateDirection(Direction.West);
}
} else {
// UP-DOWN Direction
if (newY > prevY) {
//DOWN
gameEngine.updateDirection(Direction.South);
} else {
//UP
gameEngine.updateDirection(Direction.North);
}
}
break;
}
return false;
}
问题是它只检测左和右(所以东和西)。 而且我不知道为什么没有检测到 UP 和 DOWN。
您必须在 ACTION_DOWN 之后 return "True"。这告诉 Android 监视 ACTION_UP 事件。
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = event.getX();
prevY = event.getY();
return true;
试试下面的代码。它对我有用。
Java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MainSnake extends AppCompatActivity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_snake);
findViewById(R.id.main_view).setOnTouchListener(this);
}
float prevX, prevY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = event.getX();
prevY = event.getY();
return true;
case MotionEvent.ACTION_UP:
float newX = event.getX();
float newY = event.getY();
//Calculates where we swiped
if (Math.abs(newX - prevX) > Math.abs(newY - prevY)) {
//LEFT - RiGHT Direction
if( newX > prevX) {
//RIGHT
Log.i("TOUCH INFO", "Right");
} else {
//LEFT
Log.i("TOUCH INFO", "Left");
}
} else {
// UP-DOWN Direction
if (newY > prevY) {
//DOWN
Log.i("TOUCH INFO", "Down");
} else {
//UP
Log.i("TOUCH INFO", "Up");
}
}
break;
}
return false;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_view">
</android.support.constraint.ConstraintLayout>