如何在按下按钮时发送单个数据而不在内部循环并在释放按钮时停止操作
How do you send single data when the button is pressed and not looping on the inside and stop the action when the button is release
所以我正在使用 java 在我的移动应用程序中开发一个控制器,目前每当我触摸按钮时,它都会连续发送 3 个数据。当它应该只发送一个数据和另一个数据时,当按钮被释放时
我可以设置在释放按钮时获取数据,但不准确,因为传送了很多数据
upbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = 0.6;
nav_ctrl.angular.z = 0;
return true;
} else if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
return false;
}
});
downbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_MOVE) {
nav_ctrl.linear.x = -0.6;
nav_ctrl.angular.z = 0;
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
});
leftbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = 1.2;
nav_ctrl.angular.z = 0;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
leftbtn.performClick();
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
});
rightbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = -1.2;
nav_ctrl.angular.z = 0;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
});
}
我希望只发送一行数据,例如:"angular":{"x":0.0,"y":0.0,"z":0.0}, "linear":{"x":-0.6,"y":0.0,"z":0.0}},
输出类似,但传送了多个数据
问题在于您正在发布,而不考虑事件的类型。
您至少在 touch
开始时发布一次,在 touch
结束时至少发布一次,然后再次移动。还没有检查过,但如果你按住按钮它也可能会继续发射。
将Publish
移动到ACTION_DOWN
区块
public boolean onTouch(View v, MotionEvent event) {
//rosbridge.Publish("/set_velocity", nav_ctrl); <--- This
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = 1.2;
nav_ctrl.angular.z = 0;
rosbridge.Publish("/set_velocity", nav_ctrl); <--- Goes here
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
leftbtn.performClick();
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
所以我正在使用 java 在我的移动应用程序中开发一个控制器,目前每当我触摸按钮时,它都会连续发送 3 个数据。当它应该只发送一个数据和另一个数据时,当按钮被释放时
我可以设置在释放按钮时获取数据,但不准确,因为传送了很多数据
upbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = 0.6;
nav_ctrl.angular.z = 0;
return true;
} else if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
return false;
}
});
downbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_MOVE) {
nav_ctrl.linear.x = -0.6;
nav_ctrl.angular.z = 0;
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
});
leftbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = 1.2;
nav_ctrl.angular.z = 0;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
leftbtn.performClick();
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
});
rightbtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
rosbridge.Publish("/set_velocity", nav_ctrl);
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = -1.2;
nav_ctrl.angular.z = 0;
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}
});
}
我希望只发送一行数据,例如:"angular":{"x":0.0,"y":0.0,"z":0.0}, "linear":{"x":-0.6,"y":0.0,"z":0.0}},
输出类似,但传送了多个数据
问题在于您正在发布,而不考虑事件的类型。
您至少在 touch
开始时发布一次,在 touch
结束时至少发布一次,然后再次移动。还没有检查过,但如果你按住按钮它也可能会继续发射。
将Publish
移动到ACTION_DOWN
区块
public boolean onTouch(View v, MotionEvent event) {
//rosbridge.Publish("/set_velocity", nav_ctrl); <--- This
if(event.getAction() == ACTION_DOWN) {
nav_ctrl.linear.x = 1.2;
nav_ctrl.angular.z = 0;
rosbridge.Publish("/set_velocity", nav_ctrl); <--- Goes here
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
leftbtn.performClick();
nav_ctrl.linear.x = 0;
nav_ctrl.angular.z = 0;
}
return false;
}