码迷,mamicode.com
首页 > 其他好文 > 详细

为activity增加左右手势识别

时间:2014-07-21 23:13:44      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:android   手势识别   

android开发中为activity增加左右手势识别,如右滑关闭当前页面

/*
  *  for左右手势
  *  1.复制下面的内容到目标Activity
  *  2.目标Activity的onCreate()调用initGesture()
  *  3.目标Activity需implements OnTouchListener, OnGestureListener
  */
 private GestureDetector mGestureDetector;
 private int verticalMinDistance = 180;
 private int minVelocity         = 0;
 
 private void initGesture() {
     mGestureDetector = new GestureDetector((OnGestureListener) this);
 }
 
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
 
     if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
 
         // 切换Activity
         // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
         // startActivity(intent);
         //Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
     } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
 
         // 切换Activity
         // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
         // startActivity(intent);
         //Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
         finish();
         overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
     }
 
     return false;
 }
  
 @Override
 public void onLongPress(MotionEvent arg0) {
     // TODO Auto-generated method stub
      
 }
 
 @Override
 public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
         float arg3) {
     // TODO Auto-generated method stub
     return false;
 }
 
 @Override
 public void onShowPress(MotionEvent arg0) {
     // TODO Auto-generated method stub
      
 }
 
 @Override
 public boolean onSingleTapUp(MotionEvent arg0) {
     // TODO Auto-generated method stub
     return false;
 }
 
 @Override
 public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     return mGestureDetector.onTouchEvent(event);
 }
 
 @Override
 public boolean onDown(MotionEvent arg0) {
     // TODO Auto-generated method stub
     return false;
 }
  
 @Override 
 public boolean dispatchTouchEvent(MotionEvent ev) {
     mGestureDetector.onTouchEvent(ev);
     return super.dispatchTouchEvent(ev);
}


为activity增加左右手势识别

标签:android   手势识别   

原文地址:http://blog.csdn.net/anjon520/article/details/38025783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!