多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開(kāi)源 > 綜合技術(shù) > Android的事件機(jī)制

Android的事件機(jī)制

來(lái)源:程序員人生   發(fā)布時(shí)間:2016-11-11 08:31:08 閱讀次數(shù):3612次

Android的事件機(jī)制

1、理論概述

最基本的操作類型:

  • down 手指按下
  • move 手指在屏幕上移動(dòng)
  • up 手指從屏幕上離開(kāi)

觸屏操作的順序:down->move->move->…->up

對(duì)屏幕的任1操作,系統(tǒng)都會(huì)產(chǎn)生1個(gè)MotionEvent對(duì)象來(lái)對(duì)應(yīng)這個(gè)對(duì)象。

注:點(diǎn)擊和長(zhǎng)按可以同時(shí)滿足,如果只想滿足長(zhǎng)按,則讓長(zhǎng)按的監(jiān)聽(tīng)返回true。點(diǎn)擊和長(zhǎng)按時(shí)可以move。

2、相干API

1、MotionEvent:觸發(fā)事件

  • int ACTION_DOWN = 0 : 代表down
  • int ACTION_MOVE = 2 : 代表move
  • int ACTION_UP = 1 : 代表up
  • getAction() : 得到事件類型
  • getX() : 得到事件產(chǎn)生的X軸的坐標(biāo)(相對(duì)當(dāng)前視圖)
  • getRawX() : 得到事件產(chǎn)生的X軸的坐標(biāo)(相對(duì)屏幕左頂點(diǎn))
  • getY() : 得到事件產(chǎn)生的Y軸的坐標(biāo)(相對(duì)當(dāng)前視圖)
  • getRawY() : 得到事件產(chǎn)生的Y軸的坐標(biāo)(相對(duì)屏幕左頂點(diǎn))

2、Activity

  • boolean dispatchTouchEvent(MotionEvent event) : 分發(fā)事件
  • boolean onTouchEvent(MotionEvent event) : 處理事件的回調(diào)(當(dāng)沒(méi)有子View消費(fèi)時(shí)才調(diào)用該方法)

3、View

  • boolean dispatchTouchEvent(MotionEvent event) : 分發(fā)事件(沒(méi)有子view,用來(lái)決定是使用onTouchEvent還是setOnTouchListener)
  • boolean onTouchEvent(MotionEvent event) : 處理事件的回調(diào)方法
  • void setOnTouchListener(OnTouchListener listener) : 設(shè)置事件監(jiān)聽(tīng)器
  • void setOnClickListener(OnClickListener l)
  • void setOnLongClickListener(OnClickListener l)
  • void setOnCreateContextMenuListener(OnCreateContextMenuListener l) 用于創(chuàng)建菜單監(jiān)聽(tīng)

4、ViewGroup

  • boolean dispatchTouchEvent(MotionEvent event) : 分發(fā)事件
  • boolean onInterceptTouchEvent(MotionEvent event) : 攔截事件的回調(diào)方法

注:這里引入兩個(gè)概念:處理和消費(fèi)
只要調(diào)用了方法就叫做處理了;
只有返回了true才叫消費(fèi)了;

事件對(duì)象被系統(tǒng)創(chuàng)建后,首先會(huì)調(diào)用對(duì)應(yīng)Activity的dispatchTouchEvent()進(jìn)行分發(fā);

  • down在分發(fā)給視圖對(duì)象的進(jìn)程中要肯定消費(fèi)者(onTouchEvent()返回true),如果都返回false,那事件的消費(fèi)者只能是activity了。

  • 后面的move和up都將分發(fā)給消費(fèi)者(多是視圖對(duì)象,也多是消費(fèi)者)

  • 當(dāng)前事件的消費(fèi)者只是決定了下1個(gè)事件優(yōu)先交給他處理

  • 每一個(gè)事件都需要有1個(gè)消費(fèi)者

例子:

