日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android的事件機制

Android的事件機制

來源:程序員人生   發布時間:2016-11-11 08:31:08 閱讀次數:3636次

Android的事件機制

1、理論概述

最基本的操作類型:

  • down 手指按下
  • move 手指在屏幕上移動
  • up 手指從屏幕上離開

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

對屏幕的任1操作,系統都會產生1個MotionEvent對象來對應這個對象。

注:點擊和長按可以同時滿足,如果只想滿足長按,則讓長按的監聽返回true。點擊和長按時可以move。

2、相干API

1、MotionEvent:觸發事件

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

2、Activity

  • boolean dispatchTouchEvent(MotionEvent event) : 分發事件
  • boolean onTouchEvent(MotionEvent event) : 處理事件的回調(當沒有子View消費時才調用該方法)

3、View

  • boolean dispatchTouchEvent(MotionEvent event) : 分發事件(沒有子view,用來決定是使用onTouchEvent還是setOnTouchListener)
  • boolean onTouchEvent(MotionEvent event) : 處理事件的回調方法
  • void setOnTouchListener(OnTouchListener listener) : 設置事件監聽器
  • void setOnClickListener(OnClickListener l)
  • void setOnLongClickListener(OnClickListener l)
  • void setOnCreateContextMenuListener(OnCreateContextMenuListener l) 用于創建菜單監聽

4、ViewGroup

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

注:這里引入兩個概念:處理和消費
只要調用了方法就叫做處理了;
只有返回了true才叫消費了;

事件對象被系統創建后,首先會調用對應Activity的dispatchTouchEvent()進行分發;

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

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

  • 當前事件的消費者只是決定了下1個事件優先交給他處理

  • 每一個事件都需要有1個消費者

例子:

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*/; } }

運行分析:
運行結果

如果點擊圖片并滑動,則

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

如果此時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; } });

運行結果以下:

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

可見,沒有履行MyImageview的onTouchEvent(MotionEvent event)方法了。

如果改成手指按下時返回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; } });

結果為:

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

可見down的時候,事件對象是給OnTouchListener消費了;
move和up時,事件對象給OnTouch消費了。

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 久久成人国产精品入口 | 国产一区二区三区免费观看网站上 | 国产精品精品久久久 | av一区二区不卡 | 国产精品日韩三级 | 久久免费国产精品 | 性毛片视频 | 天天看夜夜操 | 欧美特黄一级 | 亚洲综合无码一区二区 | 91成人在线播放 | 在线免费视频日韩 | 久久精品视频网站 | 一区二区日韩精品 | 午夜伦情电午夜伦情电影如如视频 | 九九精品视频在线 | 欧美性猛交xxxx乱大交蜜桃 | 欧美日韩免费在线观看 | 亚洲国产日韩在线 | 麻豆久久久久久 | 国产一区二区免费看 | 国产在视频一区二区三区吞精 | 欧美少妇一级片 | 国内精品久久久久久久97牛牛 | 久久国产精品一区二区 | 午夜激情视频在线 | 精品在线免费视频 | 精久久久久久 | 国产精品久久久久久影视 | 99免费精品 | 波多野结衣的网站 | 国产二区三区 | 美女网站黄免费 | 99免费精品| 国产精品久久久久久久久久免费看 | 国产精品久久免费视频 | 中文欧美日韩 | 99视频网| 国产亚洲精品久久久优势 | 久久亚洲国产精品 | 91精品国产自产91精品 |