Java Code Examples for android.view.MotionEvent#getActionMasked()

The following examples show how to use android.view.MotionEvent#getActionMasked() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FriendRefreshView.java    From LQRWeChat with MIT License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    mDragHelper.processTouchEvent(event);
    final int action = event.getActionMasked();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_UP:
            mLastMotionY = 0;
            bDraging = false;
            bScrollDown = false;
            rainbowRotateAngle = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            int index = MotionEventCompat.getActionIndex(event);
            int pointerId = MotionEventCompat.getPointerId(event, index);
            if (shouldIntercept()) {
                mDragHelper.captureChildView(mContentView, pointerId);
            }
            break;
    }
    return true;
}
 
Example 2
Source File: DragLayout.java    From timecat with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // 1. detector和mDragHelper判断是否需要拦截
    boolean yScroll = moveDetector.onTouchEvent(ev);
    boolean shouldIntercept = false;
    try {
        shouldIntercept = mDragHelper.shouldInterceptTouchEvent(ev);
    } catch (Exception e) {
    }

    // 2. 触点按下的时候直接交给mDragHelper
    int action = ev.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN) {
        downState = getCurrentState();
        mDragHelper.processTouchEvent(ev);
    }

    return shouldIntercept && yScroll;
}
 
Example 3
Source File: SweetSnackbar.java    From SweetTips with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, SnackbarLayout child,
        MotionEvent event) {
    // We want to make sure that we disable any SweetSnackbar timeouts if the user is
    // currently touching the SweetSnackbar. We restore the timeout when complete
    if (parent.isPointInChildBounds(child, (int) event.getX(), (int) event.getY())) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                SweetSnackbarManager.getInstance().cancelTimeout(mManagerCallback);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                SweetSnackbarManager.getInstance().restoreTimeout(mManagerCallback);
                break;
        }
    }

    return super.onInterceptTouchEvent(parent, child, event);
}
 
Example 4
Source File: MongolTextView.java    From mongol-library with MIT License 6 votes vote down vote up
@SuppressLint("ClickableViewAccessibility") // todo support accessibility
@Override
public boolean onTouchEvent(MotionEvent event) {

    // this method is currently only for detecting a ClickableSpan
    if (mMovementMethod == null)
        return super.onTouchEvent(event);

    if (event.getActionMasked() == MotionEvent.ACTION_UP) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int offset = getOffsetForPosition(x, y);

        final ClickableSpan[] links = mTextStorage.getSpans(offset, offset, ClickableSpan.class);

        if (links.length > 0) {
            links[0].onClick(this);
        }
    }

    return true;
}
 
Example 5
Source File: WXGesture.java    From weex with Apache License 2.0 5 votes vote down vote up
/**
 * Tell whether the number of motion event's pointer changed.
 * @param event the current motion event
 * @return true for number of motion event's pointer changed, otherwise false.
 */
private boolean isPointerNumChanged(MotionEvent event) {
  return event.getActionMasked() == MotionEvent.ACTION_DOWN ||
         event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN ||
         event.getActionMasked() == MotionEvent.ACTION_UP ||
         event.getActionMasked() == MotionEvent.ACTION_POINTER_UP ||
         event.getActionMasked() == MotionEvent.ACTION_CANCEL;
}
 
Example 6
Source File: SampleCameraGLView.java    From GPUVideo-android with MIT License 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    final int actionMasked = event.getActionMasked();
    if (actionMasked != MotionEvent.ACTION_DOWN) {
        return false;
    }

    if (touchListener != null) {
        touchListener.onTouch(event, v.getWidth(), v.getHeight());
    }
    return false;
}
 
Example 7
Source File: RearrangeableLayout.java    From RearrangeableLayout with MIT License 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    float x = ev.getX();
    float y= ev.getY();
    if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
        prepareTouch(x, y);
        return true;
    }
    return false;
}
 
