Example usage for android.view MotionEvent ACTION_CANCEL

List of usage examples for android.view MotionEvent ACTION_CANCEL

Introduction

In this page you can find the example usage for android.view MotionEvent ACTION_CANCEL.

Prototype

int ACTION_CANCEL

To view the source code for android.view MotionEvent ACTION_CANCEL.

Click Source Link

Document

Constant for #getActionMasked : The current gesture has been aborted.

Usage

From source file:com.commonsware.cwac.crossport.design.widget.HeaderBehavior.java

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {
    if (mTouchSlop < 0) {
        mTouchSlop = ViewConfiguration.get(parent.getContext()).getScaledTouchSlop();
    }/*from w  ww  .  j  a v  a  2  s.  c o m*/

    final int action = ev.getAction();

    // Shortcut since we're being dragged
    if (action == MotionEvent.ACTION_MOVE && mIsBeingDragged) {
        return true;
    }

    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        mIsBeingDragged = false;
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (canDragView(child) && parent.isPointInChildBounds(child, x, y)) {
            mLastMotionY = y;
            mActivePointerId = ev.getPointerId(0);
            ensureVelocityTracker();
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }
        final int pointerIndex = ev.findPointerIndex(activePointerId);
        if (pointerIndex == -1) {
            break;
        }

        final int y = (int) ev.getY(pointerIndex);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionY = y;
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP: {
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        break;
    }
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(ev);
    }

    return mIsBeingDragged;
}

From source file:com.aohuan.dodo.dispatchevent_about.dummy_demo.view1.NestedScrollWebView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;

    MotionEvent trackedEvent = MotionEvent.obtain(event);

    final int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;//from   www  . j ava2  s  .  com
    }

    int y = (int) event.getY();

    event.offsetLocation(0, mNestedYOffset);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = y;
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        result = super.onTouchEvent(event);
        break;
    case MotionEvent.ACTION_MOVE:
        int deltaY = mLastMotionY - y;

        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            trackedEvent.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }

        int oldY = getScrollY();
        mLastMotionY = y - mScrollOffset[1];
        if (deltaY < 0) {
            int newScrollY = Math.max(0, oldY + deltaY);
            deltaY -= newScrollY - oldY;
            if (dispatchNestedScroll(0, newScrollY - deltaY, 0, deltaY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                trackedEvent.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }
        }

        trackedEvent.recycle();
        result = super.onTouchEvent(trackedEvent);
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        stopNestedScroll();
        result = super.onTouchEvent(event);
        break;
    }
    return result;
}

From source file:cn.goodjobs.common.view.photodraweeview.ScaleDragDetector.java

private void onTouchActivePointer(int action, MotionEvent ev) {
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        break;/*from   w  w w. j  a  va  2 s.c  o  m*/
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        mActivePointerId = INVALID_POINTER_ID;
        break;
    case MotionEvent.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = (pointerIndex == 0) ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
            mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
            mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
        }

        break;
    }

    mActivePointerIndex = MotionEventCompat.findPointerIndex(ev,
            mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
}

From source file:android.support.v7.widget.AbsActionBarView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // ActionBarViews always eat touch events, but should still respect the touch event dispatch
    // contract. If the normal View implementation doesn't want the events, we'll just silently
    // eat the rest of the gesture without reporting the events to the default implementation
    // since that's what it expects.

    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEvent.ACTION_DOWN) {
        mEatingTouch = false;/* w w w .  ja v a 2  s.c o m*/
    }

    if (!mEatingTouch) {
        final boolean handled = super.onTouchEvent(ev);
        if (action == MotionEvent.ACTION_DOWN && !handled) {
            mEatingTouch = true;
        }
    }

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        mEatingTouch = false;
    }

    return true;
}

From source file:ca.hoogit.garagepi.Controls.DoorView.java

private void init(Context context, AttributeSet attrs, int defStyle) {
    inflate(getContext(), R.layout.door_view, this);
    ButterKnife.bind(this);

    final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DoorView, defStyle, 0);
    mOpenedColor = a.getColor(R.styleable.DoorView_openedColor,
            ContextCompat.getColor(context, R.color.colorAccent));
    mClosedColor = a.getColor(R.styleable.DoorView_closedColor,
            ContextCompat.getColor(context, R.color.colorPrimary));
    mDoorName = a.getString(R.styleable.DoorView_doorName);
    a.recycle();/*  ww  w .j av  a  2s.co  m*/

    mContext = context;

    mToggleContainer.setOnClickListener(OnSingleClickListener.wrap(v -> {
        if (mOnToggle != null) {
            mOnToggle.onToggle(mDoorName);
        }
    }));

    mToggleContainer.setOnTouchListener((v, event) -> {
        int origColor = mDoorValue ? mOpenedColor : mClosedColor;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mToggleContainer.setBackgroundColor(ColorUtil.darken(origColor));
        } else if (event.getAction() == MotionEvent.ACTION_UP
                || event.getAction() == MotionEvent.ACTION_CANCEL) {
            mToggleContainer.setBackgroundColor(origColor);
        }
        return false;
    });

    setupViews();
}

From source file:android.support.v7.widget.DropDownListView.java

