Example usage for android.view MotionEvent offsetLocation

List of usage examples for android.view MotionEvent offsetLocation

Introduction

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

Prototype

public final void offsetLocation(float deltaX, float deltaY) 

Source Link

Document

Adjust this event's location.

Usage

From source file:com.just.agentweb.NestedScrollAgentWebView.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;//  w  ww  .j  a v  a 2 s . c  om
    }

    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];
        }

        mLastMotionY = y - mScrollOffset[1];

        int oldY = getScrollY();
        int newScrollY = Math.max(0, oldY + deltaY);
        int dyConsumed = newScrollY - oldY;
        int dyUnconsumed = deltaY - dyConsumed;

        if (dispatchNestedScroll(0, dyConsumed, 0, dyUnconsumed, mScrollOffset)) {
            mLastMotionY -= mScrollOffset[1];
            trackedEvent.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }

        result = super.onTouchEvent(trackedEvent);
        trackedEvent.recycle();
        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:com.kanhan.widget.PagerContainer.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    //We capture any touches not already handled by the ViewPager
    // to implement scrolling from a touch outside the pager bounds.
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mInitialTouch.x = (int) ev.getX();
        mInitialTouch.y = (int) ev.getY();
    default:/* w w w. j  a v  a 2 s .  c o m*/
        ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
        break;
    }
    return mPager.dispatchTouchEvent(ev);
}

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 w  w  w. j  a  v a 2 s.co  m
    }

    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:com.douban.rexxar.view.NestedWebView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnableNestedScroll) {
        return super.onTouchEvent(ev);
    }//from w  w  w .ja  va  2  s  .c  o m
    boolean returnValue = false;

    MotionEvent event = MotionEvent.obtain(ev);
    final int action = MotionEventCompat.getActionMasked(event);
    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsetY = 0;
    }
    int eventX = (int) event.getX();
    int eventY = (int) event.getY();
    event.offsetLocation(0, -mNestedOffsetY);
    switch (action) {
    case MotionEvent.ACTION_MOVE:
        if (mNestedScrollEstablish) {
            mVelocityTracker.addMovement(ev);
            int deltaX = mLastX - eventX;
            int deltaY = mLastY - eventY;
            // ???
            if (mOptimizeHorizontalScroll) {
                // ??
                if (!mScrollHorizontalEstablish && !mScrollVerticalEstablish) {
                    if (Math.abs(deltaX) > Math.abs(deltaY) * 1.5 && Math.abs(deltaX) > mTouchSlop) {
                        mScrollHorizontalEstablish = true;
                    } else if (Math.abs(deltaY) > Math.abs(deltaX) && Math.abs(deltaY) > mTouchSlop) {
                        mScrollVerticalEstablish = true;
                        mFrozenX = eventX;
                    }
                }
            }
            mLastX = eventX;
            if (mScrollHorizontalEstablish) {
                event.offsetLocation(0, deltaY);
                // ?
                returnValue = super.onTouchEvent(event);
            } else {
                // ?
                if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mOffsetInWindow)) {
                    deltaY -= mScrollConsumed[1];
                    mLastY = eventY - mOffsetInWindow[1];
                    mNestedOffsetY += mOffsetInWindow[1];
                    event.offsetLocation(0, -mOffsetInWindow[1]);
                } else {
                    mLastY = eventY;
                }

                // parent?consumedelta?webView?
                int oldScrollY = getScrollY();
                if ((deltaY < 0 && getScrollY() > 0) || deltaY > 0) {
                    // ???
                    if (mScrollVerticalEstablish) {
                        event.offsetLocation(mFrozenX - eventX, 0);
                        returnValue = super.onTouchEvent(event);
                    } else {
                        returnValue = super.onTouchEvent(event);
                    }
                    mLastYWebViewConsume = event.getY();
                } else {
                    // FIXME ??
                    if (mScrollVerticalEstablish) {
                        event.offsetLocation(mFrozenX - eventX, mLastYWebViewConsume - event.getY());
                    } else {
                        event.offsetLocation(0, mLastYWebViewConsume - event.getY());
                    }
                    super.onTouchEvent(event);
                }

                // deltaY
                if (deltaY == getScrollY() - oldScrollY) {
                    // ???
                } else if (deltaY < getScrollY() - oldScrollY) {
                    // 
                    if (getScrollY() <= 5) {
                        int dyConsumed = oldScrollY - getScrollY();
                        int dyUnconsumed = deltaY - (getScrollY() - oldScrollY);
                        if (dispatchNestedScroll(0, dyConsumed, 0, dyUnconsumed, mOffsetInWindow)) {
                            mNestedOffsetY += mOffsetInWindow[1];
                            mLastY -= mOffsetInWindow[1];
                            event.offsetLocation(0, mOffsetInWindow[1]);
                        }
                    }
                    returnValue = true;
                } else {
                    // ???
                }
            }
        } else {
            returnValue = super.onTouchEvent(event);
        }
        break;
    case MotionEvent.ACTION_DOWN:
        mLastYWebViewConsume = event.getY();
        returnValue = super.onTouchEvent(event);
        mLastX = eventX;
        mLastY = eventY;
        // start NestedScroll
        mNestedScrollEstablish = startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        mScrollHorizontalEstablish = false;
        mScrollVerticalEstablish = false;
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mNestedScrollEstablish) {
            returnValue = super.onTouchEvent(event);
            // end NestedScroll
            stopNestedScroll();
        } else {
            returnValue = super.onTouchEvent(event);
        }
        mScrollHorizontalEstablish = false;
        mScrollVerticalEstablish = false;
        mFrozenX = 0;
        recycleVelocityTracker();
        break;
    case MotionEvent.ACTION_UP:
        if (mNestedScrollEstablish) {
            if (mScrollHorizontalEstablish) {
                // ?
                event.offsetLocation(0, mLastY - eventY);
            }
            returnValue = super.onTouchEvent(event);
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) velocityTracker.getYVelocity();
            if ((Math.abs(initialVelocity) > mMinimumVelocity) && getScrollY() == 0) {
                flingWithNestedDispatch(-initialVelocity);
            } else {// end NestedScroll
                stopNestedScroll();
            }
        } else {
            returnValue = super.onTouchEvent(event);
        }
        mScrollHorizontalEstablish = false;
        mScrollVerticalEstablish = false;
        mFrozenX = 0;
        recycleVelocityTracker();
        break;
    }
    return returnValue;
}

