Esempio n. 1
0
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentActivityScaleX = ViewHelper.getScaleX(viewActivity);
    if (currentActivityScaleX == 1.0f) setScaleDirectionByRawX(ev.getRawX());

    switch (ev.getAction()) {
      case MotionEvent.ACTION_DOWN:
        lastActionDownX = ev.getX();
        lastActionDownY = ev.getY();
        isInIgnoredView = isInIgnoredView(ev) && !isOpened();
        pressedState = PRESSED_DOWN;
        break;

      case MotionEvent.ACTION_MOVE:
        if (isInIgnoredView || isInDisableDirection(scaleDirection)) break;

        if (pressedState != PRESSED_DOWN && pressedState != PRESSED_MOVE_HORIZONTAL) break;

        int xOffset = (int) (ev.getX() - lastActionDownX);
        int yOffset = (int) (ev.getY() - lastActionDownY);

        if (pressedState == PRESSED_DOWN) {
          if (yOffset > 25 || yOffset < -25) {
            pressedState = PRESSED_MOVE_VERTICAL;
            break;
          }
          if (xOffset < -50 || xOffset > 50) {
            pressedState = PRESSED_MOVE_HORIZONTAL;
            ev.setAction(MotionEvent.ACTION_CANCEL);
          }
        } else if (pressedState == PRESSED_MOVE_HORIZONTAL) {
          if (currentActivityScaleX < 0.95) showScrollViewMenu(scrollViewMenu);

          float targetScale = getTargetScale(ev.getRawX());
          if (mUse3D) {
            int angle = scaleDirection == DIRECTION_LEFT ? -ROTATE_Y_ANGLE : ROTATE_Y_ANGLE;
            angle *= (1 - targetScale) * 2;
            ViewHelper.setRotationY(viewActivity, angle);

            ViewHelper.setScaleX(imageViewShadow, targetScale - shadowAdjustScaleX);
            ViewHelper.setScaleY(imageViewShadow, targetScale - shadowAdjustScaleY);
          } else {
            ViewHelper.setScaleX(imageViewShadow, targetScale + shadowAdjustScaleX);
            ViewHelper.setScaleY(imageViewShadow, targetScale + shadowAdjustScaleY);
          }
          ViewHelper.setScaleX(viewActivity, targetScale);
          ViewHelper.setScaleY(viewActivity, targetScale);
          ViewHelper.setAlpha(scrollViewMenu, (1 - targetScale) * 2.0f);

          lastRawX = ev.getRawX();
          return true;
        }

        break;

      case MotionEvent.ACTION_UP:
        if (isInIgnoredView) break;
        if (pressedState != PRESSED_MOVE_HORIZONTAL) break;

        pressedState = PRESSED_DONE;
        if (isOpened()) {
          if (currentActivityScaleX > 0.56f) closeMenu();
          else openMenu(scaleDirection);
        } else {
          if (currentActivityScaleX < 0.94f) {
            openMenu(scaleDirection);
          } else {
            closeMenu();
          }
        }

        break;
    }
    lastRawX = ev.getRawX();
    return super.dispatchTouchEvent(ev);
  }