Example 8
Source File: VerticalTouchConsumer.java    From SlideUp-Android with MIT License 5 votes vote down vote up
boolean consumeTopToBottom(View touchedView, MotionEvent event){
    float touchedArea = event.getY();
    switch (event.getActionMasked()){
        case MotionEvent.ACTION_DOWN:
            mViewHeight = mBuilder.mSliderView.getHeight();
            mStartPositionY = event.getRawY();
            mViewStartPositionY = mBuilder.mSliderView.getTranslationY();
            mCanSlide = touchFromAlsoSlide(touchedView, event);
            mCanSlide |= getBottom() - mBuilder.mTouchableArea <= touchedArea;
            break;
        case MotionEvent.ACTION_MOVE:
            float difference = event.getRawY() - mStartPositionY;
            float moveTo = mViewStartPositionY + difference;
            float percents = moveTo * 100 / -mBuilder.mSliderView.getHeight();
            calculateDirection(event);
        
            if (moveTo < 0 && mCanSlide){
                mNotifier.notifyPercentChanged(percents);
                mBuilder.mSliderView.setTranslationY(moveTo);
            }
            break;
        case MotionEvent.ACTION_UP:
            float slideAnimationFrom = -mBuilder.mSliderView.getTranslationY();
            if (slideAnimationFrom == mViewStartPositionY){
                return !Internal.isUpEventInView(mBuilder.mSliderView, event);
            }
            boolean scrollableAreaConsumed = mBuilder.mSliderView.getTranslationY() < -mBuilder.mSliderView.getHeight() / 5;
        
            if (scrollableAreaConsumed && mGoingUp){
                mAnimationProcessor.setValuesAndStart(slideAnimationFrom, mBuilder.mSliderView.getHeight() + mBuilder.mSliderView.getTop());
            }else {
                mAnimationProcessor.setValuesAndStart(slideAnimationFrom, 0);
            }
            mCanSlide = true;
            break;
    }
    mPrevPositionY = event.getRawY();
    mPrevPositionX = event.getRawX();
    return true;
}
 
Example 9
Source File: SwipeLayout.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isSwipeEnabled()) return super.onTouchEvent(event);

    int action = event.getActionMasked();
    gestureDetector.onTouchEvent(event);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mDragHelper.processTouchEvent(event);
            sX = event.getRawX();
            sY = event.getRawY();


        case MotionEvent.ACTION_MOVE: {
            //the drag state and the direction are already judged at onInterceptTouchEvent
            checkCanDrag(event);
            if (mIsBeingDragged) {
                getParent().requestDisallowInterceptTouchEvent(true);
                mDragHelper.processTouchEvent(event);
            }
            break;
        }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIsBeingDragged = false;
            mDragHelper.processTouchEvent(event);
            break;

        default://handle other action, such as ACTION_POINTER_DOWN/UP
            mDragHelper.processTouchEvent(event);
    }

    return super.onTouchEvent(event) || mIsBeingDragged || action == MotionEvent.ACTION_DOWN;
}
 
Example 10
Source File: EdgeSwipeEventFilter.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEventInternal(MotionEvent e, boolean isKeyboardShowing) {
    if (mTabModelSelector == null) return false;

    mPropagateEventsToHostView = true;

    mInLongPress = false;
    final int count = mTabModelSelector.getCurrentModel().getCount();
    final int action = e.getActionMasked();
    if (mEnableTabSwiping
            && !isKeyboardShowing
            && action == MotionEvent.ACTION_DOWN
            && count > 0
            && mScrollDirection == ScrollDirection.UNKNOWN) {
        ScrollDirection direction = ScrollDirection.UNKNOWN;
        if ((e.getX() + mCurrentTouchOffsetX) * mPxToDp < SWIPE_REGION_DP) {
            direction = ScrollDirection.RIGHT;
        } else if (mHost.getViewportWidth() * mPxToDp
                - (e.getX() + mCurrentTouchOffsetX) * mPxToDp < SWIPE_REGION_DP) {
            direction = ScrollDirection.LEFT;
        } else if ((e.getY() + mCurrentTouchOffsetY) * mPxToDp < SWIPE_REGION_DP) {
            direction = ScrollDirection.DOWN;
        }

        // Check if we have a new direction and that it's supported.
        if (direction != ScrollDirection.UNKNOWN
                && (mEdgeSwipeHandler == null || mEdgeSwipeHandler.isSwipeEnabled(direction))) {
            mScrollDirection = direction;
        }

        if (mScrollDirection != ScrollDirection.UNKNOWN) mPropagateEventsToHostView = false;
    }

    return true;
}
 
