android GestureDetector的用法和介绍
来源:互联网 发布:福州橙子网络 编辑:程序博客网 时间:2024/06/12 06:47
通过系统提供的MotionEvent来监测各种手势和(触摸)事件。当一个指定的手势事件发生时,GestureDetector.OnGestureListener回调函数将通告用户。这个类仅仅处理由触摸引发的MotionEvent(不能处理由轨迹球引发的事件)。
类中的一些手势类型:
1.boolean onDoubleTap(MotionEvent e)解释:双击的第二下Touch down时触发
2.boolean onDoubleTapEvent(MotionEvent e)解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
3.boolean onDown(MotionEvent e)解释:Touch down时触发
4.boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)解释:Touch了滑动一点距离后,up时触发。
5.void onLongPress(MotionEvent e)解释:Touch了不移动一直Touch down时触发
6.boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)解释:Touch了滑动时触发。
7.void onShowPress(MotionEvent e)解释:Touch了还没有滑动时触发(与onDown,onLongPress)比较onDown只要Touch down一定立刻触发。而Touchdown后过一 会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不 滑动,onDown->onShowPress->onLongPress这个顺序触发。
8.boolean onSingleTapConfirmed(MotionEvent e)
9.boolean onSingleTapUp(MotionEvent e)解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
点击一下非常快的(不滑动)Touchup:onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑 动)Touchup:onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
使用方法:
构造出来,mGestureDetector = new GestureDetector(mGestureListener);
mGestureListener = new BookOnGestureListener();
class BookOnGestureListener implements OnGestureListener {
同时要public boolean onTouchEvent(MotionEvent event) {
mGestureListener.onTouchEvent(event);
}
这样就可以判断手势MotionEvent 的类型了,然后做出一些处理。
下面贴出一个类做参考、
- class BookOnGestureListenerimplements OnGestureListener {
- public boolean onDown(MotionEvent event) {
- Log.d(LOG_TAG, "onDown");
- if (mState == BookState.ANIMATING)
- return false;
- float x = event.getX(), y = event.getY();
- int w = layoutWidth, h = layoutHeight;
- if (x * x + y * y < clickCornerLen) {
- if (indexPage >0) {
- mSelectCorner = Corner.LeftTop;
- aniStartPos = new Point(0,0);
- }
- } else if ((x - w) * (x - w) + y * y < clickCornerLen) {
- if (indexPage < totalPageNum -1) {
- mSelectCorner = Corner.RightTop;
- aniStartPos = new Point(layoutWidth,0);
- }
- } else if (x * x + (y - h) * (y - h) < clickCornerLen) {
- if (indexPage >0) {
- mSelectCorner = Corner.LeftBottom;
- aniStartPos = new Point(0, layoutHeight);
- }
- } else if ((x - w) * (x - w) + (y - h) * (y - h) < clickCornerLen) {
- if (indexPage < totalPageNum -1) {
- mSelectCorner = Corner.RightBottom;
- aniStartPos = new Point(layoutWidth, layoutHeight);
- }
- }
- if (mSelectCorner != Corner.None) {
- aniStopPos = new Point((int) x, (int) y);
- aniTime = 800;
- mState = BookState.ABOUT_TO_ANIMATE;
- closeBook = false;
- aniStartTime = new Date();
- mBookView.startAnimation();
- }
- return false;
- }
- public boolean onFling(MotionEvent e1, MotionEvent e2,float velocityX,
- float velocityY) {
- Log.d(LOG_TAG, "onFling velocityX:" + velocityX +" velocityY:"
- + velocityY);
- if (mSelectCorner != Corner.None) {
- if (mSelectCorner == Corner.LeftTop) {
- if (velocityX < 0) {
- aniStopPos = new Point(0, 0);
- } else {
- aniStopPos = new Point(2 * layoutWidth, 0);
- }
- } else if (mSelectCorner == Corner.RightTop) {
- if (velocityX < 0) {
- aniStopPos = new Point(-layoutWidth, 0);
- } else {
- aniStopPos = new Point(layoutWidth, 0);
- }
- } else if (mSelectCorner == Corner.LeftBottom) {
- if (velocityX < 0) {
- aniStopPos = new Point(0, layoutHeight);
- } else {
- aniStopPos = new Point(2 * layoutWidth, layoutHeight);
- }
- } else if (mSelectCorner == Corner.RightBottom) {
- if (velocityX < 0) {
- aniStopPos = new Point(-layoutWidth, layoutHeight);
- } else {
- aniStopPos = new Point(layoutWidth, layoutHeight);
- }
- }
- Log.d(LOG_TAG, "onFling animate");
- aniStartPos = new Point((int) scrollX, (int) scrollY);
- aniTime = 1000;
- mState = BookState.ABOUT_TO_ANIMATE;
- closeBook = true;
- aniStartTime = new Date();
- mBookView.startAnimation();
- }
- return false;
- }
- public void onLongPress(MotionEvent e) {
- Log.d(LOG_TAG, "onLongPress");
- }
- public boolean onScroll(MotionEvent e1, MotionEvent e2,
- float distanceX, float distanceY) {
- mState = BookState.TRACKING;
- if (mSelectCorner != Corner.None) {
- scrollX = e2.getX();
- scrollY = e2.getY();
- mBookView.startAnimation();
- }
- return false;
- }
- public void onShowPress(MotionEvent e) {
- Log.d(LOG_TAG, "onShowPress");
- }
- public boolean onSingleTapUp(MotionEvent e) {
- Log.d(LOG_TAG, "onSingleTapUp");
- if (mSelectCorner != Corner.None) {
- if (mSelectCorner == Corner.LeftTop) {
- if (scrollX < layoutWidth /2) {
- aniStopPos = new Point(0,0);
- } else {
- aniStopPos = new Point(2 * layoutWidth,0);
- }
- } else if (mSelectCorner == Corner.RightTop) {
- if (scrollX < layoutWidth /2) {
- aniStopPos = new Point(-layoutWidth,0);
- } else {
- aniStopPos = new Point(layoutWidth,0);
- }
- } else if (mSelectCorner == Corner.LeftBottom) {
- if (scrollX < layoutWidth /2) {
- aniStopPos = new Point(0, layoutHeight);
- } else {
- aniStopPos = new Point(2 * layoutWidth, layoutHeight);
- }
- } else if (mSelectCorner == Corner.RightBottom) {
- if (scrollX < layoutWidth /2) {
- aniStopPos = new Point(-layoutWidth, layoutHeight);
- } else {
- aniStopPos = new Point(layoutWidth, layoutHeight);
- }
- }
- aniStartPos = new Point((int) scrollX, (int) scrollY);
- aniTime = 800;
- mState = BookState.ABOUT_TO_ANIMATE;
- closeBook = true;
- aniStartTime = new Date();
- mBookView.startAnimation();
- }
- return false;
- }
- }
转自 :http://blog.csdn.net/peijiangping1989/article/details/7227625
- android GestureDetector的用法和介绍
- GestureDetector的用法和介绍
- GestureDetector的用法和介绍
- Android :GestureDetector有用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- GestureDetector类的用法
- gestureDetector类的用法
- GestureDetector类的用法
- android GestureDetector类及其用法
- Android--GestureDetector类及其用法
- 0.ring3-USER32!__ClientLoadLibrary定位.
- easyUI为datagrid创建自定义视图
- 呼叫中心考试的一点意见
- get方式长度限制
- VC++----简繁体转换版本2
- android GestureDetector的用法和介绍
- POJ 1228 Grandpa's Estate (求稳定凸包)
- 关于UC厂商的四象限分布
- oracle 复制数据并插入当前表的语句
- 由于协议错误,会话将被中断。请重新连接到远程计算机
- VS2010编译警告:warning C4996: 'strcpy': This function
- Havel-Hakimi定理(判断一个序列是否可图)
- ubuntu的texteditor不能显示中文
- 关于汇编ARM指令DCD