MotionEventActivity.java

package com.cwenhui.motionevent; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import com.cwenhui.test.R; /** * Created by cwenhui on 2016.02.23 */ public class MotionEventActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_motionevent); findViewById(R.id.myImageview).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.e("MotionEventActivity", "setOnTouchListener"); return false; } }); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.e("MotionEventActivity", "dispatchTouchEvent--"+ev.getAction()); return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("MotionEventActivity", "onTouchEvent--"+event.getAction()); return super.onTouchEvent(event); } }

layout_motionevent.xml:

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="motionEvent"/> <com.cwenhui.motionevent.MyImageView android:id="@+id/myImageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/test"/> </LinearLayout>

MyImageview.java

package com.cwenhui.motionevent; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.ImageView; /** * Created by cwenhui on 2016.02.23 */ public class MyImageView extends ImageView { public MyImageView(Context context) { super(context); } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); Log.e("MyImageView", "MyImageView"); } public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.e("MyImageView", "dispatchTouchEvent--"+event.getAction()); return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("MyImageView", "onTouchEvent--"+event.getAction()); return super.onTouchEvent(event)/*true*/; } }

運(yùn)行分析:
運(yùn)行結(jié)果

如果點(diǎn)擊圖片并滑動(dòng),則

09-20 03:20:37.674 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--0 09-20 03:20:37.674 25085-25085/com.cwenhui.test E/MyImageView: dispatchTouchEvent--0 09-20 03:20:37.674 25085-25085/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:20:37.674 25085-25085/com.cwenhui.test E/MyImageView: onTouchEvent--0 09-20 03:20:37.674 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--0 09-20 03:20:37.708 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:20:37.708 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--2 09-20 03:20:37.724 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:20:37.724 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--2 09-20 03:20:37.741 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:20:37.741 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--2 09-20 03:20:37.758 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:20:37.758 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--2 09-20 03:20:37.774 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:20:37.774 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--2 09-20 03:20:37.802 25085-25085/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--1 09-20 03:20:37.802 25085-25085/com.cwenhui.test E/MotionEventActivity: onTouchEvent--1

如果將MyImageview.java中的onTouchEvent(MotionEvent event)返回值改成true,則

09-20 03:22:16.122 2077-2077/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--0 09-20 03:22:16.122 2077-2077/com.cwenhui.test E/MyImageView: dispatchTouchEvent--0 09-20 03:22:16.122 2077-2077/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:22:16.122 2077-2077/com.cwenhui.test E/MyImageView: onTouchEvent--0 09-20 03:22:16.174 2077-2077/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:22:16.174 2077-2077/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 03:22:16.174 2077-2077/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:22:16.174 2077-2077/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 03:22:16.191 2077-2077/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:22:16.191 2077-2077/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 03:22:16.191 2077-2077/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:22:16.191 2077-2077/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 03:22:16.208 2077-2077/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:22:16.208 2077-2077/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 03:22:16.208 2077-2077/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:22:16.208 2077-2077/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 03:22:16.224 2077-2077/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 03:22:16.224 2077-2077/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 03:22:16.224 2077-2077/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:22:16.224 2077-2077/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 03:22:16.279 2077-2077/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--1 09-20 03:22:16.279 2077-2077/com.cwenhui.test E/MyImageView: dispatchTouchEvent--1 09-20 03:22:16.279 2077-2077/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 03:22:16.279 2077-2077/com.cwenhui.test E/MyImageView: onTouchEvent--1

如果此時(shí)MotionEventActivity中的OnTouchListener的onTouch()方法返回true,以下:

findViewById(R.id.myImageview).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.e("MotionEventActivity", "setOnTouchListener--"+event.getAction()); return true; } });

運(yùn)行結(jié)果以下:

