I am trying to implement swipe in android from left to right and right to left. It's not working in the emulator. Sometimes it works. Can anybody tell me what the problem is, in my code? If anybody has working code, can you give it to me?
package com.gesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;
public class SampleGestureActivity extends Activity implements OnGestureListener {
public final static int SWIPE_UP = 1;
public final static int SWIPE_DOWN = 2;
public final static int SWIPE_LEFT = 3;
public final static int SWIPE_RIGHT = 4;
public final static int MODE_TRANSPARENT = 0;
public final static int MODE_SOLID = 1;
public final static int MODE_DYNAMIC = 2;
private final static int ACTION_FAKE = -13;
private int swipe_Min_Distance = 100;
private int swipe_Max_Distance = 350;
private int swipe_Min_Velocity = 100;
private int mode = MODE_DYNAMIC;
private GestureDetector gestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// Do something
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final float xDistance=Math.abs(e1.getX()-e2.getX());
final float yDistance=Math.abs(e1.getY()-e2.getY());
if(xDistance>this.swipe_Max_Distance || yDistance> this.swipe_Max_Distance)
return false;
velocityX = Math.abs(velocityX);
velocityY = Math.abs(velocityY);
boolean result=false;
if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance)
{
if(e1.getX() > e2.getX()) // right to left Move
Toast.makeText(SampleGestureActivity.this, "left", Toast.LENGTH_SHORT).show();
//this.listener.onSwipe(SWIPE_LEFT);
else
Toast.makeText(SampleGestureActivity.this, "right", Toast.LENGTH_SHORT).show();
//this.listener.onSwipe(SWIPE_RIGHT);
result=true;
}
else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance)
{
if(e1.getY() > e2.getY()) // bottom to top Move
Toast.makeText(SampleGestureActivity.this, "up", Toast.LENGTH_SHORT).show();
else
Toast.makeText(SampleGestureActivity.this, "down", Toast.LENGTH_SHORT).show();
result=true;
}
return result;
}
@Override
public void onLongPress(MotionEvent e) {
//开发者_StackOverflow中文版 Do something
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// Do something
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// Do something
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// Do something
Toast t = Toast.makeText(SampleGestureActivity.this, "single", Toast.LENGTH_SHORT);
t.show();
return true;
}
}
Thanks
May be your code is fine as it works sometimes. Try on the real device, emulator is not so good for this.
精彩评论