(转自:http://blog.csdn.net/angle_rupert/article/details/6255522)
声明:
1 float x_temp01 = 0.0f;2 float y_temp01 = 0.0f;3 float x_temp02 = 0.0f;4 float y_temp02 = 0.0f;
重写Activity的onTouchEvent方法:
1 @Override 2 public boolean onTouchEvent(MotionEvent event) 3 { 4 //获得当前坐标 5 float x = event.getX(); 6 float y = event.getY(); 7 8 switch(event.getAction()) 9 {10 case MotionEvent.ACTION_DOWN: 11 {12 x_temp01 = x;13 y_temp01 = y;14 }15 break;16 case MotionEvent.ACTION_UP: 17 {18 x_temp02 = x;19 y_temp02 = y;20 21 if(x_temp01!=0 && y_temp01!=0)//22 {23 // 比较x_temp01和x_temp0224 // x_temp01>x_temp02 //向左25 // x_temp01==x_temp02 //竖直方向或者没动26 // x_temp01<x_temp02 //向右27 28 if(x_temp01>x_temp02)//向左29 {30 //移动了x_temp01-x_temp0231 }32 33 if(x_temp01<x_temp02)//向右34 {35 //移动了x_temp02-x_temp0136 }37 }38 }39 break;40 case MotionEvent.ACTION_MOVE:41 {42 43 }44 break;45 46 }47 return super.onTouchEvent(event);48 }
注经自己测试可行,简单好用;
自己更改判断是否滑动:
1 case MotionEvent.ACTION_DOWN: { 2 isMove = false; 3 downX=x; 4 downY=y; 5 break; 6 } 7 case MotionEvent.ACTION_UP: { 8 upX =x; 9 upY=y;10 if ((upX-downX!=0)||(upY-downY!=0))11 isMove=true;12 else13 isMove=false;14 break;15 }
原标题:android android 判断是否滑动
关键词:Android