android怎么为activity开发手势识别

来源:爱站网时间:2022-12-23编辑:网友分享
你知道android怎么为activity开发手势识别吗,想要详细了解文章内容的小伙伴一定要看看爱站技术频道小编所分享的文章,具有比较高的参考价值,如果你需要记得前来查看学习。

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

 

push_right_in.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500"/>
</set> 

push_right_out.xml



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500"/>
</set> 

android怎么为activity开发手势识别的文章内容都分享出来了,需要的朋友可以直接复制粘贴使用,是不是很方便呢,来js.aizhan.com学习,每天都能让你进步一点点。

上一篇:android开发之多行标签热点的相关介绍

下一篇:Cocos2d-x内存管理详细介绍

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载