要实现手指在屏幕上左右滑动的事件需要实例化对象GestureDetector,newGestureDetector(MainActivity.this,onGestureListener);首先实现监听对象GestureDetector.OnGestureListener,根据x或y轴前后变化坐标来判断是左滑动还是右滑动并根据不同手势滑动做出事件处理doResult(intaction),

然后覆写onTouchEvent方法,在onTouchEvent方法中将event对象传给gestureDetector.onTouchEvent(event);处理。

importandroid.view.GestureDetector;

publicMainActivityextendsTabActivityimplementsOnClickListener{

finalintRIGHT=0;
finalintLEFT=1;
privateGestureDetectorgestureDetector;

/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

gestureDetector=newGestureDetector(MainActivity.this,onGestureListener);
}

privateGestureDetector.OnGestureListeneronGestureListener=
newGestureDetector.SimpleOnGestureListener(){
@Override
publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
floatvelocityY){
floatx=e2.getX()-e1.getX();
floaty=e2.getY()-e1.getY();

if(x>0){
doResult(RIGHT);
}elseif(x<0){
doResult(LEFT);
}
returntrue;
}
};

publicbooleanonTouchEvent(MotionEventevent){
returngestureDetector.onTouchEvent(event);
}

publicvoiddoResult(intaction){

switch(action){
caseRIGHT:
System.out.println("goright");
break;

caseLEFT:
System.out.println("goright");
break;

}
}
}

参考http://www.cnblogs.com/meieiem/archive/2011/09/16/2178313.html

更多相关文章

  1. Android(安卓)dispatchTouchEvent源码分析
  2. Android(安卓)Content Provider基础
  3. Android—滑动ListView保存选中状态
  4. VelocityTracker使用详解
  5. Android中的事件传递
  6. Android应用程序键盘(Keyboard)消息处理机制分析(三)
  7. Android(安卓)事件处理
  8. Android(安卓)ListView CheckBox状态错乱
  9. Android开发底部点击按钮从屏幕底部滑出面板(一)

随机推荐

  1. Android ToastUtil
  2. Android:电话拨号器、呼叫记录、结束通话
  3. Android 硬编码
  4. Android 怎么防止AlertDialog Button按钮
  5. Android面试题集合
  6. Android中使用SAX解析XML
  7. Android 修改App中默认TextView的字体和
  8. android application级别的图片缓存
  9. 阅读《Android 从入门到精通》(14)——时间
  10. android1.5---2.1实现js调摄像头,调Java代