我有两张图片,我用作操纵杆。 MotionEvent.ACTION_DOWN
工作正常,并以正确的方向填充图像。 MotionEvent.ACTION_UP
假设将图像返回到其默认位置但不起作用。这是我的听众代码:
//First joystick listener
joystick1.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
Log.d("TouchTest", event.toString());
//Gets the X and Y values of the users touch when the user is touching
//the joystick move image
X = event.getX();
Y = event.getY();
CY = cursor.getHeight()/2;
CX = cursor.getWidth()/2;
joystick1Move = true;
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
Log.d("TouchTest", "Touch down");
//if the user has touched the image and moves finger right left
//up and down for the first joystick then move the joystick image
if(X>(joystick1width/2)&&X<(v.getWidth())){
//right
CX=CX+5;
joystick1.setPadding(20, 0, 0, 0);
}
if(X<(joystick1width/2)){
//left
CX=CX-5;
joystick1.setPadding(-20, 0, 0, 0);
}
if(Y>(joystick1height/2)&&Y<(v.getHeight())){
//up
CY=CY+5;
joystick1.setPadding(0, 0, 0, 20);
}
if(Y<(joystick1height/2)){
//down
CY=CY-5;
joystick1.setPadding(0,0, 0, -20);
}
break;
case MotionEvent.ACTION_UP:
Log.d("TouchTest", "Touch up");
joystick1.setPadding(0, 0, 0, 0);
break;
default:
Log.d("TouchTest", "Touch up");
joystick1.setPadding(0, 0, 0, 0);
}
return true;
}
});