当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现手势滑动(左滑和右滑)

Android实现手势滑动(左滑和右滑)

2020年08月17日  | 移动技术网IT编程  | 我要评论
最近想实现android左滑弹出菜单框,右滑消失菜单这个个功能。了解了一下android 的滑动事件,必须是在view组件或者activity上实现,同时必须实现ontouchlistener, on

最近想实现android左滑弹出菜单框,右滑消失菜单这个个功能。了解了一下android 的滑动事件,必须是在view组件或者activity上实现,同时必须实现ontouchlistener, ongesturelistener这个两个接口。

public class myrelativelayout extends relativelayout implements gesturedetector.ongesturelistener{
 private float mposx, mposy, mcurposx, mcurposy;
 private static final int fling_min_distance = 20;// 移动最小距离
 private static final int fling_min_velocity = 200;// 移动最大速度
 //构建手势探测器 
 gesturedetector mygesture = new gesturedetector(this);
 public myrelativelayout(context context){
 super(context)
 }

 public myrelativelayout(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 // todo auto-generated constructor stub
 }

 public myrelativelayout(context context, attributeset attrs) {
 super(context, attrs);
 // todo auto-generated constructor stub
 }
 @override
 public boolean ontouchevent(motionevent arg0) {
 // todo auto-generated method stub
 return mdetector.ontouchevent(arg0);

 }

 @override
 public boolean onsingletapup(motionevent e) {
 // todo auto-generated method stub
 return false;
 }

 @override
 public boolean onscroll(motionevent e1, motionevent e2, float distancex,
    float distancey) {
 // todo auto-generated method stub
 return false;
 }

 @override
 public boolean ondown(motionevent e) {
 // todo auto-generated method stub
 return false;
 }


 @override
 public void onshowpress(motionevent e) {
 // todo auto-generated method stub

 }

 @override
 public boolean onfling(motionevent e1, motionevent e2, float velocityx,
  float velocityy) {
 // todo auto-generated method stub
 // e1:第1个action_down motionevent 
 // e2:最后一个action_move motionevent 
 // velocityx:x轴上的移动速度(像素/秒) 
 // velocityy:y轴上的移动速度(像素/秒) 

 // x轴的坐标位移大于fling_min_distance,且移动速度大于fling_min_velocity个像素/秒 
 //向左 
 if (e1.gety() - e2.gety() > fling_min_distance){ 
//   && math.abs(velocityx) > fling_min_velocity) { 
  collapse();
  } 
 //向上 
 if (e2.gety() - e1.gety() > fling_min_distance 
   && math.abs(velocityx) > fling_min_velocity) {

 } 
  return false; 
 } 
}

再添加一段实现手势滑动效果:

手势滑动,其实也就是触摸事件

public class phoneguard01 extends activity {
 private gesturedetector mgesturedetector;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_phone_guard01);
 //创建手势识别对象,并创建手势识别的监听 
 mgesturedetector = new gesturedetector(this,new simpleongesturelistener(){

  //这个方法需要自己去重写
  @override
  public boolean onfling(motionevent e1, motionevent e2,
   float velocityx, float velocityy) {

  float x1=e1.getx();//获取按下去的坐标点,x轴
  float x2=e2.getx();//获取提起来的坐标点,y轴


  float y1=e1.gety();//获得按下去的y轴坐标点
  float y2=e1.gety();//获得提起来的y轴坐标点

  //y的移动距离,比x 的移动距离要大,所以不做任何的操作
  if(math.abs(y1-y2)>math.abs(x1-x2)){

   return false;
  }


  if(x1>x2){//表示下一页
   nextpage(null);
  }  
  return super.onfling(e1, e2, velocityx, velocityy);
  }

 });

 }

 /**
 下面代码的意思就是说,把自己的手势识别的触摸事件,
 让父类去调用

 */

 //ontouchevent(motionevent event)是继承来自view对象的
 @override
 public boolean ontouchevent(motionevent event) {
 //mgesturedetector.ontouchevent(event)是gesturedetector自己本身的
 mgesturedetector.ontouchevent(event);
 return super.ontouchevent(event);
 }

//-----------------上面就是手势识别的代码实现------------------------------
 //跳转到下一个页面
 public void nextpage(view v){ 
 intent intent=new intent(this,phoneguard02.class);

 startactivity(intent);
 finish();

 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网