09-20 15:01:53.068 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--0 09-20 15:01:53.068 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--0 09-20 15:01:53.068 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.105 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.105 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.105 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.122 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.122 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.122 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.138 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.139 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.139 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.155 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.155 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.155 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.172 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.172 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.172 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.189 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.189 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.189 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.237 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 15:01:53.237 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 15:01:53.237 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener 09-20 15:01:53.237 3767-3767/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--1 09-20 15:01:53.237 3767-3767/com.cwenhui.test E/MyImageView: dispatchTouchEvent--1 09-20 15:01:53.237 3767-3767/com.cwenhui.test E/MotionEventActivity: setOnTouchListener

可見(jiàn),沒(méi)有履行MyImageview的onTouchEvent(MotionEvent event)方法了。

如果改成手指按下時(shí)返回true,即

findViewById(R.id.myImageview).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.e("MotionEventActivity", "setOnTouchListener--"+event.getAction()); // return false; if (event.getAction() == MotionEvent.ACTION_DOWN) { return true; } return false; } });

結(jié)果為:

09-20 16:30:48.573 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--0 09-20 16:30:48.573 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--0 09-20 16:30:48.573 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--0 09-20 16:30:48.605 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 16:30:48.605 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 16:30:48.605 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--2 09-20 16:30:48.605 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 16:30:48.622 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 16:30:48.622 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 16:30:48.622 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--2 09-20 16:30:48.622 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 16:30:48.638 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 16:30:48.639 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 16:30:48.639 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--2 09-20 16:30:48.639 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 16:30:48.655 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 16:30:48.655 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 16:30:48.655 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--2 09-20 16:30:48.655 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 16:30:48.672 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 16:30:48.672 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 16:30:48.672 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--2 09-20 16:30:48.672 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 16:30:48.689 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--2 09-20 16:30:48.689 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--2 09-20 16:30:48.689 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--2 09-20 16:30:48.689 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--2 09-20 16:30:48.717 16591-16591/com.cwenhui.test E/MotionEventActivity: dispatchTouchEvent--1 09-20 16:30:48.717 16591-16591/com.cwenhui.test E/MyImageView: dispatchTouchEvent--1 09-20 16:30:48.718 16591-16591/com.cwenhui.test E/MotionEventActivity: setOnTouchListener--1 09-20 16:30:48.718 16591-16591/com.cwenhui.test E/MyImageView: onTouchEvent--1

可見(jiàn)down的時(shí)候,事件對(duì)象是給OnTouchListener消費(fèi)了;
move和up時(shí),事件對(duì)象給OnTouch消費(fèi)了。

生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美性暴力变态xxxx | 欧美成人h版整片合集 | 亚洲精品一区二区观看 | 久久精品在线视频 | 性欧美丨18一19 | 精品国产欧美一区二区三区成人 | 26uuu色噜噜欧美在线播放 | 欧美黑粗特黄午夜大片 | 国产精品久久久久久久久免费 | 久久久免费精品视频 | 日韩精品亚洲人成在线播放 | 一区二区精品久久 | 欧美视频 亚洲视频 | 久久免费精品国产72精品剧情 | 久久精品免视看国产明星 | 成人精品视频一区二区在线 | 最新国产福利 | 在线观看视频h | 欧美精品18videosex性欧 | 伊人手机在线观看 | 久久最新 | 欧美超清free videos | 男人懂的网站 | 噜噜片| 亚洲久久在线观看 | 国产一二三区有声小说 | 伊人网在线播放 | a久久久久一级毛片护士免费 | 免费澳门一级毛片 | 91亚洲欧美综合高清在线 | 欧美成人免费在线 | 亚洲国产日韩欧美 | 国产亚洲精品一区二区久久 | 国产美女一级做受在线观看 | 久久99爱爱 | 亚洲狠狠狠一区二区三区 | 国产精品日韩欧美一区二区三区 | 日本wwwxxxx| 中国一级毛片国产高清 | 校园春色亚洲欧美 | 福利片在线观看免费高清 |