日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 中触摸事件与点击事件分析

發布時間:2024/3/12 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 中触摸事件与点击事件分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

觸摸事件

兩種檢測觸摸事件的方式:

  • 設置觸摸監聽 ?setOnTouchListener

返回 true: 表示消費事件 , 可以檢測到 down/move/up 事件 返回 false: 不消費事件, 只能檢測到 down 事件
  • 重寫onTouchEvent()
必須繼承自系統的一個View,才能重寫 onTouchEvent()方法;如 MyImageView extends ImageView ,才能重寫onTouchEvent返回 true: 消費事件,能檢測到 down/move/up 事件 返回 false: 不消費事件,只能檢測到 down 事件

如果兩種觸摸事件都存在,并且都返回true,結果如何?

setOnTouchListener 有效onTouchEvent() 無效 另外:一個View能否消費事件,關鍵看 dispatchTouchEvent(),我們來看下 android-10的源碼(為什么要看這個版本的源碼呢?因為低版本的源碼中,原理性的知識點都是一樣的,高版本的源碼中,加入了非常多的邏輯判斷)?View.java事件是怎么分發的 /*** Pass the touch screen motion event down to the target view, or this* view if it is the target.** @param event The motion event to be dispatched.* @return True if the event was handled by the view, false otherwise.*/public boolean dispatchTouchEvent(MotionEvent event) {if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&mOnTouchListener.onTouch(this, event)) {return true;}return onTouchEvent(event);} 我們可以看到如果兩種方式都設置的話,每次這個if條件判斷都會走,所以 onTouchListener 可以拿到所有事件(down/move/up事件);只有onTouchListener返回false時,onTouchEvent()方法才會走,才能拿到(down/move/up)所有事件;

如何讓兩種方式同時生效呢?

setOnTouchListener ?返回false onTouchEvent() 返回true
下面我們上代碼: <?xml version="1.0" encoding="utf-8"?> <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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.xialm.touchdemo.MainActivity"><ImageViewandroid:id="@+id/iv"android:layout_width="100dp"android:layout_height="100dp"android:src="@mipmap/ic_launcher" /> </RelativeLayout> 修改返回true,所有事件都能拿到;返回false,只能拿到down事件; true時: 04-28 01:44:13.515 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 0 04-28 01:44:18.266 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:44:18.283 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:44:18.333 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:44:19.555 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 1 false時: 04-28 01:44:13.515 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 0
同理,我們創建 CustomImageView,繼承自ImageView,同時修改布局文件中的ImageView為 CustomImageView public class CustomImageView extends ImageView {private static final String TAG = "CustomImageView";public CustomImageView(Context context) {super(context);}public CustomImageView(Context context, AttributeSet attrs) {super(context, attrs);}public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/*** 返回false:只能拿到down事件* 返回true: 能拿到down/move/up事件* @param event* @return*/@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.e(TAG, "onTouchEvent: "+event.getAction() );return true; //super.onTouchEvent(event);} }同時設置,同時返回true的情況,可以看到跟我們的分析是一樣的 04-28 01:47:54.060 5005-5005/com.xialm.touchdemo E/MainActivity: onTouch: 0 04-28 01:47:54.434 5005-5005/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:47:54.450 5005-5005/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:47:54.817 5005-5005/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:47:54.850 5005-5005/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:47:55.188 5005-5005/com.xialm.touchdemo E/MainActivity: onTouch: 1
同時設置setOnTouchListener返回false,onTouchEvent()返回true
04-28 01:49:55.452 6978-6978/com.xialm.touchdemo E/MainActivity: onTouch: 0 04-28 01:49:55.452 6978-6978/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 0 04-28 01:49:58.850 6978-6978/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:49:58.850 6978-6978/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 01:49:58.866 6978-6978/com.xialm.touchdemo E/MainActivity: onTouch: 2 04-28 01:49:58.866 6978-6978/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 01:49:59.926 6978-6978/com.xialm.touchdemo E/MainActivity: onTouch: 1 04-28 01:49:59.926 6978-6978/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 1

Touch事件處理流程分析

我們自定義一個MyLinearLayout 繼承自系統的LinearLayout,然后在布局文件中包裹 CustomImageView,

public class MyLinearLayout extends LinearLayout {private static final String TAG = "MyLinearLayout";public MyLinearLayout(Context context) {super(context);}public MyLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);}public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.e(TAG, "onTouchEvent: "+event.getAction() );return true;//super.onTouchEvent(event); 返回true} }

<com.xialm.touchdemo.MyLinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ccc"android:padding="10dp"><com.xialm.touchdemo.CustomImageViewandroid:id="@+id/iv"android:layout_width="100dp"android:layout_height="100dp"android:src="@mipmap/ic_launcher" /></com.xialm.touchdemo.MyLinearLayout>
我們只研究onTouchEvent()

CustomImageView的onTouchEvent()也返回true

04-28 02:37:15.952 14101-14101/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 0 04-28 02:37:16.350 14101-14101/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:37:16.367 14101-14101/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:37:16.700 14101-14101/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:37:17.367 14101-14101/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:37:17.648 14101-14101/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 1當父View和子view都有處理觸摸事件的邏輯時,子view可以接收到所有的事件;

這是因為事件傳遞是由外向內,而事件的消費是由內向外的;

只有子View不處理觸摸事件時,父View才能處理消費事件(即CustomImageView的onTouchEvent()返回false,MyLinearLayout的onTouchEvent()返回true)
使用下面示意圖來解釋,再好不過了:

