android android 判斷是否滑動

來源:互聯網
上載者:User

標籤:

(轉自: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 判斷是否滑動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.