android android 判断是否滑动

(转自: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_temp02
24                                 // 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_temp02
31                                 }
32                                 
33                                 if(x_temp01<x_temp02)//向右
34                                 {
35                                         //移动了x_temp02-x_temp01
36                                 }
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             else
13                 isMove=false;
14                         break;
15                         }

 注意:对于某些手机灵敏度太高,可考虑判断

if ((Math.abs(upX - downX) > 5) || (Math.abs(upY - downY) > 5))

posted @ 2015-04-03 10:48  jenson138  阅读(2265)  评论(0编辑  收藏  举报