/**
 * Handles forwarded events./*from w  w  w.  ja  va  2 s.c o m*/
 *
 * @param activePointerId id of the pointer that activated forwarding
 * @return whether the event was handled
 */
public boolean onForwardedEvent(MotionEvent event, int activePointerId) {
    boolean handledEvent = true;
    boolean clearPressedItem = false;

    final int actionMasked = MotionEventCompat.getActionMasked(event);
    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) {
            clickPressedItem(child, position);
        }
        break;
    }

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

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

    return handledEvent;
}

From source file:com.bytestemplar.tonedef.international.ButtonsFragment.java

public void updateButtons(int position) {
    LinearLayout ll_btn_container = (LinearLayout) getView().findViewById(R.id.buttons_container);

    if (ll_btn_container != null) {

        _current_position = position;//w  w  w.  j a  v a2s  .c  o m
        CountryTones current_tones = CountryTonesRepository.getInstance().getCountryAtPosition(position);

        ll_btn_container.removeAllViewsInLayout();

        // Update label
        TextView tv_name = (TextView) getView().findViewById(R.id.tv_countryname);
        tv_name.setText(current_tones.getName());
        tv_name.setTypeface(UICustom.getInstance().getTypeface());
        if (current_tones.getFlagDrawable() > 0) {
            tv_name.setCompoundDrawablesWithIntrinsicBounds(current_tones.getFlagDrawable(), 0, 0, 0);
        }

        // Generate buttons
        HashMap<String, ToneSequence> sequences = current_tones.getSequences();

        for (Object o : sequences.entrySet()) {
            Map.Entry pair = (Map.Entry) o;

            String sequence_name = (String) pair.getKey();
            final ToneSequence sequence = (ToneSequence) pair.getValue();

            Button btn = new Button(ll_btn_container.getContext());
            btn.setText(sequence_name);
            btn.setTypeface(UICustom.getInstance().getTypeface());
            btn.setBackgroundResource(R.drawable.touchpadbutton);
            btn.setTextColor(Color.WHITE);
            btn.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        sequence.start();
                        v.setBackgroundResource(R.drawable.touchpadbutton_selected);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        sequence.stop();
                        v.setBackgroundResource(R.drawable.touchpadbutton);
                        break;
                    }
                    return false;
                }
            });
            btn.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            ll_btn_container.addView(btn);
        }
    }
}

From source file:android.support.v7.widget.AbsActionBarView.java

@Override
public boolean onHoverEvent(MotionEvent ev) {
    // Same deal as onTouchEvent() above. Eat all hover events, but still
    // respect the touch event dispatch contract.

    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEventCompat.ACTION_HOVER_ENTER) {
        mEatingHover = false;//  ww  w.  j a va 2  s .  com
    }

    if (!mEatingHover) {
        final boolean handled = super.onHoverEvent(ev);
        if (action == MotionEventCompat.ACTION_HOVER_ENTER && !handled) {
            mEatingHover = true;
        }
    }

    if (action == MotionEventCompat.ACTION_HOVER_EXIT || action == MotionEvent.ACTION_CANCEL) {
        mEatingHover = false;
    }

    return true;
}

From source file:com.android.ex.photo.PhotoViewPager.java

/**
 * {@inheritDoc}//from  w w  w  . j a va2 s  . c  om
 * <p>
 * We intercept touch event intercepts so we can prevent switching views when the
 * current view is internally scrollable.
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    final InterceptType intercept = (mListener != null) ? mListener.onTouchIntercept(mActivatedX, mActivatedY)
            : InterceptType.NONE;
    final boolean ignoreScrollLeft = (intercept == InterceptType.BOTH || intercept == InterceptType.LEFT);
    final boolean ignoreScrollRight = (intercept == InterceptType.BOTH || intercept == InterceptType.RIGHT);

    // Only check ability to page if we can't scroll in one / both directions
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mActivePointerId = INVALID_POINTER;
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        if (ignoreScrollLeft || ignoreScrollRight) {
            final int activePointerId = mActivePointerId;
            if (activePointerId == INVALID_POINTER) {
                // If we don't have a valid id, the touch down wasn't on content.
                break;
            }

            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);

            if (ignoreScrollLeft && ignoreScrollRight) {
                mLastMotionX = x;
                return false;
            } else if (ignoreScrollLeft && (x > mLastMotionX)) {
                mLastMotionX = x;
                return false;
            } else if (ignoreScrollRight && (x < mLastMotionX)) {
                mLastMotionX = x;
                return false;
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        mLastMotionX = ev.getX();
        // Use the raw x/y as the children can be located anywhere and there isn't a
        // single offset that would be meaningful
        mActivatedX = ev.getRawX();
        mActivatedY = ev.getRawY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            // Our active pointer going up; select a new active pointer
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        break;
    }
    }

    return super.onInterceptTouchEvent(ev);
}

From source file:com.amitupadhyay.aboutexample.ui.widget.BottomSheet.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    hasInteractedWithSheet = true;//from ww  w.j  ava2s .  co m
    if (isNestedScrolling)
        return false; /* prefer nested scrolling to dragging */

    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        sheetDragHelper.cancel();
        return false;
    }
    return isDraggableViewUnder((int) ev.getX(), (int) ev.getY())
            && (sheetDragHelper.shouldInterceptTouchEvent(ev));
}