1.废话不多说,直接上代码,如下

package com.example.mygestrue;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView; public class MainActivity extends ActionBarActivity {
GestureDetector mDetector;
TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetector = new GestureDetector(this,new GestureDetector.OnGestureListener() { @Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
} @Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub } @Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
} @Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub } @Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
} @Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
});
tv = (TextView)findViewById(R.id.tv1);
tv.setOnTouchListener(new View.OnTouchListener() { @Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Log.e("123", "ontouch");
// TODO Auto-generated method stub
return false;
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.e("123","ontouchevent");
return super.onTouchEvent(event);
};
}

从上面的代码我们可以了解两个知识点

1.onTouchEvent()事件,好像只能在控件中继承的时候写死

2.onTouch()事件,可以在代码中给控件注册监听事件

打印的log如下:

1)点击屏幕tv以外的地方

- ::14.665: E/(): ontouchevent
- ::14.689: E/(): ontouchevent
- ::14.706: E/(): ontouchevent
- ::14.723: E/(): ontouchevent
- ::14.740: E/(): ontouchevent
- ::14.758: E/(): ontouchevent
- ::14.774: E/(): ontouchevent
- ::14.791: E/(): ontouchevent
- ::14.809: E/(): ontouchevent
- ::14.826: E/(): ontouchevent
- ::14.843: E/(): ontouchevent
- ::14.860: E/(): ontouchevent
- ::14.877: E/(): ontouchevent

2)点击tv的时候

- ::18.290: E/(): ontouch
- ::18.291: E/(): ontouchevent
- ::18.320: E/(): ontouchevent
- ::18.340: E/(): ontouchevent
- ::18.354: E/(): ontouchevent

得出的结论是

onToucht()事件总是最早接获得监听,然后才是的监听事件onTouchEvent()

2.如果我们把onTouch() 的返回值设置为 true

- ::52.923: E/(): ontouch
- ::52.954: E/(): ontouch
- ::52.971: E/(): ontouch
- ::52.985: E/(): ontouch
- ::52.988: E/(): ontouch
- ::54.701: E/(): ontouch
- ::54.729: E/(): ontouch
- ::54.746: E/(): ontouch
- ::54.755: E/(): ontouch

这个我们可以得出的结论是

在onTouch->onTouchEvent()过程中,前一个(onTouch())返回true,那么这个响应事件就不会在传递给下一个接收着(onTouchEvent()),返回false,就是继续传递

3.现在我们在继续看一下他们两个跟GestureDetector的关系

我们把onTouchEvent()的返回值更改如下

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.e("123","ontouchevent");
// return super.onTouchEvent(event);
return mDetector.onTouchEvent(event);
};

注意一点,虽然GestureDetector监听事件里没有onTouchEvent(),但是它本身却是有这种方法的

打印的log如下

1)点击屏幕tv 外一点

- ::39.414: E/(): ontouchevent
- ::39.414: E/(): ondown
- ::39.443: E/(): ontouchevent
- ::39.447: E/(): ontouchevent
- ::39.447: E/(): onsingle
- ::49.245: E/(): ontouchevent
- ::49.246: E/(): ondown
- ::49.273: E/(): ontouchevent
- ::49.291: E/(): ontouchevent
- ::49.293: E/(): ontouchevent

传递顺序onTouchEvent()->GestureDetector.onDown()->GestureDetector.onsingle()

2)滑动屏幕tv外一点

- ::34.008: E/(): ontouchevent
- ::34.008: E/(): ondown
- ::34.056: E/(): ontouchevent
- ::34.071: E/(): ontouchevent
- ::34.088: E/(): ontouchevent
- ::34.089: E/(): onscroll
- ::34.106: E/(): ontouchevent
- ::34.106: E/(): onscroll
- ::34.124: E/(): ontouchevent
- ::34.124: E/(): onscroll
- ::34.140: E/(): ontouchevent
- ::34.140: E/(): onscroll
- ::34.157: E/(): ontouchevent
- ::34.157: E/(): onscroll
- ::34.174: E/(): ontouchevent
- ::34.174: E/(): onscroll
- ::34.192: E/(): ontouchevent
- ::34.192: E/(): onscroll
- ::34.208: E/(): ontouchevent
- ::34.209: E/(): onscroll
- ::34.226: E/(): ontouchevent
- ::34.226: E/(): onscroll
- ::34.243: E/(): ontouchevent
- ::34.243: E/(): onscroll
- ::34.260: E/(): ontouchevent
- ::34.260: E/(): onscroll
- ::34.277: E/(): ontouchevent
- ::34.278: E/(): onscroll
- ::34.294: E/(): ontouchevent
- ::34.294: E/(): onscroll
- ::34.311: E/(): ontouchevent
- ::34.311: E/(): onscroll
- ::34.328: E/(): ontouchevent
- ::34.328: E/(): onscroll
- ::34.341: E/(): ontouchevent
- ::34.341: E/(): onscroll
- ::34.342: E/(): ontouchevent
- ::34.342: E/(): onfling

传递顺序onTouchEvent()->GestureDetector.onDown()->GestureDetector.onScroll()->GestureDetector.onfling()

对于点击tv,情况跟上面一样,仅仅是先执行ontouch罢了

