android 如何获得最后一个动作
android how to get last movement
例如,我将手指在屏幕上向下移动,然后再向上移动。所以,这应该算作两次拖拽,在我停顿一秒钟之前的最后一个动作,以及在我重新向上移动时的最后一个动作。我基本上每次在手指不离开屏幕的情况下进行新动作时都在计数。那么,如何在不抬起手指的情况下获得停止运动之前的最后一个动作?
我正在使用动作事件。这是 action_move 中的代码:
case MotionEvent.ACTION_MOVE:
posY = event.getY();
posX = event.getX();
diffPosY = posY - oldY;
diffPosX = posX - oldX;
if (checkMovement(posY, oldY)){
if (diffPosY > 0 || diffPosY < 0){
count +=1;
}
}
public boolean checkMovement(float posY, float oldY) {
int newY = Math.round(posY);
double distance = Math.abs(newY - oldY);
oldY = newY;
if (distance < 25)
return false;
return true;
}
就这么简单
private int mLastMovY = 0;
case MotionEvent.ACTION_MOVE:
posY = event.getY();
posX = event.getX();
diffPosY = posY - oldY;
diffPosX = posX - oldX;
if(diffPosY > 0){//up
if(mLastMovY != 0){//if have any drag down before, the value will != 0
count +=1;
//could save value of mLastMovY before reset it, this is last position when user drag down
mLastMovY = 0;//reset it to avoid 'count' be increased
}
}
else{//down
mLastMovY = posY;//drag down will assign value to mLastMovY
}
例如,我将手指在屏幕上向下移动,然后再向上移动。所以,这应该算作两次拖拽,在我停顿一秒钟之前的最后一个动作,以及在我重新向上移动时的最后一个动作。我基本上每次在手指不离开屏幕的情况下进行新动作时都在计数。那么,如何在不抬起手指的情况下获得停止运动之前的最后一个动作?
我正在使用动作事件。这是 action_move 中的代码:
case MotionEvent.ACTION_MOVE:
posY = event.getY();
posX = event.getX();
diffPosY = posY - oldY;
diffPosX = posX - oldX;
if (checkMovement(posY, oldY)){
if (diffPosY > 0 || diffPosY < 0){
count +=1;
}
}
public boolean checkMovement(float posY, float oldY) {
int newY = Math.round(posY);
double distance = Math.abs(newY - oldY);
oldY = newY;
if (distance < 25)
return false;
return true;
}
就这么简单
private int mLastMovY = 0;
case MotionEvent.ACTION_MOVE:
posY = event.getY();
posX = event.getX();
diffPosY = posY - oldY;
diffPosX = posX - oldX;
if(diffPosY > 0){//up
if(mLastMovY != 0){//if have any drag down before, the value will != 0
count +=1;
//could save value of mLastMovY before reset it, this is last position when user drag down
mLastMovY = 0;//reset it to avoid 'count' be increased
}
}
else{//down
mLastMovY = posY;//drag down will assign value to mLastMovY
}