码迷,mamicode.com
首页 > 其他好文 > 详细

左右菜单

时间:2016-05-08 10:19:38      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

菜单布局

 

新建工程MyMenu

新建class MainUI作为主UI 继承自RelativeLayout

   private Context context;

   private FrameLayout leftMenu;

   private FrameLayout middleMenu;

   private FrameLayout rightMenu;

   public MainUI(Context context, AttributeSet attrs) {

      super(context, attrs);

      // TODO Auto-generated constructor stub

      initView(context);

   }

 

   public MainUI(Context context) {

      super(context);

      // TODO Auto-generated constructor stub

      initView(context);

   }

   public void initView(Context context){

      this.context = context;

      leftMenu = new FrameLayout(context);

      middleMenu = new FrameLayout(context);

      rightMenu = new FrameLayout(context);

      leftMenu.setBackgroundColor(Color.RED);

      middleMenu.setBackgroundColor(Color.GREEN);

      rightMenu.setBackgroundColor(Color.RED);

      addView(leftMenu);

      addView(middleMenu);

      addView(rightMenu);

   }

  

   //测量设置宽高

   @Override

   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

      // TODO Auto-generated method stub

      super.onMeasure(widthMeasureSpec, heightMeasureSpec);

      middleMenu.measure(widthMeasureSpec, heightMeasureSpec);

      int realWidth = MeasureSpec.getSize(widthMeasureSpec);

      int tempWidthMeasure = MeasureSpec.makeMeasureSpec((int)(realWidth*0.8f), MeasureSpec.EXACTLY);

      leftMenu.measure(tempWidthMeasure, heightMeasureSpec);

      rightMenu.measure(tempWidthMeasure, heightMeasureSpec);

   }

   //填充

   @Override

   protected void onLayout(boolean changed, int l, int t, int r, int b) {

      // TODO Auto-generated method stub

      super.onLayout(changed, l, t, r, b);

      middleMenu.layout(l, t, r, b);

      leftMenu.layout(l-leftMenu.getMeasuredWidth(), t, l, b);

      rightMenu.layout(r, t, r+rightMenu.getMeasuredWidth(), b);

   }

 

MainActivity文件

private MainUI mainUI;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      mainUI = new MainUI(this);

      setContentView(mainUI);

   }

左右滑动

监听事件,做事件处理 事件分发

判断上下事件还是左右事件还有点击事件

 

   private boolean isTestCompete;//测试值

   private boolean isleftrightEvent;

   @Override

   //事件分发

   public boolean dispatchTouchEvent(MotionEvent ev) {

      if (!isTestCompete) {

        //判断什么事件

        getEventType(ev);

        return true;

      }

      if (isleftrightEvent) {

        switch (ev.getActionMasked()) {

        case MotionEvent.ACTION_MOVE:

           int curScrollX = getScrollX();

           int dis_x = (int)(ev.getX() - point.x);

           System.out.println("curScrollX="+curScrollX +",dis_x="+dis_x );

           int expectX = -dis_x + curScrollX;

           int finalX=0;

           if (expectX < 0) {

              finalX = Math.max(expectX, -leftMenu.getMeasuredWidth());

           }else{

              finalX = Math.min(expectX, rightMenu.getMeasuredWidth());

           }

           scrollTo(finalX, 0);

           point.x = (int) ev.getX();

           break;

        case MotionEvent.ACTION_UP:

           isleftrightEvent = false;

           isTestCompete = false;

           break;

        case MotionEvent.ACTION_CANCEL:

           isleftrightEvent = false;

           isTestCompete = false;

           break;

 

        default:

           break;

        }

      }else{

        switch (ev.getActionMasked()) {

        case MotionEvent.ACTION_UP:

           isleftrightEvent = false;

           isTestCompete = false;

           break;

 

        default:

           break;

        }

      }

     

      return super.dispatchTouchEvent(ev);

   }

 

   private Point point = new Point();

  

   private static final int TEST_DIS = 20;

   private void getEventType(MotionEvent ev) {

      // TODO Auto-generated method stub

      switch (ev.getActionMasked()) {

      case MotionEvent.ACTION_DOWN:

        point.x = (int)ev.getX();

        point.y = (int)ev.getY();

        break;

      case MotionEvent.ACTION_MOVE:

        int dx = Math.abs((int)ev.getX()-point.x);

        int dy = Math.abs((int)ev.getY()-point.y);

        if (dx>=20 && dx>dy) {//左右滑动

           isleftrightEvent = true;

           isTestCompete = true;

        }else if(dy>=20 && dy>dx){//上下滑动

           isleftrightEvent = false;

           isTestCompete = true;

        }

        break;

      case MotionEvent.ACTION_UP:

       

        break;

      case MotionEvent.ACTION_CANCEL:

       

        break;

 

      default:

        break;

      }

   }