From source file:com.conduit.plastic.widget.NestedWebView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (this.position == ScrollStateChangedListener.ScrollState.MIDDLE) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            this.mIsBeingDragged = false;
            this.mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            this.startNestedScroll(2);
            break;
        }/* w  ww  .ja v a  2s .  co m*/
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL: {
            this.endTouch();
            break;
        }
        }
        super.onTouchEvent(ev);
        return true;
    }
    final int actionMasked = MotionEventCompat.getActionMasked(ev);
    initVelocityTrackerIfNotExists();
    MotionEvent vtev = MotionEvent.obtain(ev);
    final int index = MotionEventCompat.getActionIndex(ev);
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);
    this.consumedY = 0;
    this.direction = 0;
    boolean onTouchEvent = false;
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        // Remember where the motion event started
        onTouchEvent = super.onTouchEvent(ev);
        mLastMotionY = (int) (ev.getY() + 0.5f);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        this.preY = vtev.getY();
        this.mIsBeingDragged = false;
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        onTouchEvent = super.onTouchEvent(ev);
        mLastMotionY = (int) (MotionEventCompat.getY(ev, index) + 0.5f);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }
        if (!mIsBeingDragged && Math.abs(vtev.getY() - this.preY) > mTouchSlop) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
        }
        //                if(!mIsBeingDragged){
        //                    setLongClickEnable(true);
        //                }
        final int y = (int) (MotionEventCompat.getY(ev, activePointerIndex) + 0.5f);
        Log.i(TAG, "mLastMotionY=====" + mLastMotionY);
        Log.i(TAG, "YYYYYYY=====" + y);
        int deltaY = mLastMotionY - y;

        if (deltaY != 0) {
            this.direction = this.directionDetector.getDirection(deltaY, true, this.scrollStateChangedListener);
        }
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            vtev.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }
        if (mIsBeingDragged) {
            //                    setJavaScriptEnable(true);
            // Scroll to follow the motion event
            mLastMotionY = y - mScrollOffset[1];
            Log.i(TAG, "deltaY===" + deltaY);
            Log.i(TAG, "this.consumedY===" + this.consumedY);
            final int unconsumedY = deltaY - this.consumedY;

            Log.i(TAG, " child consumed = " + this.mScrollConsumed[1] + " un_consumed = " + unconsumedY
                    + " position = " + this.position + " direction = " + this.direction);
            onTouchEvent = super.onTouchEvent(ev);
            if (this.position == ScrollStateChangedListener.ScrollState.MIDDLE) {
                return true;
            }
            switch (this.direction) {
            case 1: {
                if ((this.position != ScrollStateChangedListener.ScrollState.BOTTOM)
                        && (this.contentHeight != this.webviewHeight)) {
                    scrollBy(0, unconsumedY);
                    break;
                }
                Log.i(TAG, "1111111consumedY===" + consumedY + "  unconsumedY==" + unconsumedY);
                if (dispatchNestedScroll(0, this.consumedY, 0, unconsumedY, this.mScrollOffset)) {
                    vtev.offsetLocation(0.0F, this.mScrollOffset[1]);
                    this.mNestedYOffset += this.mScrollOffset[1];
                    this.mLastMotionY -= this.mScrollOffset[1];
                }
            }
                break;
            case 2:
                if ((this.position == ScrollStateChangedListener.ScrollState.TOP)
                        || (this.contentHeight == this.webviewHeight)) {
                    Log.i(TAG, "2222222consumedY===" + consumedY + "  unconsumedY==" + unconsumedY);
                    if (dispatchNestedScroll(0, this.consumedY, 0, unconsumedY, this.mScrollOffset)) {
                        vtev.offsetLocation(0.0F, this.mScrollOffset[1]);
                        this.mNestedYOffset += this.mScrollOffset[1];
                        this.mLastMotionY -= this.mScrollOffset[1];
                    }
                } else {
                    scrollBy(0, unconsumedY);
                }
                break;
            default:
                break;
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        onTouchEvent = super.onTouchEvent(ev);
        break;
    case MotionEvent.ACTION_UP:
        onTouchEvent = super.onTouchEvent(ev);
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId);
            if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                flingWithNestedDispatch(-initialVelocity);
            }
        }
        mActivePointerId = INVALID_POINTER;
        endTouch();
        break;

    case MotionEventCompat.ACTION_POINTER_UP:
        onTouchEvent = super.onTouchEvent(ev);
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) (MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId)) + 0.5F);
        break;
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return onTouchEvent;
}

