手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android使用gesturedetector手势识别示例分享
android使用gesturedetector手势识别示例分享
摘要:复制代码代码如下:publicclassMyGestureLintenerextendsSimpleOnGestureListener{pr...

复制代码 代码如下:

public class MyGestureLintener extends SimpleOnGestureListener {

private Context context;

public MyGestureLintener(Context context) {

super();

this.context = context;

}

// 单击,触摸屏按下时立刻触发

/*@Override

public boolean onDown(MotionEvent e) {

// TODO Auto-generated method stub

Toast.makeText(context, "Down " + e.getAction(), Toast.LENGTH_SHORT)

.show();

return true;

}*/

// 双击,手指在触摸屏上迅速点击第二下时触发

@Override

public boolean onDoubleTap(MotionEvent e) {

// TODO Auto-generated method stub

return super.onDoubleTap(e);

}

// 双击的按下跟抬起各触发一次

@Override

public boolean onDoubleTapEvent(MotionEvent e) {

// TODO Auto-generated method stub

return super.onDoubleTapEvent(e);

}

// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

// TODO Auto-generated method stub

return super.onFling(e1, e2, velocityX, velocityY);

}

// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发

@Override

public void onLongPress(MotionEvent e) {

// TODO Auto-generated method stub

Toast.makeText(context, "LONG " + e.getAction(), Toast.LENGTH_SHORT)

.show();

}

// 滚动,触摸屏按下后移动

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

float distanceY) {

Toast.makeText(context, "onScroll " + e2.getAction(), Toast.LENGTH_SHORT)

.show();

return true;

}

// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会

@Override

public void onShowPress(MotionEvent e) {

// TODO Auto-generated method stub

Toast.makeText(context, "Show " + e.getAction(), Toast.LENGTH_SHORT)

.show();

}

// 单击确认,即很快的按下并抬起,但并不连续点击第二下

/*@Override

public boolean onSingleTapConfirmed(MotionEvent e) {

// TODO Auto-generated method stub

Toast.makeText(context, "onSingleTapConfirmed " + e.getAction(), Toast.LENGTH_SHORT)

.show();

return true;

}*/

// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)

/*@Override

public boolean onSingleTapUp(MotionEvent e) {

// TODO Auto-generated method stub

Toast.makeText(context, "onSingleTapUp " + e.getAction(), Toast.LENGTH_SHORT)

.show();

return true;

}*/

public class MainActivity extends Activity {

private GestureDetector mGestureDetector;//手势对象

private MyGestureLintener myGestureLintener;//手势监听的接口对象

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

myGestureLintener = new MyGestureLintener(this);

//手势对象的构造方法

mGestureDetector = new GestureDetector(this,

myGestureLintener);

}

/**GestureDetector类的onTouchEvent方法用来辨别不同的手势*/

@Override

public boolean onTouchEvent(MotionEvent event) {

boolean b = false;

int i = event.getAction();

int j = MotionEvent.ACTION_MOVE;

System.out.println(i+"<----------------->"+j);

b = mGestureDetector.onTouchEvent(event);

if (b) {

Intent in = new Intent();

in.setClass(this, testActivity.class);

startActivity(in);

}

return b;

}

@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;

}

}

【android使用gesturedetector手势识别示例分享】相关文章:

android 加载本地联系人实现方法

Android 使用新浪微博SSO授权

html5在android中的使用问题及技巧解读

Android ListView数据绑定显示的三种解决方法

android调用web service(cxf)实例应用详解

android 类似微信的摇一摇功能实现思路及代码

android listview 水平滚动和垂直滚动的小例子

android中使用SharedPreferences进行数据存储的操作方法

Android开发之软键盘用法实例分析

Android layout_weight使用方法及实例

精品推荐
分类导航