当前位置: 移动技术网 > 移动技术>移动开发>Android > Android手势左右滑动效果

Android手势左右滑动效果

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

最近想实现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;   
  } 
}

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

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

相关文章:

验证码:
移动技术网