当前位置: 移动技术网 > 移动技术>移动开发>Android > android开发之为activity增加左右手势识别示例

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网