Android activity和view判断滑动

       实例代码:

//手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2)
 float x1 = 0;
 float x2 = 0;
 float y1 = 0;
 float y2 = 0;

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 //继承了Activity的onTouchEvent方法,直接监听点击事件
 if(event.getAction() == MotionEvent.ACTION_DOWN) {
 //当手指按下的时候
 x1 = event.getX();
 y1 = event.getY();
 }
 if(event.getAction() == MotionEvent.ACTION_UP) {
 //当手指离开的时候
 x2 = event.getX();
 y2 = event.getY();
 if(y1 - y2 > 50) {
 Toast.makeText(MainActivity.this, "向上滑", Toast.LENGTH_SHORT).show();
 } else if(y2 - y1 > 50) {
 Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show();
 } else if(x1 - x2 > 50) {
 Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show();
 } else if(x2 - x1 > 50) {
 Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show();
 }
 }
 return super.onTouchEvent(event);
 }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

02-02 13:59