Android学习 多读官网,故意健康---手势
官网地址 ttp://developer.android.com/training/gestures/detector.html:
一、能够直接覆盖Activity的onTouch方法
public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
@Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG,"Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return super.onTouchEvent(event);
}
}
二:也能够给单个的view设置监听器
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events
return true;
}
});
三:可是 onTouch方法毕竟仅仅能检測一些简单的手势,像滑动、双击、长按等。单用onTouch方法处理就显得棘手了,谷歌提供了方便的 GestureDetector
类来更方便的处理手势。
详细的须要实例 GestureDetectorCompat 类, 该类中的构造方法须要实现
监听器。GestureDetector.OnGestureListener 去通知用户,当各种事件(滑动、双击、长按等)发生。为了使
GestureDetector
正常监听事件,同一时候须要重写Activity的
nTouch方法。把事件交给Detector。
详细见官网demo:
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector; // Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
} @Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
} @Override
public boolean onDown(MotionEvent event) {
Log.d(DEBUG_TAG,"onDown: " + event.toString());
return true;
} @Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return true;
} @Override
public void onLongPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
return true;
} @Override
public void onShowPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
} @Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
return true;
} @Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return true;
} @Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
return true;
} @Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
}
补充:
onScroll:在屏幕上慢慢的滚动。
onFling:在屏幕上高速的滚动,并高速移开手指。
Whether or not you useGestureDetector.OnGestureListener
, it's best practice to implement anonDown()
method that returnstrue
. This is because all gestures begin with anonDown()
message. If you returnfalse
fromonDown()
, asGestureDetector.SimpleOnGestureListener
does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods ofGestureDetector.OnGestureListener
never get called. This has the potential to cause unexpected problems in your app. The only time you should returnfalse
fromonDown()
is if you truly want to ignore an entire gesture.
另外,官网上说最好复写onDown方法,并返回true,由于其它方法的消息都是从onDown方法開始的。并且onDown系统默认返回false,其它的方法也不会被调用。
但是我自己试了下,当onDown方法返回false,其它方法是能够被调用的。。这就奇怪了。。。哪位高手亲试过这个样例的,回复下。。
Android学习 多读官网,故意健康---手势的更多相关文章
- go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE
go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE Go语言是谷歌2009发布的专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速 ...
- 【Spark深入学习 -16】官网学习SparkSQL
----本节内容-------1.概览 1.1 Spark SQL 1.2 DatSets和DataFrame2.动手干活 2.1 契入点:SparkSess ...
- 记录几个 Android x86 系统的官网
首先是官网:https://www.android-x86.org/ 国内: 凤凰OS(Android 7.1):http://www.phoenixos.com/download_x86 技德Rem ...
- GoldData学习实例-采集官网新闻数据
概述 在本节中,我们将讲述抓取政府官网地方新闻.并将抓取的新闻数据融入到以下两张数据表news_site和news中. news_site(新闻来源) 字段 类型 说明 id bigint 主键,自动 ...
- Nginx的配置文件(nginx.conf)解析和领读官网
步骤一:vi nginx.conf配置文件,参考本博文的最下面总结,自行去设置 最后nginx.conf内容为 步骤二:每次修改了nginx.conf配置文件后,都要reload下. index.ht ...
- (转)android之Fragment(官网资料翻译)
Fragment要点 Fragment作为Activity界面的一部分组成出现 可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用. ...
- android 之Fragment(官网资料翻译)
原文地址: http://blog.csdn.net/lilu_leo/article/details/7671533 *************************** 正文分割线 ***** ...
- android之Fragment(官网资料翻译)
Fragment要点 Fragment作为Activity界面的一部分组成出现 能够在一个Activity中同一时候出现多个Fragment,而且,一个Fragment亦可在多个Activity中使用 ...
- Drupal8学习之路--官网文档碎碎记--主题篇
主要记录一些琐碎的知识点. 1.“In Drupal 8 drupal_add_css(), drupal_add_js() and drupal_add_library()were removed ...
随机推荐
- 异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext 解决方案:缺少spring-expres ...
- sublime text 3 安装Nodejs插件
如题 1)集成Nodejs插件到sublime,地址:https://github.com/tanepiper/SublimeText-Nodejs2)解压zip文件, 并重命名文件夹“Nodejs” ...
- springmvc请求方法那些事
@RequestMapping 用法详解之地址映射 (2013-08-11 16:06:58) 转载▼ 标签: it 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式 ...
- hdu 6441 Find Integer(费马大定理+勾股数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6441(本题来源于2018年中国大学生程序设计竞赛网络选拔赛) 题意:输入n和a,求满足等式a^n+b^ ...
- Luogu-P1020(导弹拦截)(DP,LIS ,二分优化)
Luogu-P1020(导弹拦截)(DP) 题意: 给n(n<=100000) 个数字,求最长不上升子序列的长度和最少的不上升子序列的个数. 分析: 第一问: 求最长不上升子序列有 O(n^2) ...
- 12. KEY_COLUMN_USAGE
12. KEY_COLUMN_USAGE KEY_COLUMN_USAGE表描述哪些键列具有约束. KEY_COLUMN_USAGE表有以下列: CONSTRAINT_CATALOG :约束所属目录的 ...
- 条款10:令operator=返回一个reference to * this(Have assignment operators return a reference to *this)
NOTE: 1.令赋值(assignment)操作符返回一个reference to *this. 2.此协议适用于所有赋值相关的运算比如:+= -= *=....
- 【笔记】ubuntu如何切换到root用户&&linux如何关闭各种保护
默认安装完成之后并不知道root用户的密码,那么如何应用root权限呢? (1)sudo 命令 这样输入当前管理员用户密码就可以得到超级用户的权限.但默认的情况下5分钟root权限就失效了. () ...
- python3 时间复杂度
时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就 ...
- 常见的 Android 新手误区
在过去十年的移动开发平台中,作为资深的移动开发人员,我们认为Android平台是一个新手最广为人知的平台.它不仅是一个廉价的工具,而且有着良好的 开发社区,以及从所周知的编程语言(Java),使得开发 ...