From source file:com.wenhui.syncedListView.lib.SyncedListLayout.java

private void dispatchTouchToList(final MotionEvent e) {
    if (mDownEvent == null) {
        return;// w  w w  . ja  v  a 2 s  .  c  o m
    }

    int leftListWidth = mListViewLeft.getWidth();

    if (mDownEvent.getX() <= mListViewLeft.getWidth()) {
        mListViewLeft.dispatchTouchEvent(mDownEvent);
        mListViewLeft.dispatchTouchEvent(e);
    } else {
        // For some reason, this will only recognize x of left list
        mDownEvent.offsetLocation(-leftListWidth, 0f);
        e.offsetLocation(-leftListWidth, 0f);
        mListViewRight.dispatchTouchEvent(mDownEvent);
        mListViewRight.dispatchTouchEvent(e);
    }

    mDownEvent.recycle();
    mDownEvent = null;
}

From source file:org.faudroids.boredrudolf.ui.CustomSwipeRefreshLayout.java

/**
 * @return Whether it is possible for the child view of this layout to
 * scroll up. Override this if the child view is a custom view.
 *///w ww .j av  a2  s .co m
private boolean canViewScrollUp(View view, MotionEvent event) {
    boolean ret;

    event.offsetLocation(view.getScrollX() - view.getLeft(), view.getScrollY() - view.getTop());
    if (mScrollUpHandler != null) {
        boolean canViewScrollUp = mScrollUpHandler.canScrollUp(view);
        if (canViewScrollUp)
            return true;
    }

    if (android.os.Build.VERSION.SDK_INT < 14) {
        if (view instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) view;
            ret = absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0
                    || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
        } else {
            ret = view.getScrollY() > 0 || canChildrenScrollUp(view, event);
        }
    } else {
        ret = ViewCompat.canScrollVertically(view, -1) || canChildrenScrollUp(view, event);
    }
    if (DEBUG)
        Log.d(TAG, "canViewScrollUp " + view.getClass().getName() + " " + ret);
    return ret;
}