父View想要消費事件,有沒有辦法呢? 辦法是有的,只需重寫onInterceptTouchEvent()方法,返回true,把事件中斷掉,則事件就不能往下傳遞了,我們處理消費邏輯即可:
public class CustomImageView extends ImageView {...省略代碼.../*** 返回false:只能拿到down事件* 返回true: 能拿到down/move/up事件* @param event* @return*/@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.e(TAG, "onTouchEvent: "+event.getAction() );return true; //super.onTouchEvent(event);} } public class MyLinearLayout extends LinearLayout {...省略代碼...private int count = 0;@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {count++;if (count > 4) {return true;}return false; // 中斷事件}@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.e(TAG, "onTouchEvent: "+event.getAction() );return true;//super.onTouchEvent(event);} } 04-28 02:52:30.948 28332-28332/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 0 04-28 02:52:31.250 28332-28332/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:52:31.266 28332-28332/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:52:31.783 28332-28332/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 2 04-28 02:52:31.800 28332-28332/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 3 04-28 02:52:31.867 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 2 04-28 02:52:31.933 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 2 04-28 02:52:31.950 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 2 04-28 02:52:32.033 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 2 04-28 02:52:32.050 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 2 04-28 02:52:32.167 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 2 04-28 02:52:35.396 28332-28332/com.xialm.touchdemo E/MyLinearLayout: onTouchEvent: 1
由下面流程示意圖來說明:

觸摸事件與點擊事件

onTouchEvent() 與 onClickListener
點擊事件是由 down 和 up 事件組成的,系統一直檢測 up事件是否發生,如果有up事件,就會判斷 onClickListener監聽是否存在,存在就回調onClick()方法
觸摸事件和點擊事件同時存在時: 系統是怎么區分點擊事件還是觸摸事件呢?不知道你有沒有注意到我們重寫onTouchEvent()時有一句話super.onTouchEvent(event); // 在內部處理了,點擊事件的邏輯 查看android-10 View.java源碼 我們著重留意一下 up事件 /*** Implement this method to handle touch screen motion events.** @param event The motion event.* @return True if the event was handled, false otherwise.*/public boolean onTouchEvent(MotionEvent event) {final int viewFlags = mViewFlags;if ((viewFlags & ENABLED_MASK) == DISABLED) {// A disabled view that is clickable still consumes the touch// events, it just doesn't respond to them.return (((viewFlags & CLICKABLE) == CLICKABLE ||(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));}if (mTouchDelegate != null) {if (mTouchDelegate.onTouchEvent(event)) {return true;}}if (((viewFlags & CLICKABLE) == CLICKABLE ||(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {switch (event.getAction()) {case MotionEvent.ACTION_UP: // 著重研究up事件if ((mPrivateFlags & PRESSED) != 0) {// take focus if we don't have it already and we should in// touch mode.boolean focusTaken = false;if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {focusTaken = requestFocus();}if (!mHasPerformedLongPress) {// This is a tap, so remove the longpress checkif (mPendingCheckForLongPress != null) {removeCallbacks(mPendingCheckForLongPress);}// Only perform take click actions if we were in the pressed stateif (!focusTaken) {performClick(); // 執行點擊監聽回調}}if (mUnsetPressedState == null) {mUnsetPressedState = new UnsetPressedState();}if (!post(mUnsetPressedState)) {// If the post failed, unpress right nowmUnsetPressedState.run();}}break;case MotionEvent.ACTION_DOWN:mPrivateFlags |= PRESSED;refreshDrawableState();if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {postCheckForLongClick();}break;case MotionEvent.ACTION_CANCEL:mPrivateFlags &= ~PRESSED;refreshDrawableState();break;case MotionEvent.ACTION_MOVE:final int x = (int) event.getX();final int y = (int) event.getY();// Be lenient about moving outside of buttonsint slop = ViewConfiguration.get(mContext).getScaledTouchSlop();if ((x < 0 - slop) || (x >= getWidth() + slop) ||(y < 0 - slop) || (y >= getHeight() + slop)) {// Outside buttonif ((mPrivateFlags & PRESSED) != 0) {// Remove any future long press checksif (mPendingCheckForLongPress != null) {removeCallbacks(mPendingCheckForLongPress);}// Need to switch from pressed to not pressedmPrivateFlags &= ~PRESSED;refreshDrawableState();}} else {// Inside buttonif ((mPrivateFlags & PRESSED) == 0) {// Need to switch from not pressed to pressedmPrivateFlags |= PRESSED;refreshDrawableState();}}break;}return true;}return false;} /*** Call this view's OnClickListener, if it is defined.** @return True there was an assigned OnClickListener that was called, false* otherwise is returned.*/public boolean performClick() {sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);if (mOnClickListener != null) { // 點擊監聽回調不為null,就執行回調,并返回trueplaySoundEffect(SoundEffectConstants.CLICK);mOnClickListener.onClick(this);return true;}return false;}如果我們想要同時處理點擊事件和觸摸事件,super.onTouchEvent(event); 就不可少 @Overridepublic boolean onTouchEvent(MotionEvent event) {super.onTouchEvent(event);Log.e(TAG, "onTouchEvent: "+event.getAction() );return true;}04-28 03:40:08.296 8330-8330/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 0 04-28 03:40:08.392 8330-8330/com.xialm.touchdemo E/CustomImageView: onTouchEvent: 1 04-28 03:40:08.393 8330-8330/com.xialm.touchdemo E/MainActivity: onClick:




總結

以上是生活随笔為你收集整理的Android 中触摸事件与点击事件分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。