Example 11
Source File: BottomSheetCoordinatorBehavior.java    From BottomSheetCoordinatorLayout with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, BottomSheetCoordinatorLayout sheet, MotionEvent event) {
    if (!sheet.hasAppBar()) return super.onInterceptTouchEvent(parent, sheet, event);

    // If the touch is not on the sheet, we don't care.
    if (!parent.isPointInChildBounds(sheet, (int) event.getX(), (int) event.getY())) {
        return super.onInterceptTouchEvent(parent, sheet, event);
    }

    updateDirection(event);
    if (sheet.getState() == BottomSheetCoordinatorBehavior.STATE_EXPANDED) {
        // If finger is going down and
        if (!sheet.canScrollUp()) {
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                // Pass to both, we don't know yet what will happen.
                super.onInterceptTouchEvent(parent, sheet, event);
                return false;
                // return false;
            } else if (fingerDown) {
                // Not a DOWN and finger is going down. Intercept.
                return super.onInterceptTouchEvent(parent, sheet, event);
            } else {
                // Not a DOWN and finger is going up. propagate to ABL.
                return false;
            }
        } else {
            // Expanded, but ABL is not expanded. It should catch events.
            return false;
        }
    } else {
        // Sheet is not expanded. It should catch events.
        return super.onInterceptTouchEvent(parent, sheet, event);
    }
}
 
Example 12
Source File: PhotoFilterView.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public void onTouch(MotionEvent event) {
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN || event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        LayoutParams layoutParams = (LayoutParams) textureView.getLayoutParams();
        if (layoutParams != null && event.getX() >= layoutParams.leftMargin && event.getY() >= layoutParams.topMargin && event.getX() <= layoutParams.leftMargin + layoutParams.width && event.getY() <= layoutParams.topMargin + layoutParams.height) {
            setShowOriginal(true);
        }
    } else if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
        setShowOriginal(false);
    }
}
 
Example 13
Source File: ColorPicker.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getPointerCount() > 1) {
        return false;
    }

    float x = event.getX() - rectF.left;
    float y = event.getY() - rectF.top;

    if (!interacting && y < -AndroidUtilities.dp(10)) {
        return false;
    }

    int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
        if (interacting && delegate != null) {
            delegate.onFinishedColorPicking();

            getContext().getSharedPreferences("paint", Activity.MODE_PRIVATE).edit().putFloat("last_color_location", location).commit();
        }
        interacting = false;
        wasChangingWeight = changingWeight;
        changingWeight = false;
        setDragging(false, true);
    } else if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
        if (!interacting) {
            interacting = true;
            if (delegate != null) {
                delegate.onBeganColorPicking();
            }
        }

        float colorLocation = Math.max(0.0f, Math.min(1.0f, x / rectF.width()));
        setLocation(colorLocation);

        setDragging(true, true);

        if (y < -AndroidUtilities.dp(10)) {
            changingWeight = true;
            float weightLocation = (-y - AndroidUtilities.dp(10)) / AndroidUtilities.dp(190);
            weightLocation = Math.max(0.0f, Math.min(1.0f, weightLocation));
            setWeight(weightLocation);
        }

        if (delegate != null) {
            delegate.onColorValueChanged();
        }
        return true;
    }
    return false;
}
 
