Android自定义侧滑菜单slidmenu(2)

/**
  * @see android.view.View#onTouchEvent(android.view.MotionEvent)
  * 手势处理
  * 这里去处理触屏操作
  */
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  int action = event.getAction();
  float x = event.getX();
  float y = event.getY();
  if (mVelocityTracker == null) {
   mVelocityTracker = VelocityTracker.obtain();
   mVelocityTracker.addMovement(event);
  }

switch (action) {
  case MotionEvent.ACTION_DOWN:
   if (!mScroller.isFinished()) {
    mScroller.abortAnimation();
   }
   mLastMotionX = x;
   mLastMotionY = y;
   System.out.println("mSlidView-scrollX-->" + mSlidView.getScrollX());
   System.out.println("mMenuView-Width-->" + getMenuViewWidth());
   if (mSlidView.getScrollX() == -getMenuViewWidth()
     && mLastMotionX < getMenuViewWidth()) {
    // return false;
   }

break;
  case MotionEvent.ACTION_MOVE:
   if (mIsBeingDragged) {
    float delateX = mLastMotionX - x;
    mLastMotionX = x;
    if (!(mSlidView.getScrollX() < 0)) {
     if (delateX > 0) {
      break;
     }
    }

if ((mSlidView.getScrollX() <= 0)
      && (mSlidView.getScrollX() >= -(ConstantQuantity.screenWidth - ConstantQuantity.screenDensity * 50))) {
     mSlidView.scrollBy((int) delateX, 0);
    }

System.out.println("delateX--->" + delateX);
    System.out.println("mSlidView.ScrollX--->"
      + mSlidView.getScrollX());
   }

if (mSlidView.getScrollX() >= 0) {
    return false;
   }

if (mSlidView != null) {
    bgShade.scrollTo(mSlidView.getScrollX() + 10, 0);
   }
   break;
  case MotionEvent.ACTION_CANCEL:
  case MotionEvent.ACTION_UP:
   int velocityX = 0;
   if (mVelocityTracker != null) {
    mVelocityTracker.addMovement(event);
    mVelocityTracker.computeCurrentVelocity(1000);
    velocityX = (int) mVelocityTracker.getXVelocity();
   }
   //当离开屏幕时根据滑动的速度来判断菜单是否打开或关闭
   showVelocityMenuView(velocityX);
   if (mVelocityTracker != null) {
    mVelocityTracker.recycle();
    mVelocityTracker = null;
   }
   break;
  }

return true;
 }

/**
  * @param view
  * 设置侧边菜单布局
  */
 public void setMenuView(View view) {
  LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.MATCH_PARENT);
  addView(view, behindParams);
  mMenuView = view;

}

/**
  * @param view
  * 设置中间滑动布局 设置中间滑动布局的同时,增加一个背景布局,用来显示侧边阴影!增加层级立体效果
  */
 public void setSlidView(View view) {
  LayoutParams bgShadeParams = new LayoutParams(
    ConstantQuantity.screenWidth, ConstantQuantity.screenHeight);
  bgShadeParams.addRule(RelativeLayout.CENTER_IN_PARENT);
  bgShade.setLayoutParams(bgShadeParams);

LayoutParams bgParams = new LayoutParams(ConstantQuantity.screenWidth,
    ConstantQuantity.screenHeight - 48);
  bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);

View bgShadeContent = new View(mContext);

bgShadeContent.setBackgroundResource(R.drawable.view_left_bg);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/862a9c5f591755ad5b432c0abcb43606.html