Android 中触摸事件与点击事件分析
生活随笔
收集整理的這篇文章主要介紹了
Android 中触摸事件与点击事件分析
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
觸摸事件
兩種檢測(cè)觸摸事件的方式:
- 設(shè)置觸摸監(jiān)聽(tīng) ?setOnTouchListener
返回 true: 表示消費(fèi)事件 , 可以檢測(cè)到 down/move/up 事件 返回 false: 不消費(fèi)事件, 只能檢測(cè)到 down 事件
- 重寫(xiě)onTouchEvent()
如果兩種觸摸事件都存在,并且都返回true,結(jié)果如何?
setOnTouchListener 有效onTouchEvent() 無(wú)效 另外:一個(gè)View能否消費(fèi)事件,關(guān)鍵看 dispatchTouchEvent(),我們來(lái)看下 android-10的源碼(為什么要看這個(gè)版本的源碼呢?因?yàn)榈桶姹镜脑创a中,原理性的知識(shí)點(diǎn)都是一樣的,高版本的源碼中,加入了非常多的邏輯判斷)?View.java事件是怎么分發(fā)的 /*** 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);} 我們可以看到如果兩種方式都設(shè)置的話,每次這個(gè)if條件判斷都會(huì)走,所以 onTouchListener 可以拿到所有事件(down/move/up事件);只有onTouchListener返回false時(shí),onTouchEvent()方法才會(huì)走,才能拿到(down/move/up)所有事件;如何讓兩種方式同時(shí)生效呢?
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時(shí): 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時(shí): 04-28 01:44:13.515 1471-1471/com.xialm.touchdemo E/MainActivity: onTouch: 0
同理,我們創(chuàng)建 CustomImageView,繼承自ImageView,同時(shí)修改布局文件中的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);} }同時(shí)設(shè)置,同時(shí)返回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
同時(shí)設(shè)置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事件處理流程分析
我們自定義一個(gè)MyLinearLayout 繼承自系統(tǒng)的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當(dāng)父View和子view都有處理觸摸事件的邏輯時(shí),子view可以接收到所有的事件;這是因?yàn)槭录鬟f是由外向內(nèi),而事件的消費(fèi)是由內(nèi)向外的;
只有子View不處理觸摸事件時(shí),父View才能處理消費(fèi)事件(即CustomImageView的onTouchEvent()返回false,MyLinearLayout的onTouchEvent()返回true)
使用下面示意圖來(lái)解釋,再好不過(guò)了:
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
由下面流程示意圖來(lái)說(shuō)明:
觸摸事件與點(diǎn)擊事件
onTouchEvent() 與 onClickListener點(diǎn)擊事件是由 down 和 up 事件組成的,系統(tǒng)一直檢測(cè) up事件是否發(fā)生,如果有up事件,就會(huì)判斷 onClickListener監(jiān)聽(tīng)是否存在,存在就回調(diào)onClick()方法
觸摸事件和點(diǎn)擊事件同時(shí)存在時(shí): 系統(tǒng)是怎么區(qū)分點(diǎn)擊事件還是觸摸事件呢?不知道你有沒(méi)有注意到我們重寫(xiě)onTouchEvent()時(shí)有一句話super.onTouchEvent(event); // 在內(nèi)部處理了,點(diǎn)擊事件的邏輯 查看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(); // 執(zhí)行點(diǎn)擊監(jiān)聽(tīng)回調(diào)}}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) { // 點(diǎn)擊監(jiān)聽(tīng)回調(diào)不為null,就執(zhí)行回調(diào),并返回trueplaySoundEffect(SoundEffectConstants.CLICK);mOnClickListener.onClick(this);return true;}return false;}如果我們想要同時(shí)處理點(diǎn)擊事件和觸摸事件,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:
總結(jié)
以上是生活随笔為你收集整理的Android 中触摸事件与点击事件分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何满足实验室认可对不确定度评定的相关要
- 下一篇: 第30届深圳礼品展暨1688工厂直采季开