Example 14
Source File: TouchEffectAnimator.java    From FamilyChat with Apache License 2.0 4 votes vote down vote up
public void onTouchEvent(final MotionEvent event) {

        if (event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
            isTouchReleased = true;
            if (!isAnimatingFadeIn) {
                fadeOutEffect();
            }
        }
        if (event.getActionMasked() == MotionEvent.ACTION_UP) {
            isTouchReleased = true;
            if (!isAnimatingFadeIn) {
                fadeOutEffect();
            }
        } else if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {

            // gets the bigger value (width or height) to fit the circle
            requiredRadius = mView.getWidth() > mView.getHeight() ? mView.getWidth() : mView.getHeight();

            // increasing radius for circle to reach from corner to other corner
            requiredRadius *= 1.2;

            isTouchReleased = false;
            mDownX = event.getX();
            mDownY = event.getY();

            mCircleAlpha = MAX_RIPPLE_ALPHA;
            mRectAlpha = 0;

            ValueGeneratorAnim valueGeneratorAnim = new ValueGeneratorAnim(new InterpolatedTimeCallback() {
                @Override
                public void onTimeUpdate(float interpolatedTime) {
                    if (hasRippleEffect)
                        mRadius = requiredRadius * interpolatedTime;
                    mRectAlpha = (int) (interpolatedTime * MAX_RIPPLE_ALPHA);
                    mView.invalidate();
                }
            });
            valueGeneratorAnim.setInterpolator(new DecelerateInterpolator());
            valueGeneratorAnim.setDuration(animDuration);
            valueGeneratorAnim.setAnimationListener(animationListener);
            mView.startAnimation(valueGeneratorAnim);
        }
    }
 
Example 15
Source File: TouchEffectAnimator.java    From UPMiss with GNU General Public License v3.0 4 votes vote down vote up
public void onTouchEvent(final MotionEvent event) {
    // On disEnable return
    if (!mView.isEnabled())
        return;

    if (event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
        isTouchReleased = true;
        if (!isAnimatingFadeIn) {
            fadeOutEffect();
        }
    }
    if (event.getActionMasked() == MotionEvent.ACTION_UP) {
        isTouchReleased = true;
        if (!isAnimatingFadeIn) {
            fadeOutEffect();
        }
    } else if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        // Init
        initTouch();

        // Set default operation to fadeOutEffect()
        isTouchReleased = false;
        isAnimatingFadeIn = true;

        // Set this start point
        mPaintX = mDownX = event.getX();
        mPaintY = mDownY = event.getY();

        // This color alpha
        mBackAlpha = 0;
        mRippleAlpha = 255;

        // Gets the bigger value (width or height) to fit the circle
        mEndRadius = mCenterX > mCenterY ? mCenterX : mCenterY;
        mStartRadius = 0;
        mRadius = 0;

        // This circle radius is 78% 90% or fill all
        switch (mTouchEffect) {
            case RIPPLE:
                float x = mDownX < mCenterX ? 2 * mCenterX : 0;
                float y = mDownY < mCenterY ? 2 * mCenterY : 0;
                mEndRadius = (float) Math.sqrt((x - mDownX) * (x - mDownX) + (y - mDownY) * (y - mDownY));
                break;
            case MOVE:
                mStartRadius = 0;
                mEndRadius *= 0.78;
                break;
            case PRESS:
                mStartRadius = mEndRadius * 0.68f;
                mEndRadius *= 0.98;
                mPaintX = mCenterX;
                mPaintY = mCenterY;
                break;
        }

        // Cancel and Start new animation
        cancelEffect();
        fadeInEffect();
    }
}
 
