欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-04 12:16:43
android开发中为activity增加左右手势识别,如右滑关闭当前页面。 复制代码 代码如下:/* *  for左右手势 * ...

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>