欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android中dispatchTouchEvent的作用是什么

今天就跟大家聊聊有關Android中dispatchTouchEvent的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

創(chuàng)新互聯(lián)成都網站建設按需制作網站,是成都網站設計公司,為混凝土攪拌站提供網站建設服務,有成熟的網站定制合作流程,提供網站定制設計服務:原型圖制作、網站創(chuàng)意設計、前端HTML5制作、后臺程序開發(fā)等。成都網站營銷推廣熱線:028-86922220

一個最簡單的屏幕觸摸動作觸發(fā)了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP

當屏幕中包含一個ViewGroup,而這個ViewGroup又包含一個子view,這個時候android系統(tǒng)如何處理Touch事件呢?到底是 ViewGroup來處理Touch事件,還是子view來處理Touch事件呢?這個并不一定。為什么呢?看看下面的調查結果就明白了。

android系統(tǒng)中的每個View的子類都具有下面三個和TouchEvent處理密切相關的方法:

1)public boolean dispatchTouchEvent(MotionEvent ev)  這個方法用來分發(fā)TouchEvent

2)public boolean onInterceptTouchEvent(MotionEvent ev) 這個方法用來攔截TouchEvent

3)public boolean onTouchEvent(MotionEvent ev) 這個方法用來處理TouchEvent

當TouchEvent發(fā)生時,首先Activity將TouchEvent傳遞給最頂層的View, TouchEvent***到達最頂層  view 的 dispatchTouchEvent ,然后由  dispatchTouchEvent  方法進行分發(fā),如果dispatchTouchEvent返回true  ,則交給這個view的onTouchEvent處理,如果dispatchTouchEvent返回 false ,則交給這個 view 的  interceptTouchEvent 方法來決定是否要攔截這個事件,如果 interceptTouchEvent 返回 true  ,也就是攔截掉了,則交給它的 onTouchEvent 來處理,如果 interceptTouchEvent 返回 false ,那么就傳遞給子  view ,由子 view 的 dispatchTouchEvent 再來開始這個事件的分發(fā)。如果事件傳遞到某一層的子 view 的  onTouchEvent 上了,這個方法返回了 false ,那么這個事件會從這個 view 往上傳遞,都是 onTouchEvent  來接收。而如果傳遞到最上面的 onTouchEvent 也返回 false 的話,這個事件就會“消失”,而且接收不到下一次事件。

通過語言描述這個處理邏輯很抽象,下面就用代碼來具體說明一下。

layout配置文件 main.xml:

<?xml version="1.0" encoding="utf-8"?> <test.lzqdiy.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:gravity="center" >        <test.lzqdiy.MyTextView             android:layout_width="200px"             android:layout_height="200px"             android:id="@+id/tv"             android:text="lzqdiy"             android:textSize="40sp"             android:textStyle="bold"             android:background="#FFFFFF"             android:textColor="#0000FF"/> </test.lzqdiy.MyLinearLayout>

節(jié)點層次很簡單,一個LinearLayout中添加了一個TextView。

下面是java代碼:

package test.lzqdiy;  import android.app.Activity; import android.os.Bundle;  public class TestTouchEventApp extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);     } } package test.lzqdiy;  import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.LinearLayout;  public class MyLinearLayout extends LinearLayout {     private final String TAG = "MyLinearLayout";      public MyLinearLayout(Context context, AttributeSet attrs) {          super(context, attrs);          Log.d(TAG, TAG);      }      @Override     public boolean dispatchTouchEvent(MotionEvent ev) {         int action = ev.getAction();          switch (action) {          case MotionEvent.ACTION_DOWN:              Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");              break;          case MotionEvent.ACTION_MOVE:              Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");              break;          case MotionEvent.ACTION_UP:              Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");              break;          case MotionEvent.ACTION_CANCEL:              Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");              break;          }         return super.dispatchTouchEvent(ev);     }      @Override     public boolean onInterceptTouchEvent(MotionEvent ev) {          int action = ev.getAction();          switch (action) {          case MotionEvent.ACTION_DOWN:              Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");              break;          case MotionEvent.ACTION_MOVE:              Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");              break;          case MotionEvent.ACTION_UP:              Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");              break;          case MotionEvent.ACTION_CANCEL:              Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");              break;          }          return false;      }      @Override     public boolean onTouchEvent(MotionEvent ev) {          int action = ev.getAction();          switch (action) {          case MotionEvent.ACTION_DOWN:              Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");              break;          case MotionEvent.ACTION_MOVE:              Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");              break;          case MotionEvent.ACTION_UP:              Log.d(TAG, "---onTouchEvent action:ACTION_UP");              break;          case MotionEvent.ACTION_CANCEL:              Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");              break;          }          return true;     }  }  package test.lzqdiy;  import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.TextView;  public class MyTextView extends TextView {      private final String TAG = "MyTextView";      public MyTextView(Context context, AttributeSet attrs) {          super(context, attrs);      }      @Override     public boolean dispatchTouchEvent(MotionEvent ev) {         int action = ev.getAction();          switch (action) {          case MotionEvent.ACTION_DOWN:              Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");              break;          case MotionEvent.ACTION_MOVE:              Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");              break;          case MotionEvent.ACTION_UP:              Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");              break;          case MotionEvent.ACTION_CANCEL:              Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");              break;          }         return super.dispatchTouchEvent(ev);     }      @Override     public boolean onTouchEvent(MotionEvent ev) {          int action = ev.getAction();          switch (action) {          case MotionEvent.ACTION_DOWN:              Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");              break;          case MotionEvent.ACTION_MOVE:              Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");              break;          case MotionEvent.ACTION_UP:              Log.d(TAG, "---onTouchEvent action:ACTION_UP");              break;          case MotionEvent.ACTION_CANCEL:              Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");              break;          }          return true;      }  }

為了指代方便,下面將MyLinearLayout簡稱為L,將MyTextView簡稱為 T,L.onInterceptTouchEvent=true  表示的含義為MyLinearLayout中的onInterceptTouchEvent方法返回值為true,通過程序運行時輸出的Log來說明調用 時序。

第1種情況 L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=true 輸出下面的Log:

D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_MOVE
D/MyTextView(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyTextView(11865): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_UP
D/MyTextView(11865): dispatchTouchEvent action:ACTION_UP
D/MyTextView(11865): ---onTouchEvent action:ACTION_UP

結論:TouchEvent完全由TextView處理。

第2種情況  L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=false 輸出下面的Log:

D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_UP

結論:TextView只處理了ACTION_DOWN事件,LinearLayout處理了所有的TouchEvent。

第3種情況  L.onInterceptTouchEvent=true&& L.onTouchEvent=true 輸出下面的Log:

D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): onInterceptTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_UP

結論:LinearLayout處理了所有的TouchEvent。

第4種情況  L.onInterceptTouchEvent=true&& L.onTouchEvent=false 輸出下面的Log:

D/MyLinearLayout(13452): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13452): onInterceptTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN

看完上述內容,你們對Android中dispatchTouchEvent的作用是什么有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享題目:Android中dispatchTouchEvent的作用是什么
鏈接URL:http://chinadenli.net/article30/goesso.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站設計公司網站維護云服務器網頁設計公司電子商務靜態(tài)網站

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網站建設網站維護公司