Example 16
Source File: MultiTouchView2.java    From PlusDemo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    float sumX = 0;
    float sumY = 0;

    boolean pointerUp = event.getActionMasked() == MotionEvent.ACTION_POINTER_UP;
    int skippedPointer = pointerUp ? event.getActionIndex() : -1; // 计算焦点时跳过抬起的手指
    int count = event.getPointerCount();
    for (int i = 0; i < count; i++) {
        if (skippedPointer != i) { // 跳过抬起的手指
            sumX += event.getX(i);
            sumY += event.getY(i);
        }
    }
    int divisor = pointerUp ? count - 1 : count; // 如果是 ACTION_POINTER_UP 事件,除数减一
    float focusX = sumX / divisor;
    float focusY = sumY / divisor;

    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            downX = focusX;
            downY = focusY;
            oldOffsetX = offsetX;
            oldOffsetY = offsetY;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            downX = focusX;
            downY = focusY;
            oldOffsetX = offsetX;
            oldOffsetY = offsetY;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            downX = focusX;
            downY = focusY;
            oldOffsetX = offsetX;
            oldOffsetY = offsetY;
            break;
        case MotionEvent.ACTION_MOVE:
            float deltaX = focusX - downX;
            float deltaY = focusY - downY;
            offsetX = oldOffsetX + deltaX;
            offsetY = oldOffsetY + deltaY;
            invalidate();
            break;
    }

    return true;
}
 
Example 17
Source File: TimeRuleView.java    From RulerView with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    final int actionIndex = event.getActionIndex();
    int pointerId = event.getPointerId(actionIndex);
    final int actionMasked = event.getActionMasked();
    final int action = event.getAction();
    final int pointerCount = event.getPointerCount();
    logD("onTouchEvent: isScaling=%b, actionIndex=%d, pointerId=%d, actionMasked=%d, action=%d, pointerCount=%d",
            isScaling, actionIndex, pointerId, actionMasked, action, pointerCount);
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    mScaleGestureDetector.onTouchEvent(event);

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    switch (actionMasked) {
        case MotionEvent.ACTION_DOWN:
            isMoving = false;
            mInitialX = x;
            if (!mScroller.isFinished()) {
                mScroller.forceFinished(true);
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            // 只要第二手指按下,就禁止滑动
            isScaling = true;
            isMoving = false;
            break;
        case MotionEvent.ACTION_MOVE:
            if (isScaling) {
                break;
            }
            int dx = x - mLastX;
            if (!isMoving) {
                final int dy = y - mLastY;
                if (Math.abs(x - mInitialX) <= SCROLL_SLOP || Math.abs(dx) <= Math.abs(dy)) {
                    break;
                }
                isMoving = true;
            }
            mCurrentDistance -= dx;
            computeTime();
            break;
        case MotionEvent.ACTION_UP:
            if (isScaling || !isMoving) {
                break;
            }
            mVelocityTracker.computeCurrentVelocity(1000, MAX_VELOCITY);
            final int xVelocity = (int) mVelocityTracker.getXVelocity();
            if (Math.abs(xVelocity) >= MIN_VELOCITY) {
                // 惯性滑动
                final int maxDistance = (int) (MAX_TIME_VALUE / mUnitGap * mUnitGap);
                mScroller.fling((int) mCurrentDistance, 0, -xVelocity, 0, 0, maxDistance, 0, 0);
                invalidate();
            }
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // 两个中的有一个手指被抬起,允许滑动。同时把未抬起的手机当前位置赋给初始X
            isScaling = false;
            int restIndex = actionIndex == 0 ? 1 : 0;
            mInitialX = (int) event.getX(restIndex);
            break;
        default: break;
    }
    mLastX = x;
    mLastY = y;
    return true;
}
 
