package com.leu.gesturedetectordemo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ImageView img;
GestureDetector myGestureDetector;
class myGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX()-e2.getX()>50){
Log.d("img","right to left");
Toast.makeText(MainActivity.this,"right to left",Toast.LENGTH_LONG).show();
}else if (e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"left to right",Toast.LENGTH_LONG).show();
Log.d("img","left to right");
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img= (ImageView) findViewById(R.id.img);
myGestureDetector =new GestureDetector(new myGestureListener());
img.setOnTouchListener(new View.OnTouchListener() {
@Override//可以捕获到触摸屏幕发生的event事件
public boolean onTouch(View v, MotionEvent event) {
//转发MotionEvent对象到OnGestureListener
myGestureDetector.onTouchEvent(event);
return true;
}
});
}
}