加入左右滑动动画

修改MainUI

添加代码

public class MainUI extends RelativeLayout{

   private Scroller mScroller;

  

   public void initView(Context context){

           //创建滑动动画

      mScroller = new Scroller(context, new DecelerateInterpolator());

     

   }

   //事件分发

   public boolean dispatchTouchEvent(MotionEvent ev) {

      if (!isTestCompete) {

        //判断什么事件

     

        case MotionEvent.ACTION_UP:

        case MotionEvent.ACTION_CANCEL:

           //手抬起时执行滑动动画

           curScrollX = getScrollX();

           if (Math.abs(curScrollX) > leftMenu.getMeasuredWidth() >> 1) {

              //执行左右滑动

              if (curScrollX < 0) {

                 //向右滑 出现左菜单

                 mScroller.startScroll(curScrollX, 0, -leftMenu.getMeasuredWidth()-curScrollX, 0);

              }else{

                 //向左滑 出现右菜单

                 mScroller.startScroll(curScrollX, 0, leftMenu.getMeasuredWidth()-curScrollX, 0);

              }

           }else{

              //返回原点

              mScroller.startScroll(curScrollX, 0, -curScrollX, 0);

           }

           //刷新 执行重绘

           invalidate();

           break;

 

        default:

           break;

        }

      }

     

        }

   //执行回调方法

   public void computeScroll() {

      // TODO Auto-generated method stub

      super.computeScroll();

      if (!mScroller.computeScrollOffset()) {

        return;

      }

      int tempX = mScroller.getCurrX();

      scrollTo(tempX, 0);

   }

}

添加点击事件

填充LeftMenu 新建class LeftMenu继承自Fragment

新建left.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <Button

        android:layout_height="wrap_content"

        android:layout_width="wrap_content"

        android:text="button"

        android:id="@+id/button"/>

   

 

</LinearLayout>

public class LeftMenu extends Fragment{

   @Override

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      // TODO Auto-generated method stub

      View v = inflater.inflate(R.layout.left, container, false);

      v.findViewById(R.id.button).setOnClickListener(new OnClickListener() {

       

        @Override

        public void onClick(View v) {

           // TODO Auto-generated method stub

           System.out.println("hello jikexueyuan");

        }

      });

      return v;

   }

}

修改MainActivity

public class MainActivity extends Activity {

 

   private MainUI mainUI;

   private LeftMenu leftMenu;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      mainUI = new MainUI(this);

      setContentView(mainUI);

      leftMenu = new LeftMenu();

      getFragmentManager().beginTransaction().add(mainUI.LEFT_ID, leftMenu).commit();

   }

}

修改MainUI

public class MainUI extends RelativeLayout{

 

   public static final int LEFT_ID = 0xaabbcc;

   public static final int MIDDLE_ID = 0xbbaacc;

   public static final int RIGHT_ID = 0xaaccbb;

public void initView(Context context){

     

      leftMenu.setId(LEFT_ID);

      middleMenu.setId(MIDDLE_ID);

      rightMenu.setId(RIGHT_ID);

}

   private void getEventType(MotionEvent ev) {

     

      switch (ev.getActionMasked()) {

      case MotionEvent.ACTION_DOWN:

       

        //处理点击事件

        super.dispatchTouchEvent(ev);

        break;

     

      case MotionEvent.ACTION_UP:

      case MotionEvent.ACTION_CANCEL:

        //处理点击事件

        super.dispatchTouchEvent(ev);

        isleftrightEvent = false;

        isTestCompete = false;

        break;

 

      default:

        break;

      }

   }

 

添加蒙版效果

修改MainUI

private FrameLayout middleMask;//蒙版

public void initView(Context context){

    middleMask = new FrameLayout(context);

middleMask.setBackgroundColor(0x88000000);

  addView(middleMask);

    middleMask.setAlpha(0);

  }

public void scrollTo(int x, int y) {

    // TODO Auto-generated method stub

    super.scrollTo(x, y);

    int curX = Math.abs(getScrollX());

    float scale = curX/(float)leftMenu.getMeasuredWidth();

    middleMask.setAlpha(scale);

  }

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

   

  middleMask.measure(widthMeasureSpec, heightMeasureSpec);

}

protected void onLayout(boolean changed, int l, int t, int r, int b) {

   

    middleMask.layout(l, t, r, b);

  }

左右菜单

标签:

原文地址:http://www.cnblogs.com/cityking/p/a024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!