Wie wische ich eine Schaltfläche auf Touch-Ereignis in Android
Wie wische ich eine Taste auf Touch-Ereignis Android mit Richtungen nach links, rechts, oben, unten ...? ... Ich bin neu in Android und möchte eine Rückrufanwendung entwickeln.Bitte helfen Sie, indem Sie einige Links
callbackButton.setOnTouchListener(new OnTouchListener() {
private float currX;
private float currY;
@Override
public boolean onTouch(View v, MotionEvent event) {
float x1 = 0, x2, y1 = 0, y2, dx, dy , oldx =0,oldy=0;
String direction;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldx = event.getX();
oldy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
y2 = event.getY();
dx = x2-x1;
dy = y2-y1;
// Use dx and dy to determine the direction
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0) {
direction = "right";
Log.e("right...","moving..");
}else{
direction = "left";
Log.e("left...","moving..");
}
} else {
if(dy>0) {
direction = "down";
Log.e("down...","moving..");
currX = event.getRawX();
currY = event.getRawY();
Log.e("x=", ""+(currX-oldx));
Log.e("y=", ""+(currY-oldy));
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
marginParams.setMargins((int)(currX-oldx), (int)(currY-oldy),0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);
} else {
direction = "up";
Log.e("up...","moving..");
}
}
}
return true;
}
});
Dies ist mein Code für das Ereignis imageButton moving on touch. Aber dieser Code funktioniert nicht so, wie ich es will