Android GestureDetector手势识别类
2013-07-26 11:08
337 查看
为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap
Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:
boolean onDoubleTap(MotionEvent e)
解释:双击的第二下Touch down时触发
boolean onDoubleTapEvent(MotionEvent e)
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
boolean onDown(MotionEvent e)
解释:Touch down时触发
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
void onLongPress(MotionEvent e)
解释:Touch了不移动一直Touch down时触发
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
void onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
(与onDown,onLongPress比较
onDown只要Touch down一定立刻触发。
而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。
boolean onSingleTapConfirmed(MotionEvent e)
boolean onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
使用GestureDetector需要在View中重写onTouchEvent事件,例如:
[java] view
plaincopy
GestureDetector mGesture = null;
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
详细的测试例子如下:
[cpp] view
plaincopy
package com.jiubang.android.gesturetest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class GestureActivity extends Activity
implements OnTouchListener
{
private Button mButton = null;
GestureDetector mGesture = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("TEST", "onCreate");
mButton = (Button)findViewById(R.id.button1);
mButton.setOnTouchListener(this);
mGesture = new GestureDetector(this, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
class GestureListener extends SimpleOnGestureListener
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDoubleTap");
return super.onDoubleTap(e);
}
@Override
public boolean onDown(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDown");
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onLongPress");
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onSingleTapUp");
return super.onSingleTapUp(e);
}
}
}
Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:
boolean onDoubleTap(MotionEvent e)
解释:双击的第二下Touch down时触发
boolean onDoubleTapEvent(MotionEvent e)
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
boolean onDown(MotionEvent e)
解释:Touch down时触发
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
void onLongPress(MotionEvent e)
解释:Touch了不移动一直Touch down时触发
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
void onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
(与onDown,onLongPress比较
onDown只要Touch down一定立刻触发。
而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。
boolean onSingleTapConfirmed(MotionEvent e)
boolean onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
使用GestureDetector需要在View中重写onTouchEvent事件,例如:
[java] view
plaincopy
GestureDetector mGesture = null;
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
详细的测试例子如下:
[cpp] view
plaincopy
package com.jiubang.android.gesturetest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class GestureActivity extends Activity
implements OnTouchListener
{
private Button mButton = null;
GestureDetector mGesture = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("TEST", "onCreate");
mButton = (Button)findViewById(R.id.button1);
mButton.setOnTouchListener(this);
mGesture = new GestureDetector(this, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
class GestureListener extends SimpleOnGestureListener
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDoubleTap");
return super.onDoubleTap(e);
}
@Override
public boolean onDown(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDown");
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onLongPress");
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onSingleTapUp");
return super.onSingleTapUp(e);
}
}
}
相关文章推荐
- android开发之GestureDetector手势识别(调节音量、亮度、快进和后退)
- Android中手势识别检测器GestureDetector
- Android ScaleGestureDetector缩放手势识别
- Android 手势识别类 ( 二 ) GestureDetector 源码浅析
- Android GestureDetector手势识别类
- Android手势识别之GestureDetector
- Android GestureDetector手势识别类的几种情况
- android开发之GestureDetector手势识别(调节音量、亮度、快进和后退)
- Android GestureDetector手势识别类
- 2014-11-8Android学习------手势识别事件的处理GestureDetector--------动画Animation学习篇
- android GestureDetector应用(手势识别)
- Android GestureDetector手势识别类
- Android GestureDetector手势识别
- Android GestureDetector手势识别类
- Android 手势识别类 ( 一 ) GestureDetector 基本介绍
- Android 手势识别类 ( 三 ) GestureDetector 源码浅析
- Android GestureDetector手势识别与多点触控探究
- Android 手势识别中的两个默认实现SimpleOnScaleGestureListener、SimpleOnGestureListener
- GestureDetector.OnGestureListener 详解以及android手势动作详解!
- Android Gesture 手势识别使用实例 - Android - mobile - ITeye论坛