【发布时间】:2014-07-24 18:57:26
【问题描述】:
所以我创建了一个使用两个操纵杆的程序。每个操纵杆都处于相对布局中,操纵杆的拇指被困在布局内并返回我用作 X 和 Y 值的边距值。这是我的监听程序:
public boolean onTouch(View view, MotionEvent event) {
final int action = event.getAction();
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
}
case MotionEvent.ACTION_MOVE:{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
leftM = X - _xDelta;
//System.out.println("LeftM: " + leftM);
topM = Y - _yDelta;
//System.out.println("TopM: " + topM);
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
if(leftM < 0) {
leftM = 0;
} else if (leftM > 220) {
leftM= 220;
}
if(topM < 0) {
topM = 0;
} else if (topM > 220) {
topM = 220;
}
//Quadrant I
if (topM < 110 && leftM > 110 ){
x = (int) (leftM - 110);
y = (int) (110 - topM);
//Quadrant II
} else if (topM < 110 && leftM < 110) {
x = (int) (leftM - 110);
y = (int) (110 - topM);
//Quadrant III
} else if ( topM > 110 && leftM < 110) {
x = (int) (leftM - 110);
y = (int) -(topM - 110);
//Quadrant IV
} else if (topM > 110 && leftM > 110){
x = (int) (leftM - 110);
y = (int) -(topM - 110);
//Origin
} else {
layoutParams.leftMargin = leftM;
layoutParams.topMargin = topM;
}
if ((Math.pow(x, 2) + Math.pow(y, 2)) <= 12100) {
recentX = leftM;
recentY = topM;
layoutParams.topMargin = topM;
layoutParams.leftMargin = leftM;
} else{
layoutParams.leftMargin = recentX;
layoutParams.topMargin = recentY;
}
switch (view.getId()) {
case R.id.thumbL:
view.setLayoutParams(layoutParams);
joystick1.LeftY = (byte) (layoutParams.topMargin + 37);
joystick1.LeftX = (byte) (layoutParams.leftMargin + 37);
System.out.println("Left X Value: " + layoutParams.leftMargin + 37);
System.out.println("Left Y Value: " + layoutParams.topMargin + 37);
case R.id.thumbR:
view.setLayoutParams(layoutParams);
joystick1.RightX = (byte) (layoutParams.leftMargin + 37);
System.out.println(" Right X Value: " + layoutParams.leftMargin +37);
}
break;
我不得不将象限重新映射到一个正常的坐标平面,(这是if 语句的点)然后我有我的 switch 语句。我只希望每个案例在其相应的拇指被按下时运行。但是,如果我不必握住右手拇指,它会打印“Right X Value”,告诉我它进入了那个案例。有什么想法吗?
【问题讨论】:
-
您的前两个案例有额外的
{},您不会将其用于 switch 语句中的案例。 -
我刚加进去,还是进入了switch语句。 @jhobbie
-
由于我们不知道
action & MotionEvent.ACTION_MASK的值是什么,所以我们不能说它为什么会进入switch case。 -
@Shizz 你不应该添加任何东西......你应该从你的案例中删除
{}。
标签: java android switch-statement joystick