- ::20.804: E/(): ontouch
- ::20.805: E/(): ontouchevent
- ::20.806: E/(): ondown
- ::20.837: E/(): ontouchevent
- ::20.854: E/(): ontouchevent
- ::20.872: E/(): ontouchevent
- ::20.889: E/(): ontouchevent
- ::20.906: E/(): ontouchevent
- ::20.923: E/(): ontouchevent
- ::20.940: E/(): ontouchevent
- ::20.957: E/(): ontouchevent
- ::20.975: E/(): ontouchevent
- ::20.981: E/(): onshowpress
- ::20.991: E/(): ontouchevent
- ::21.009: E/(): ontouchevent
- ::21.026: E/(): ontouchevent
- ::21.043: E/(): ontouchevent
- ::21.043: E/(): onscroll
- ::21.060: E/(): ontouchevent
- ::21.063: E/(): ontouchevent

GestureDetector监听时间方法

ondown:这是当按下屏幕的时候执行

onscroll:当屏幕滑动过程中一直时候执行

onfling:滑动屏幕结束,手离开的时候执行

onsingle:点击结束,手离开的时候执行

touch、touchevent-事件总结的更多相关文章

  1. 【转】Android TouchEvent事件传递机制

    Android TouchEvent事件传递机制   事件机制参考地址: http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html ht ...

  2. 一文读懂 Android TouchEvent 事件分发、拦截、处理过程

    什么是事件?事件是用户触摸手机屏幕,引起的一系列TouchEvent,包括ACTION_DOWN.ACTION_MOVE.ACTION_UP.ACTION_CANCEL等,这些action组合后变成点 ...

  3. Touch事件 移动端touch触摸事件

    <!-- HTML5 --> <!DOCTYPE html> <html> <head> <title>TouchEvent测试</t ...

  4. 与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦

    原文:与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦 [索引页][源码下载] ...

  5. 移动端触摸(touch)事件

    移动端时代已经到来,作为前端开发的我们没有理由也不应该坐井观天,而是勇敢地跳出心里的那口井,去拥抱蔚蓝的天空.该来的总会来,我们要做的就是接受未知的挑战.正如你所看到的,这是一篇关于移动端触摸事件的文 ...

  6. 移动端touch触摸事件(滑动效果和手势操作)

    一.定义 ①touch是移动端的触摸事件,而且是一组事件,主要有以下事件: touchstart 事件:当手指触摸屏幕的时候触发 touchmove 事件:当手指在屏幕来回滑动的时候触发 touche ...

  7. touch触摸事件

    事件对象 事件对象是用来记录一些事件发生时的相关信息的对象.事件对象只有事件发生时才会产生,并且只能是事件处理函数内部访问,在所有事件处理函数运行结束后,事件对象就被销毁! W3C DOM把事件对象作 ...

  8. android touchEvent事件学习

    学习网址:http://www.apkbus.com/forum.php?mod=viewthread&tid=44296 1:Android Touch事件传递机制解析 android系统中 ...

  9. 移动端touch拖动事件和click事件冲突问题解决

    通过一个悬浮球交互功能的案例来阐述问题,以及解决办法. 实现效果 类似微信里的悬浮窗效果,苹果手机的悬浮球功能效果 可以点击拖动,然后吸附在窗口边缘 点击悬浮球,可以跳转界面,或者更改悬浮球的形态 准 ...

  10. 移动端WEB开发,click,touch,tap事件浅析

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

随机推荐

  1. ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database

    报错如下:ORA-12012: error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_88"ORA-2000 ...

  2. mysql的my.cnf文件详解

    一.缘由 最近要接手数据库的维护工作,公司首选MySQL.对于MySQL的理解,我认为很多性能优化工作.主从主主复制都是在调整参数,来适应不同时期不同数量级的数据. 故,理解透彻my.cnf里的参数是 ...

  3. CSUOJ 1638 Continued Fraction

    1638: Continued Fraction Time Limit: 1 Sec  Memory Limit: 128 MB Description Input Output Sample Inp ...

  4. boost::asio与ACE的对比

    http://blog.163.com/miky_sun/blog/static/3369405201041753652505/

  5. 读书笔记-深入理解JVM虚拟机-1.OOM初探

    Java堆OOM(Out-Of-Memory)异常 执行例如以下程序,爆出异常 java.lang.OutOfMemoryError: Java heap space /** * VM Args:-X ...

  6. spark资料下载

    很全的spark资料下载,包含pdf书籍和培训学校视频教程, 1.spark多语言编程:spark多语言开发 2.tachyon:tachyon 3.sparkR:sparkR 所有内容请点击:所有内 ...

  7. 黑马程序猿——JAVA面向对象的特性:封装,继承,多态

                                           - ----------android培训.java培训.java学习型技术博客.期待与您交流!------------  ...

  8. OpenCASCADE解非线性方程组

    OpenCASCADE解非线性方程组 eryar@163.com Abstract. 在科学技术领域里常常提出求解非线性方程组的问题,例如,用非线性函数拟合实验数据问题.非线性网络问题.几何上的曲线曲 ...

  9. android定时操作

    Timer: 代码: Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { ...

  10. C#加减乘除

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...