栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android开发之为activity增加左右手势识别示例

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android开发之为activity增加左右手势识别示例

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

复制代码 代码如下:

   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);
  } 

push_right_in.xml

复制代码 代码如下:


            android:duration="500" />
            android:duration="500"/>

push_right_out.xml

复制代码 代码如下:


            android:duration="500" />
            android:duration="500"/>

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164603.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号