onTouchEvent 按住,持续执行
onTouchEvent hold down, continuous execution
在我的 onTouchEvent() 方法中,我想执行一个持续重复的操作,直到我将手指从屏幕上移开。这是我的代码:
public void onTouchEvent(MotionEvent event) {
synchronized (this) {
Matrix matrix = new Matrix();
float x = event.getX();
if (x >= screenWidth / 2) {
rotate += 10;
} else {
rotate -= 10;
}
matrix.postRotate(rotate, square.getWidth() / 2, square.getHeight() / 2);
position.set(matrix);
position.postTranslate(xPos, yPos);
}
return true;
}
但问题是,如果我按住手指不动,动作只会执行一次。我尝试过各种解决方案,包括
boolean actionUpFlag = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
actionUpFlag = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
actionUpFlag = false;
}
while (actionUpFlag) {
//the block of code above
}
并使操作仅在事件为 MotionEvent.ACTION_MOVE 时执行,并在 onTouchEvent() 结束时返回 false,所有这些均未成功。谁能告诉我错误是什么?
MotionEvent.ACTION_MOVE 尝试的代码块:
if (event.getAction() == MotionEvent.ACTION_MOVE) {
//block of code above
}
您是否考虑过使用 Thread
来完成此操作?
这里已经很晚了(我已经工作了 13 个小时),但这应该能为您提供要点:
WorkerThread workerThread;
public void onTouchEvent(MotionEvent event){
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
if (workerThread == null){
workerThread = new WorkerThread();
workerThread.start();
}
break;
case MotionEvent.ACTION_UP:
if (workerThread != null){
workerThread.stop();
workerThread = null;
}
break;
}
return false;
}
您的 Thread
实现可以是一个内部 class,例如:
class WorkerThread extends Thread{
private volatile boolean stopped = false;
@Override
public void run(){
super.run();
while(!stopped){
//do your work here
}
}
public void stop(){
stopped = true;
}
}
除非您想执行其他操作,否则您可能只想忽略 MotionEvent.ACTION_MOVE
。
如果您要将 UI 更新为 WorkerThread
,请确保以线程安全的方式执行此操作。
Here is a link to the Android API Guide on Processes and Threads
在我的 onTouchEvent() 方法中,我想执行一个持续重复的操作,直到我将手指从屏幕上移开。这是我的代码:
public void onTouchEvent(MotionEvent event) {
synchronized (this) {
Matrix matrix = new Matrix();
float x = event.getX();
if (x >= screenWidth / 2) {
rotate += 10;
} else {
rotate -= 10;
}
matrix.postRotate(rotate, square.getWidth() / 2, square.getHeight() / 2);
position.set(matrix);
position.postTranslate(xPos, yPos);
}
return true;
}
但问题是,如果我按住手指不动,动作只会执行一次。我尝试过各种解决方案,包括
boolean actionUpFlag = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
actionUpFlag = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
actionUpFlag = false;
}
while (actionUpFlag) {
//the block of code above
}
并使操作仅在事件为 MotionEvent.ACTION_MOVE 时执行,并在 onTouchEvent() 结束时返回 false,所有这些均未成功。谁能告诉我错误是什么?
MotionEvent.ACTION_MOVE 尝试的代码块:
if (event.getAction() == MotionEvent.ACTION_MOVE) {
//block of code above
}
您是否考虑过使用 Thread
来完成此操作?
这里已经很晚了(我已经工作了 13 个小时),但这应该能为您提供要点:
WorkerThread workerThread;
public void onTouchEvent(MotionEvent event){
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
if (workerThread == null){
workerThread = new WorkerThread();
workerThread.start();
}
break;
case MotionEvent.ACTION_UP:
if (workerThread != null){
workerThread.stop();
workerThread = null;
}
break;
}
return false;
}
您的 Thread
实现可以是一个内部 class,例如:
class WorkerThread extends Thread{
private volatile boolean stopped = false;
@Override
public void run(){
super.run();
while(!stopped){
//do your work here
}
}
public void stop(){
stopped = true;
}
}
除非您想执行其他操作,否则您可能只想忽略 MotionEvent.ACTION_MOVE
。
如果您要将 UI 更新为 WorkerThread
,请确保以线程安全的方式执行此操作。
Here is a link to the Android API Guide on Processes and Threads