I have a joystick application that I am implementing and am having trouble with handling motion events. Everything works fine for the most part except that I am trying to capture an event after ACTION_MOVE. So after the user is done dragging the joystick around, I want to execute some code in a MotionEvent case. ACTION_UP does not fulfill this requirement although putting ACTION_UP after ACTION_MOVE did work, and everything functioned fine, but only in debug mode and not when actually running the program which is very strange. Here is the code in question:
public boolean onTouch(View v, MotionEvent me) {
switch(v.getId()) {
case R.id.x_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "X";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.a_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "Q";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.y_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "Y";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.b_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "B";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.start_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "START";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.select_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "SELECT";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
case R.id.rb_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "RB";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.lb_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "LB";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.rt_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "RT";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
break;
case R.id.lt_button:
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
buttonPressed = "LT";
new SendServer().execute();
break;
case MotionEvent.ACTION_UP:
break;
}
case R.id.analogStick:
switch(me.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
x = me.getRawX();
y = me.getRawY();
System.out.println(x);
System.out.println(y);
dx = x - v.getX();
dy = y - v.getY();
//direction_result = sendDirection(originX, originY, me.getRawX(), me.getRawY());
//new SendServer().execute();
break;
case MotionEvent.ACTION_MOVE:
float xx = (me.getRawX() - originX) - dx;
float yy = (me.getRawY() - originY) - dy;
float displacement = (float) Math.sqrt((xx * xx) + (yy * yy));
if (displacement < deadzoneRadius) {
v.setX(me.getRawX() - dx);
v.setY(me.getRawY() - dy);
direction_result = sendDirection(originX, originY, me.getRawX(), me.getRawY());
new SendServer().execute();
System.out.println(me.getRawX() - dx);
System.out.println(me.getRawY() - dy);
}
else {
float ratio = (float) deadzoneRadius / displacement;
float constrainedX = originX + ((me.getRawX() - originX) - dx) * ratio;
float constrainedY = originY + ((me.getRawY() - originY) - dy) * ratio;
direction_result = sendDirection(originX, originY, me.getRawX(), me.getRawY());
new SendServer().execute();
v.setX(constrainedX);
v.setY(constrainedY);
me.getActionMasked();
}
break;
case MotionEvent.ACTION_UP:
xDirection2 = me.getRawX();
yDirection2 = me.getRawY();
delta_xDirection = x - xDirection2;
delta_yDirection = y - yDirection2;
v.setX(originX);
v.setY(originY);
resetKeyboardState();
new SendServer().execute();
break;
}
break;
}
return true;
}
Also my problem seems very similar to this post, although it was never answered if this helps at all: how to detect when MotionEvent.ACTION_MOVE is finished