Example 18
Source File: DeleteView.java    From budget-envelopes with GNU General Public License v3.0 4 votes vote down vote up
@Override public boolean onTouchEvent(MotionEvent event) {
    float x = event.getRawX();
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            switch (mSwipeState) {
                case STATE_READY:
                    mSwipeState = STATE_PRESSED;
                    mSwipeStart = x;
                    mSwipeStartTime = event.getEventTime();
                    removeCallbacks(mUnpressRunnable);
                    postDelayed(mPressRunnable, mPressTimeout);
                    postDelayed(mLongPressRunnable, mLongPressTimeout);
                    break;
                case STATE_ANIMATING:
                    cancelAnimation(x);
                    break;
                case STATE_DELETING:
                    // Do nothing.
                    break;
                default:
                    throw new Error("Invalid state with ACTION_DOWN: "+mSwipeState);
            }
            break;
        case MotionEvent.ACTION_MOVE:
            switch (mSwipeState) {
                case STATE_PRESSED:
                    if (Math.abs(x - mSwipeStart) > mTouchSlop) {
                        cancelPressed();
                        startSwipe();
                    }
                    break;
                case STATE_IN_SWIPE:
                    setInnerViewPosition(event.getRawX() - mSwipeStart);
                    mVelocityTracker.addMovement(event);
                    break;
                case STATE_ANIMATING:
                    cancelAnimation(x);
                    break;
                case STATE_DELETING:
                    // Do nothing.
                    break;
                default:
                    throw new Error("Invalid state with ACTION_MOVE: "+mSwipeState);
            }
            break;
        case MotionEvent.ACTION_UP:
            switch (mSwipeState) {
                case STATE_PRESSED:
                    if (event.getEventTime() - mSwipeStartTime > mLongPressTimeout) {
                        // Long click is already performed.
                    } else {
                        mInnerView.setPressed(true);
                        performClick();
                    }
                    cancelPressed();
                    mSwipeState = STATE_READY;
                    break;
                case STATE_IN_SWIPE:
                    startAnimation();
                    break;
                case STATE_ANIMATING:
                case STATE_DELETING:
                    // Do nothing.
                    break;
                default:
                    throw new Error("Invalid state with ACTION_UP: "+mSwipeState);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            switch (mSwipeState) {
                case STATE_PRESSED:
                    cancelPressed();
                    mSwipeState = STATE_READY;
                    break;
                case STATE_IN_SWIPE:
                    startAnimation();
                    break;
                case STATE_READY:
                case STATE_ANIMATING:
                case STATE_DELETING:
                    // Do nothing.
                    break;
                default:
                    throw new Error("Invalid state with ACTION_CANCEL: "+mSwipeState);
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_POINTER_UP:
            // We don't do anything with multitouch.
            break;
        default:
            throw new Error("Invalid MotionEvent: "+event.getActionMasked());
    }

    Log.d("Budget", "onTouchEvent(): "+mSwipeState);

    return true;
}
 
Example 19
Source File: DropDownListView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Handles forwarded events.
 *
 * @param activePointerId id of the pointer that activated forwarding
 * @return whether the event was handled
 */
public boolean onForwardedEvent(@NonNull MotionEvent event, int activePointerId) {
    boolean handledEvent = true;
    boolean clearPressedItem = false;

    final int actionMasked = event.getActionMasked();
    switch (actionMasked) {
        case MotionEvent.ACTION_CANCEL:
            handledEvent = false;
            break;
        case MotionEvent.ACTION_UP:
            handledEvent = false;
            // $FALL-THROUGH$
        case MotionEvent.ACTION_MOVE:
            final int activeIndex = event.findPointerIndex(activePointerId);
            if (activeIndex < 0) {
                handledEvent = false;
                break;
            }

            final int x = (int) event.getX(activeIndex);
            final int y = (int) event.getY(activeIndex);
            final int position = pointToPosition(x, y);
            if (position == INVALID_POSITION) {
                clearPressedItem = true;
                break;
            }

            final View child = getChildAt(position - getFirstVisiblePosition());
            setPressedItem(child, position, x, y);
            handledEvent = true;

            if (actionMasked == MotionEvent.ACTION_UP) {
                final long id = getItemIdAtPosition(position);
                performItemClick(child, position, id);
            }
            break;
    }

    // Failure to handle the event cancels forwarding.
    if (!handledEvent || clearPressedItem) {
        clearPressedItem();
    }

    // Manage automatic scrolling.
    if (handledEvent) {
        if (mScrollHelper == null) {
            mScrollHelper = new AbsListViewAutoScroller(this);
        }
        mScrollHelper.setEnabled(true);
        mScrollHelper.onTouch(this, event);
    } else if (mScrollHelper != null) {
        mScrollHelper.setEnabled(false);
    }

    return handledEvent;
}
 
Example 20
Source File: OverlayPanelEventFilter.java    From delion with Apache License 2.0 4 votes vote down vote up
/**
 * Propagates the given {@link MotionEvent} to the {@link ContentViewCore}.
 * @param e The {@link MotionEvent} to be propagated.
 */
protected void propagateEventToContentViewCore(MotionEvent e) {
    MotionEvent event = e;
    int action = event.getActionMasked();
    boolean isSyntheticEvent = false;
    if (mGestureOrientation == GestureOrientation.HORIZONTAL && !mPanel.isMaximized()) {
        // Ignores multitouch events to prevent the Content View from scrolling.
        if (action == MotionEvent.ACTION_POINTER_UP
                || action == MotionEvent.ACTION_POINTER_DOWN) {
            return;
        }

        // NOTE(pedrosimonetti): Lock horizontal motion, ignoring all vertical changes,
        // when the Panel is not maximized. This is to prevent the Content View
        // from scrolling when side swiping on the expanded Panel. Also, note that the
        // method {@link OverlayPanelEventFilter#lockEventHorizontallty} will always
        // return an event with a single pointer, which is necessary to prevent
        // the app from crashing when the motion involves multiple pointers.
        // See: crbug.com/486901
        event = MotionEvent.obtain(
                e.getDownTime(),
                e.getEventTime(),
                // NOTE(pedrosimonetti): Use getActionMasked() to make sure we're not
                // send any pointer information to the event, given that getAction()
                // may have the pointer Id associated to it.
                e.getActionMasked(),
                e.getX(),
                mInitialEventY,
                e.getMetaState());

        isSyntheticEvent = true;
    }

    final float contentViewOffsetXPx = mPanel.getContentX() / mPxToDp;
    final float contentViewOffsetYPx = mPanel.getContentY() / mPxToDp;

    // Adjust the offset to be relative to the Content View.
    event.offsetLocation(-contentViewOffsetXPx, -contentViewOffsetYPx);

    // Get the container view to propagate the event to.
    ContentViewCore cvc = mPanel.getContentViewCore();
    ViewGroup containerView = cvc == null ? null : cvc.getContainerView();

    boolean wasEventCanceled = false;
    if (mWasActionDownEventSynthetic && action == MotionEvent.ACTION_UP) {
        float deltaX = event.getX() - mSyntheticActionDownX;
        float deltaY = event.getY() - mSyntheticActionDownY;
        // NOTE(pedrosimonetti): If the ACTION_DOWN event was synthetic and the distance
        // between it and the ACTION_UP event was short, then we should synthesize an
        // ACTION_CANCEL event to prevent a Tap gesture from being triggered on the
        // Content View. See crbug.com/408654
        if (!isDistanceGreaterThanTouchSlop(deltaX, deltaY)) {
            event.setAction(MotionEvent.ACTION_CANCEL);
            if (containerView != null) containerView.dispatchTouchEvent(event);
            wasEventCanceled = true;
        }
    } else if (action == MotionEvent.ACTION_DOWN) {
        mPanel.onTouchSearchContentViewAck();
    }

    if (!wasEventCanceled && containerView != null) containerView.dispatchTouchEvent(event);

    // Synthetic events should be recycled.
    if (isSyntheticEvent) event.recycle();
}