Esempio n. 2
0
  @SuppressLint("ResourceAsColor")
  @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
  @Override
  public boolean onTouch(final View view, MotionEvent event) {
    if (mViewWidth < 2) {
      mViewWidth = mListView.getWidth();
      smallWidth = mViewWidth / 7;
      textwidth2 = mViewWidth / 3;
      textwidth = textwidth2;
      //            原版
      //            largewidth	=	textwidth+textwidth2;
      //           自己修改
      largewidth = textwidth;
    }

    int tempwidth = 0;
    if (SwipeType == 1) tempwidth = smallWidth;
    else tempwidth = textwidth2 / 2;

    switch (event.getActionMasked()) {
      case MotionEvent.ACTION_DOWN:
        {
          if (mPaused) {
            return false;
          }
          Rect rect = new Rect();
          int childCount = mListView.getChildCount();
          int[] listViewCoords = new int[2];
          mListView.getLocationOnScreen(listViewCoords);
          int x = (int) event.getRawX() - listViewCoords[0];
          int y = (int) event.getRawY() - listViewCoords[1];
          ViewGroup child;
          for (int i = 0; i < childCount; i++) {
            child = (ViewGroup) mListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
              mDownView_parent = child;
              mDownView = (ViewGroup) child.findViewById(R.id.list_display_view_container);
              if (mDownView_parent.getChildCount() == 1) {
                textheight = mDownView_parent.getHeight();
                if (SwipeType == Dismiss) {
                  HalfColor = singleColor;
                  //                                HalfDrawable	=
                  //	activity.getResources().getDrawable(R.drawable.content_discard);
                }
                SetBackGroundforList();
              }

              if (old_mDownView != null && mDownView != old_mDownView) {
                ResetListItem(old_mDownView);
                old_mDownView = null;
                return false;
              }
              break;
            }
          }

          if (mDownView != null) {
            mDownX = event.getRawX();
            mDownY = event.getRawY();
            mDownPosition = mListView.getPositionForView(mDownView);
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(event);
          } else {
            mDownView = null;
          }

          // mSwipeDetected              =   false;
          temp_position = mListView.pointToPosition((int) event.getX(), (int) event.getY());
          view.onTouchEvent(event);
          return true;
        }

      case MotionEvent.ACTION_UP:
        {
          if (mVelocityTracker == null) {
            break;
          }
          float deltaX = event.getRawX() - mDownX;
          // 在OnItemClick事件中判断如果Deltax!=0则表示不是点击事件
          DeltaX = deltaX;
          mVelocityTracker.addMovement(event);
          mVelocityTracker.computeCurrentVelocity(1000); // 1000 by defaut but
          float velocityX = mVelocityTracker.getXVelocity(); // it was too much
          float absVelocityX = Math.abs(velocityX);
          float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
          boolean swipe = false;
          boolean swipeRight = false;

          if (Math.abs(deltaX) > tempwidth) {
            swipe = true;
            swipeRight = deltaX > 0;

          } else if (mMinFlingVelocity <= absVelocityX
              && absVelocityX <= mMaxFlingVelocity
              && absVelocityY < absVelocityX) {
            // dismiss only if flinging in the same direction as dragging
            swipe = (velocityX < 0) == (deltaX < 0);
            swipeRight = mVelocityTracker.getXVelocity() > 0;
          }
          if (deltaX < 0 && swipe) {
            mListView.setDrawSelectorOnTop(false);
            // && Math.abs(DeltaY)<30
            if (swipe && !swipeRight && deltaX <= -tempwidth && Math.abs(DeltaY) < 100) {
              FullSwipeTrigger();
            } else if (deltaX >= -textwidth && SwipeType == Double) {
              ResetListItem(mDownView);
            } else {
              ResetListItem(mDownView);
            }
          } else if (deltaX != 0) {
            ResetListItem(mDownView);
          }

          mVelocityTracker.recycle();
          mVelocityTracker = null;
          mDownX = 0;
          mDownView = null;
          mDownPosition = ListView.INVALID_POSITION;
          mSwiping = false;
          break;
        }

        // 根据手势滑动方向滑出滑入

      case MotionEvent.ACTION_MOVE:
        {
          float deltaX = event.getRawX() - mDownX;
          float deltaY = event.getRawY() - mDownY;
          DeltaY = deltaY;
          if (mVelocityTracker == null || mPaused || deltaX > 0) {
            break;
          }

          mVelocityTracker.addMovement(event);

          // && Math.abs(deltaY)<30
          if (Math.abs(deltaX) > (mSlop * 5) && Math.abs(deltaY) < 50) {
            mSwiping = true;
            mListView.requestDisallowInterceptTouchEvent(true);

            // Cancel ListView's touch (un-highlighting the item)
            MotionEvent cancelEvent = MotionEvent.obtain(event);
            cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL
                    | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            mListView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
          } else if (Math.abs(deltaY) > 50) {
            mSwiping = false;
            //                     ResetListItem(mDownView);
          }
          System.out.println(
              "<<<<<<<<<" + deltaY + "<<<<<<" + deltaX + "<<<<" + mSwiping + "<<<<<" + mSlop * 10);
          if (mSwiping && deltaX < 0) {
            int width;
            //                     if(SwipeType==1){
            width = textwidth2;
            //                     }
            //                     else{
            //                         width	=	largewidth;
            //                     }

            if (-deltaX < width) {

              mDownView.setTranslationX(deltaX);
              return false;
            }
            return false;
          } else if (mSwiping) {
            ResetListItem(mDownView);
          }
          break;
        }
    }
    return false;
  }