From source file:org.faudroids.boredrudolf.ui.CustomSwipeRefreshLayout.java

/**
 * @param direction Negative to check scrolling left, positive to check scrolling right.
 * @return Whether it is possible for the child view of this layout to
 * scroll left or right. Override this if the child view is a custom view.
 *//*from ww  w  . j  av a 2s  .com*/
private boolean canViewScrollHorizontally(View view, MotionEvent event, int direction) {
    boolean ret;
    event.offsetLocation(view.getScrollX() - view.getLeft(), view.getScrollY() - view.getTop());
    if (mScrollLeftOrRightHandler != null) {
        boolean canViewScrollLeftOrRight = mScrollLeftOrRightHandler.canScrollLeftOrRight(view, direction);
        if (canViewScrollLeftOrRight)
            return true;
    }
    if (android.os.Build.VERSION.SDK_INT < 14) {
        if (view instanceof ViewPager) {
            ret = ((ViewPager) view).canScrollHorizontally(direction);
        } else {
            ret = view.getScrollX() * direction > 0;
        }
    } else {
        ret = ViewCompat.canScrollHorizontally(view, direction);
    }

    ret = ret || canChildrenScrollHorizontally(view, event, direction);
    if (DEBUG)
        Log.d(TAG, "canViewScrollHorizontally " + view.getClass().getName() + " " + ret);
    return ret;
}

From source file:com.taobao.weex.ui.view.WXScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!scrollable) {
        return true; // when scrollable is set to false, then eat the touch event
    }// w w  w  .j a  va2s .c om
    if (mRedirectTouchToStickyView) {

        if (mScrollRect == null) {
            mScrollRect = new Rect();
            getGlobalVisibleRect(mScrollRect);
        }
        mCurrentStickyView.getLocationOnScreen(stickyViewP);
        ev.offsetLocation(0, -(stickyViewP[1] - mScrollRect.top));
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mHasNotDoneActionDown = false;
    }

    if (mHasNotDoneActionDown) {
        MotionEvent down = MotionEvent.obtain(ev);
        down.setAction(MotionEvent.ACTION_DOWN);
        mHasNotDoneActionDown = false;
        down.recycle();
    }

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        ox = ev.getX();
        oy = ev.getY();
        // Dispatch touch event to parent view
        startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL);
    }

    if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        mHasNotDoneActionDown = true;
        // stop nested scrolling dispatch
        stopNestedScroll();
    }

    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        float clampedX = ev.getX();
        float clampedY = ev.getY();
        int dx = (int) (ox - clampedX);
        int dy = (int) (oy - clampedY);

        if (dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)) {
            // sub dx/dy was consumed by parent view!!!
            ev.setLocation(clampedX + consumed[0], clampedY + consumed[1]);
        }
        ox = ev.getX();
        oy = ev.getY();
    }

    boolean result = super.onTouchEvent(ev);
    if (wxGesture != null) {
        result |= wxGesture.onTouch(this, ev);
    }
    return result;
}

From source file:com.yanzhenjie.recyclerview.swipe.widget.StickyNestedScrollView.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        redirectTouchesToStickyView = true;
    }//from   ww  w  . j  av a 2  s  . c  o m
    if (redirectTouchesToStickyView) {
        redirectTouchesToStickyView = currentlyStickingView != null;
        if (redirectTouchesToStickyView) {
            redirectTouchesToStickyView = ev.getY() <= (currentlyStickingView.getHeight() + stickyViewTopOffset)
                    && ev.getX() >= getLeftForViewRelativeOnlyChild(currentlyStickingView)
                    && ev.getX() <= getRightForViewRelativeOnlyChild(currentlyStickingView);
        }
    } else if (currentlyStickingView == null) {
        redirectTouchesToStickyView = false;
    }
    if (redirectTouchesToStickyView) {
        ev.offsetLocation(0, -1 * ((getScrollY() + stickyViewTopOffset)
                - getTopForViewRelativeOnlyChild(currentlyStickingView)));
    }
    return super.dispatchTouchEvent(ev);
}