所以我创建了一个使用两个操纵杆的程序。每个操纵杆都处于相对布局中,操纵杆的拇指被困在布局内,并返回我用作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语句。我只希望每个案例在按下相应的拇指的情况下运行。然而,如果我不必握住我的右手拇指,它将打印出#X; Right X Value&#34;告诉我它进入了那个案子。有什么想法吗?
答案 0 :(得分:2)
你可以使用break。
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);
**break;**
case R.id.thumbR:
view.setLayoutParams(layoutParams);
joystick1.RightX = (byte) (layoutParams.leftMargin + 37);
System.out.println(" Right X Value: " + layoutParams.leftMargin +37);
**break;**
}
答案 1 :(得分:0)
如果您知道操纵杆占用的屏幕空间,您是否不能根据X和Y坐标检查它们?