- 浏览: 294290 次
- 性别:
- 来自: 上海
最新评论
-
再_见孙悟空:
写的不错
Dialog -
a549262189:
支持下,正好在学习这块的内容!
Android Gesture Detector -
dcsff:
受教了
LayoutInflater -
庆_啊:
真厉害。
LayoutInflater -
ghj234:
学习了,哈哈
LayoutInflater
感谢android中国开发者的众多先驱,本文主要内容来自于总结,一小部分是自己的体会。主要参考文章为:
http://www.williamhua.com/2009/04/23/android-touchscreen-gesture-recogniton/
http://goro.iteye.com/blog/402163
Android Touch Screen 与传统Click Touch Screen不同,会有一些手势(Gesture),例如Fling,Scroll等等。这些Gesture会使用户体验大大提升。Android中的Gesture识别(detector)是通过GestureDetector.OnGestureListener接口实现的。
首先,Android事件处理机制是基于Listener实现的,比如触摸屏相关的事件,就是通过onTouchListener实现;
其次,所有View的子类都可以通过setOnTouchListener()、setOnKeyListener()等方法来添加对某一类事件的Listener;
第三,Listener一般会以Interface的方式来提供,其中包含一个或多个abstract方法,我们需要实现这些方法来完成onTouch()、onKey()等操作。这样,程序便可以在特定的事件被dispatch到该view的时候,通过callback函数给予适当的响应。
1. Touch Screen Click举例
public class MyGesture extends Activity implements OnTouchListener { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); } public boolean onTouch(View v, MotionEvent event) { Toast.makeText(this, "Touch Touch", Toast.LENGTH_SHORT).show(); return false; } }
我们可以通过MotionEvent的getAction()方法来获取Touch事件的类型,包括 ACTION_DOWN(按下触摸屏), ACTION_MOVE(按下触摸屏后移动受力点), ACTION_UP(松开触摸屏)和ACTION_CANCEL(不会由用户直接触发)。借助对于用户不同操作的判断,结合getRawX()、getRawY()、getX()和getY()等方法来获取坐标后,我们可以实现诸如拖动某一个按钮,拖动滚动条等功能。
2. 回到今天的重点,当我们捕捉到Touch操作的时候,如何识别出用户的Gesture?这里我们需要GestureDetector.OnGestureListener接口的帮助,代码如下:
public class MyGesture extends Activity implements OnTouchListener, OnGestureListener { private GestureDetector mGestureDetector; public MyGesture() { mGestureDetector = new GestureDetector(this); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); mGestureDetector.setIsLongpressEnabled(true); } /* * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector * 来分析是否有合适的callback函数来处理用户的手势 */ public boolean onTouch(View v, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发 public boolean onDown(MotionEvent arg0) { Log.i("MyGesture", "onDown"); Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show(); return true; } /* * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 * 注意和onDown()的区别,强调的是没有松开或者拖动的状态 */ public void onShowPress(MotionEvent e) { Log.i("MyGesture", "onShowPress"); Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show(); } // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发 public boolean onSingleTapUp(MotionEvent e) { Log.i("MyGesture", "onSingleTapUp"); Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show(); return true; } // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i("MyGesture", "onFling"); Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show(); return true; } // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll"); Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show(); return true; } // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发 public void onLongPress(MotionEvent e) { Log.i("MyGesture", "onLongPress"); Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show(); } }
3. Fling事件的处理代码:除了第一个触发Fling的ACTION_DOWN和最后一个ACTION_MOVE中包含的坐标等信息外,我们还可以根据用户在X轴或者Y轴上的移动速度作为条件。比如下面的代码中我们就在用户移动超过100个像素,且X轴上每秒的移动速度大于200像素时才进行处理。
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 参数解释: // e1:第1个ACTION_DOWN MotionEvent // e2:最后一个ACTION_MOVE MotionEvent // velocityX:X轴上的移动速度,像素/秒 // velocityY:Y轴上的移动速度,像素/秒 // 触发条件 : // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒 final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200; if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { // Fling left Log.i("MyGesture", "Fling left"); Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show(); } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { // Fling right Log.i("MyGesture", "Fling right"); Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show(); } return false; }
这个例子中,tv.setLongClickable(
true
)是必须的,因为
只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点。
4. SimpleOnGestureListener
public class MyGesture extends Activity implements OnTouchListener { private GestureDetector mGestureDetector; public MyGesture() { mGestureDetector = new GestureDetector(new MySimpleGesture()); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { Log.i("MyGesture", "MotionEvent.ACTION_UP"); } return mGestureDetector.onTouchEvent(event); } // SimpleOnGestureListener implements GestureDetector.OnDoubleTapListener, GestureDetector.OnGestureListener private class MySimpleGesture extends SimpleOnGestureListener { // 双击的第二下Touch down时触发 public boolean onDoubleTap(MotionEvent e) { Log.i("MyGesture", "onDoubleTap"); return super.onDoubleTap(e); } // 双击的第二下Touch down和up都会触发,可用e.getAction()区分 public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return super.onDoubleTapEvent(e); } // Touch down时触发 public boolean onDown(MotionEvent e) { Log.i("MyGesture", "onDown"); return super.onDown(e); } // Touch了滑动一点距离后,up时触发 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i("MyGesture", "onFling"); return super.onFling(e1, e2, velocityX, velocityY); } // Touch了不移动一直Touch down时触发 public void onLongPress(MotionEvent e) { Log.i("MyGesture", "onLongPress"); super.onLongPress(e); } // Touch了滑动时触发 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll"); return super.onScroll(e1, e2, distanceX, distanceY); } /* * Touch了还没有滑动时触发 * (1)onDown只要Touch Down一定立刻触发 * (2)Touch Down后过一会没有滑动先触发onShowPress再触发onLongPress * So: Touch Down后一直不滑动,onDown -> onShowPress -> onLongPress这个顺序触发。 */ public void onShowPress(MotionEvent e) { Log.i("MyGesture", "onShowPress"); super.onShowPress(e); } /* * 两个函数都是在Touch Down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touch Up时触发 * 点击一下非常快的(不滑动)Touch Up: onDown->onSingleTapUp->onSingleTapConfirmed * 点击一下稍微慢点的(不滑动)Touch Up: onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed */ public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("MyGesture", "onSingleTapConfirmed"); return super.onSingleTapConfirmed(e); } public boolean onSingleTapUp(MotionEvent e) { Log.i("MyGesture", "onSingleTapUp"); return super.onSingleTapUp(e); } } }
5.
评论
发表评论
-
如果在Eclipse中debug
2010-05-02 14:15 0最基本的操作是: 1, 首先在一个java文件中设断点,然后 ... -
How to download sourcecode of android with cygwin
2010-04-14 16:07 5076cygwin是一个在windows平台 ... -
TelephoneManager
2010-03-08 16:04 01. protected void onCreate(Bun ... -
eclipse c
2010-03-07 21:05 0Eclipse 除 了可以开发Java之外,还支持了许 ... -
android sdk安装
2010-02-02 23:37 34451.下载:http://developer.android.c ... -
google map my demo
2010-02-02 23:36 3155<uses-library android:name=& ... -
Toast
2010-02-02 23:35 14161. (1) public class ToastActi ... -
如何在MapView上画图
2010-02-02 23:34 1551http://ophone8.com/thread-4340- ... -
Service
2010-02-02 23:33 31081. Service生命周期 (1)Servic ... -
Notification
2010-02-02 23:33 18771. Notification构造器的参数 分别为: ... -
导航相关概念
2010-02-02 23:32 16691. Google Map的定位 (1)Goog ... -
PendingIntent和Intent
2010-02-02 23:28 9487Notification n = new Notificati ... -
Broadcast调用Service做的一个定时器
2010-02-02 23:28 36661. public class AlarmActivity ... -
Broadcast Receiver
2010-02-02 23:27 8654Broadcast Receiver用于接收并处理广播通知(b ... -
LayoutInflater
2010-02-02 23:27 21758一般来讲,我们用LayoutInflater做一件事:infl ... -
Android's Components 生命周期
2010-02-02 23:27 2104Android中,Component(activity、ser ... -
Activity文档翻译
2010-02-02 23:26 1539转自:http://www.blogjava.net/m ... -
Intent
2010-01-20 10:51 011 public class MumActivity ex ... -
aidl
2010-01-05 11:24 30201. android进程之间如何通讯呢(在不同的进程中怎样传递 ... -
Gallery 画廊
2009-12-17 20:25 33271. images.xml <?xml ver ...
相关推荐
自定义ViewSwitcher实现分屏及切换动画效果,利用Gesture Detector解决GridView的onTouch与onItemClick事件冲突
Myo-Armband-Gesture-Detector-Sample---Android 这是关于如何从 Myo Armband 中可用的手势获取提要的简单示例
Android源码-使用ViewFlipper+GestureDetector实现划动翻页,以及捕捉长按,双击,上下滑动,左右滑动等事件。
详见http://blog.csdn.net/huaxun66/article/details/52356872
3.1.4 VelocityT racker、Gesture Detector和Scroller / 126 3.2 View的滑动 / 129 3.2.1 使用scroll To/scroll By / 129 3.2.2 使用动画 / 131 3.2.3 改变布局参数 / 133 3.2.4 各种滑动方式的对比 / ...
Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要...
/ 123 3.1.3 Motion Event和Touch Slop / 125 3.1.4 VelocityT racker、Gesture Detector和Scroller / 126 3.2 View的滑动 / 129 3.2.1 使用scroll To/scroll By / 129 3.2.2 使用动画 / 131 3.2.3 改变布局...
Android 触摸及手势操作GestureDetector http://blog.csdn.net/xyz_lmn/article/details/16826669
若被平台自动设置收费、C币,请联系我改免费。平台老是自动变收费,有毛病?Android手势处理库android-gesture-detectors使用,参考文章https://blog.csdn.net/Cupster/article/details/113352077
一、概述 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing...Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的on
利用GestureDetector关闭activity 手势向右滑动关闭当前activity