Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 讓人又愛又恨的觸摸機制(二)

Android 讓人又愛又恨的觸摸機制(二)

編輯:關於Android編程

概述: 在上回,從原理上講解了android中觸摸機制的運行模式,那麼這次我們通過代碼來驗證一下這個結論。是驢子是馬拿出來遛一遛吧。另外,所學知識有限,如果有任何問題歡迎拍磚和交流。   補充一下,其實我寫的(一)中,存在一些問題,對於三個與觸摸機制息息相關的方法,並不是上文說的那麼容統,細分而言。只有ViewGroup才完全擁有這三個方法(onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent),而對於View並沒有onInterceptTouchEvent,其實理由很簡單,我已經是最小單位了,我還需要攔截麼?我下面已經木有東東可以接收了~   案例驅動: MainActivity.class 這裡拋出一個問題,有興趣可以回答一下:activity和View是什麼關系呢?是不是感覺兩者好像都是界面?還是另有隱情呢?   public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }   @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: System.out.println("MainActivity---dispatchTouchEvent---ACTION_DOWN"); break; case MotionEvent.ACTION_UP: System.out.println("MainActivity---dispatchTouchEvent---ACTION_UP"); break; default: break; } boolean result = super.dispatchTouchEvent(event); System.out.println("MainActivity---dispatchTouchEvent---result:"+result); return result; } } 自定義Button,作為最小View單位,是沒有onInterceptTouchEvent方法可以復寫的。其中只復寫了其他兩個方法。     public class MyButton extends Button {   public MyButton(Context context, AttributeSet attrs) { super(context, attrs); }   @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyButton---onTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyButton---onTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyButton---onTouchEvent---ACTION_UP"); return true; }   @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyButton---dispatchTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyButton---dispatchTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyButton---dispatchTouchEvent---ACTION_UP"); boolean result = super.dispatchTouchEvent(event); System.out.println("MyButton---dispatchTouchEvent---result:"+result); return result; }   } 自定義ViewGroup的子類,MyLinearLayout,可以看到它是存在onInterceptTouchEvent方法,對於其中的子View,它可以進行攔截或者不攔截。     public class MyLinearLayout extends LinearLayout {   public MyLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); }   @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyLinearLayout---onTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyLinearLayout---onTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyLinearLayout---onTouchEvent---ACTION_UP"); boolean result = super.onTouchEvent(event); System.out.println("MyLinearLayout---onTouchEvent---result:"+result); return result; }   @Override public boolean onInterceptTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyLinearLayout---onInterceptTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyLinearLayout---onInterceptTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyLinearLayout---onInterceptTouchEvent---ACTION_UP"); boolean result = super.onInterceptTouchEvent(event); result = true; System.out.println("MyLinearLayout---onInterceptTouchEvent---result:"+result); return result; }   @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyLinearLayout---dispatchTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyLinearLayout---dispatchTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyLinearLayout---dispatchTouchEvent---ACTION_UP"); boolean result = super.dispatchTouchEvent(event); System.out.println("MyLinearLayout---dispatchTouchEvent---result:"+result); return result; } } 自定義ImageView,和自定義Button相同,作為最小View單位,不存在onInterceptTouchEvent     public class MyImageView extends ImageView {   public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); }   @Override public boolean dispatchTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyImageView---dispatchTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyImageView---dispatchTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyImageView---dispatchTouchEvent---ACTION_UP"); boolean result = super.dispatchTouchEvent(event); System.out.println("MyImageView---dispatchTouchEvent---result:"+result); return result; }   @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) System.out.println("MyImageView---onTouchEvent---ACTION_DOWN"); if (action == MotionEvent.ACTION_MOVE) System.out.println("MyImageView---onTouchEvent---ACTION_MOVE"); if (action == MotionEvent.ACTION_UP) System.out.println("MyImageView---onTouchEvent---ACTION_UP"); return true; } } 最後看一下布局文件,很簡單,這裡不贅述了。     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" >   <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:textStyle="bold" android:textColor="@color/blue" android:text="@string/touch" />   <com.example.touchevent.MyButton android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_marginTop="10dp" android:text="@string/mybutton1" />   <com.example.touchevent.MyLinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/button1" android:layout_marginTop="50dp" android:background="@color/blue" android:gravity="center" >   <com.example.touchevent.MyRelativeLayout android:layout_width="200dp" android:layout_height="200dp" android:gravity="center" android:background="@color/dark_gray" > <com.example.touchevent.MyImageView android:layout_width="100dp" android:layout_height="100dp" android:scaleType="fitCenter" android:src="@drawable/jacky" /> </com.example.touchevent.MyRelativeLayout> </com.example.touchevent.MyLinearLayout>   </RelativeLayout>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved