Android 侧滑菜单实现

    第一篇技术文章,本着开源精神,欢迎大家讨论交流,如要转载或使用请注明出处,尊重知识版权 哈哈

   侧滑菜单如今比较流行应该工作需要,网上有各种实现方法,顺便自己实现了一种,中间有不足之处希望大家讨论修正.

  主要思路是重写RelativeLayout,两个RelatvieLayout叠加,上层的那个Layout经过重写改造成了滑动菜单,下方的作为背景,布局原封不动。

   下面是XML:

 



    

        
    

    

        

            
        
    




 

可侧滑的RelatvieLayout类

package com.superidea.slidmenu.widget;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

/**
 * 功能:侧滑菜单
 * @author huangyehui
 * 创建日期:2013.8.23
 */
public class SlidMenuLayout extends RelativeLayout implements OnTouchListener {

 private int mStartX = -1;
 private int mStartY = -1;
 private int mCurPosX = 0;
 private int mEndX = 0;
 private int mEndY = 0;
 private final static int RIGHTPOS = 50;

 public SlidMenuLayout(Context context) {
  super(context);
  init(context);
 }

 public SlidMenuLayout(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context);
 }
 
 public SlidMenuLayout(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init(context);
 }

 public void init(Context context)
 {
  setOnTouchListener(this);
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN: {
    Log.d("slidmenu", "down");
    mStartX = (int) event.getX();
    mStartY = (int) event.getY();
    break;
   }
   case MotionEvent.ACTION_MOVE: {
   
    Log.d("slidmenu", "move");
    mEndX = (int) event.getX();
    mEndY = (int) event.getY();
    setPadding((mCurPosX+mEndX-mStartX), 0, 0, 0);
    break;
   }
   case MotionEvent.ACTION_UP: {
    mCurPosX = mCurPosX+(mEndX-mStartX);
    int nDirect = directorDetector(mStartX, mEndX);
    int targetPosX = 0;
    if(nDirect == 1)
    {
     targetPosX = getWidth() - RIGHTPOS;
    }
    slideMenuByAnimation(mCurPosX, 0, targetPosX, 0);
    
    mStartX = 0;
    mStartY = 0;
    break;
   }
  }
  super.onTouchEvent(event);
  return false;
 }
 
 public int directorDetector(int nStart, int nEnd)
 {
  int nDirector = 0;  //0为左向,1为右向
  if(Math.abs(nEnd - nStart) < 10)
  {
   //滑动距离过短,逆向
   if(nEnd < nStart)
   {
    nDirector = 1;
   }
  }
  else if(nEnd > nStart)
  {
   nDirector = 1;
  }
  return nDirector;
 }

  public void slideMenuByAnimation(int fromX, int fromY , final int toX, final int toY)
     {
      
      ValueAnimator animation = ValueAnimator.ofInt(fromX, toX);
      animation.setDuration(200);
      animation.addUpdateListener(new AnimatorUpdateListener() 
      {  
       @Override  
       public void onAnimationUpdate(ValueAnimator animation) {  
        int temp = (Integer) animation.getAnimatedValue();
        setPadding(temp, 0, 0, 0);
        }});
      animation.addListener(new AnimatorListener(){

    @Override
    public void onAnimationCancel(Animator arg0) {
     // TODO Auto-generated method stub
     
    }

    @Override
    public void onAnimationEnd(Animator arg0) {
     // TODO Auto-generated method stub
     //重置当前控间位置
     mCurPosX = toX;
    }

    @Override
    public void onAnimationRepeat(Animator arg0) {
     // TODO Auto-generated method stub
     
    }

    @Override
    public void onAnimationStart(Animator arg0) {
     // TODO Auto-generated method stub
     
    }});
      animation.start();

     }
}

效果如下:

Android 侧滑菜单实现_第1张图片
 

 

思路比较简单就不仔细讲解了。 中间位了实现属性动画对低版本android版本支持,加入了 nineold的一个动画开源库。 使用时请注意~~~

 

你可能感兴趣的:(Android 侧滑菜单实现)