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.android.launcher3.Utilities.java

/**
 * Emulates View.toGlobalMotionEvent(). This implementation does not handle transformations
 * (scaleX, scaleY, etc)./*from w  w  w .j a  va2s . co  m*/
 */
private static void toGlobalMotionEvent(View view, MotionEvent event) {
    view.getLocationOnScreen(sLoc0);
    event.offsetLocation(sLoc0[0], sLoc0[1]);
}

From source file:com.android.launcher3.Utilities.java

/**
 * Emulates View.toLocalMotionEvent(). This implementation does not handle transformations
 * (scaleX, scaleY, etc).//w  w  w  .j a  v  a2  s. c o  m
 */
private static void toLocalMotionEvent(View view, MotionEvent event) {
    view.getLocationOnScreen(sLoc0);
    event.offsetLocation(-sLoc0[0], -sLoc0[1]);
}

From source file:com.marshalchen.ultimaterecyclerview.UltimateRecyclerView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    URLogs.d("ev---" + ev);
    if (mCallbacks != null) {

        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIntercepted = false;//w w  w.  j  a  v a 2 s . c  o  m
            mDragging = false;
            mCallbacks.onUpOrCancelMotionEvent(mObservableScrollState);
            break;
        case MotionEvent.ACTION_MOVE:
            if (mPrevMoveEvent == null) {
                mPrevMoveEvent = ev;
            }
            float diffY = ev.getY() - mPrevMoveEvent.getY();
            mPrevMoveEvent = MotionEvent.obtainNoHistory(ev);
            if (getCurrentScrollY() - diffY <= 0) {
                // Can't scroll anymore.

                if (mIntercepted) {
                    // Already dispatched ACTION_DOWN event to parents, so stop here.
                    return false;
                }

                // Apps can set the interception target other than the direct parent.
                final ViewGroup parent;
                if (mTouchInterceptionViewGroup == null) {
                    parent = (ViewGroup) getParent();
                } else {
                    parent = mTouchInterceptionViewGroup;
                }

                // Get offset to parents. If the parent is not the direct parent,
                // we should aggregate offsets from all of the parents.
                float offsetX = 0;
                float offsetY = 0;
                for (View v = this; v != null && v != parent; v = (View) v.getParent()) {
                    offsetX += v.getLeft() - v.getScrollX();
                    offsetY += v.getTop() - v.getScrollY();
                }
                final MotionEvent event = MotionEvent.obtainNoHistory(ev);
                event.offsetLocation(offsetX, offsetY);

                if (parent.onInterceptTouchEvent(event)) {
                    mIntercepted = true;

                    // If the parent wants to intercept ACTION_MOVE events,
                    // we pass ACTION_DOWN event to the parent
                    // as if these touch events just have began now.
                    event.setAction(MotionEvent.ACTION_DOWN);

                    // Return this onTouchEvent() first and set ACTION_DOWN event for parent
                    // to the queue, to keep events sequence.
                    post(new Runnable() {
                        @Override
                        public void run() {
                            parent.dispatchTouchEvent(event);
                        }
                    });
                    return false;
                }
                // Even when this can't be scrolled anymore,
                // simply returning false here may cause subView's click,
                // so delegate it to super.
                return super.onTouchEvent(ev);
            }
            break;
        }
    }
    return super.onTouchEvent(ev);
}

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        mRedirectTouchToStickyView = true;
    }/*w  w  w  . j  a va2 s  . c om*/

    if (mRedirectTouchToStickyView) {
        mRedirectTouchToStickyView = mCurrentStickyView != null;

        if (mRedirectTouchToStickyView) {
            mRedirectTouchToStickyView = ev.getY() <= mCurrentStickyView.getHeight()
                    && ev.getX() >= mCurrentStickyView.getLeft() && ev.getX() <= mCurrentStickyView.getRight();
        }
    }

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

From source file:com.creativtrendz.folio.ui.FolioWebViewScroll.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean rs = false;
    final int action = MotionEventCompat.getActionMasked(event);
    if (action == MotionEvent.ACTION_DOWN) {
        mNestedOffsetY = 0;/* w w w.j  a  va 2s  .c o m*/
    }
    int y = (int) event.getY();
    event.offsetLocation(0, mNestedOffsetY);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        rs = super.onTouchEvent(event);
        mLastY = y;
        break;
    case MotionEvent.ACTION_MOVE:
        int dy = mLastY - y;
        int oldY = getScrollY();
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        if (dispatchNestedPreScroll(0, dy, mScrollConsumed, mScrollOffset)) {
            dy -= mScrollConsumed[1];
            event.offsetLocation(0, -mScrollOffset[1]);
            mNestedOffsetY += mScrollOffset[1];
        }
        rs = super.onTouchEvent(event);
        mLastY = y - mScrollOffset[1];
        if (dy < 0) {
            int newScrollY = Math.max(0, oldY + dy);
            dy -= newScrollY - oldY;
            if (dispatchNestedScroll(0, newScrollY - dy, 0, dy, mScrollOffset)) {
                event.offsetLocation(0, mScrollOffset[1]);
                mNestedOffsetY += mScrollOffset[1];
                mLastY -= mScrollOffset[1];
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        rs = super.onTouchEvent(event);
        stopNestedScroll();
        break;
    }
    return rs;
}

From source file:com.medhdj.pagergallery.PagerGalleryContainer.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int marginClick = 10;
    // 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();
        break;/*from   w w  w  .j  av a 2  s . com*/
    case MotionEvent.ACTION_UP:
        // capturing the click on the PagerGalleryContainer to perform
        // selection of an item in the PagerGallery
        if ((int) ev.getX() >= (mInitialTouch.x - marginClick)
                && (int) ev.getX() <= (mInitialTouch.x + marginClick)
                && (int) ev.getY() >= mInitialTouch.y - marginClick
                && (int) ev.getY() <= mInitialTouch.y + marginClick) {
            float pageWidth = mPager.getChildAt(0).getWidth();
            float pageMargin = mPager.getPageMargin();
            int jump = Math.round((ev.getX() - mCenter.x) / (pageWidth + pageMargin));

            jump = mPager.getCurrentItem() + jump;
            if (jump < 0) {
                jump = 0;
            } else if (jump > mPager.getChildCount()) {
                jump = mPager.getChildCount() - 1;
            }
            mPager.setCurrentItem(jump, true);
            return true;
        }
        break;
    default:
        break;
    }
    ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
    return mPager.dispatchTouchEvent(ev);

}

From source file:com.xiaosu.lib.base.widget.drawerLayout.DrawerLayout.java

private boolean anyChildWantMotionEvent(MotionEvent ev, ViewGroup group) {
    MotionEvent event = MotionEvent.obtain(ev);
    event.offsetLocation(-group.getLeft(), -group.getTop());
    //view/*w  w  w  .ja  v  a 2 s .com*/
    int childCount = group.getChildCount();
    final float x = event.getX();
    final float y = event.getY();

    for (int i = 0; i < childCount; i++) {
        View child = group.getChildAt(i);
        if (x >= child.getLeft() && x < child.getRight() && y >= child.getTop() && y < child.getBottom()) {
            if (child instanceof ViewGroup) {
                MotionEvent chileEvent = MotionEvent.obtain(event);
                chileEvent.offsetLocation(-child.getLeft(), -child.getTop());
                if (anyChildWantMotionEvent(chileEvent, (ViewGroup) child)) {
                    chileEvent.recycle();
                    return true;
                }
            } else if (child.onTouchEvent(event)) {
                return true;
            }
        }
    }
    event.recycle();
    return false;
}

From source file:com.xiaosu.lib.base.widget.drawerLayout.DrawerLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    //        Log.i(TAG, "onInterceptTouchEvent: " + ev);
    final int action = ev.getAction();
    boolean interceptForTap = false;
    boolean interceptForNonNestedScrollChild = false;
    boolean interceptForNoChildHandle = false;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        if (mScrimOpacity > 0) {
            final float x = ev.getX();
            final float y = ev.getY();
            final View child = findTopChildUnder(this, (int) x, (int) y);
            if (child != null && child == mContentView) {
                interceptForTap = true;/*w  w w .  ja  v a  2  s  .  c o  m*/
            } else if (child != null && child == mDrawerView) {
                if (child instanceof ViewGroup) {
                    int insetX = (int) (x - child.getLeft());
                    int insetY = (int) (y - child.getTop());
                    View topChild = findTopChildUnder(child, insetX, insetY);

                    if (null == topChild)
                        interceptForNoChildHandle = true;

                    if (topChild instanceof ViewGroup) {
                        if (!ViewCompat.isNestedScrollingEnabled(topChild)) {
                            MotionEvent event = MotionEvent.obtain(ev);
                            event.offsetLocation(-child.getLeft(), -child.getTop());
                            if (!anyChildWantMotionEvent(event, (ViewGroup) topChild))
                                interceptForNoChildHandle = true;
                            event.recycle();
                        }
                    } else if (!ViewCompat.isNestedScrollingEnabled(topChild)) {
                        //mDrawerView?View(?ViewGroup)
                        interceptForNonNestedScrollChild = true;
                    }
                } else if (!ViewCompat.isNestedScrollingEnabled(child)) {
                    interceptForNonNestedScrollChild = true;
                }
            }
        }
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
    }
    }
    return interceptForTap || interceptForNonNestedScrollChild || interceptForNoChildHandle;
}

From source file:androidVNC.VncCanvasActivity.java

boolean trackballMouse(MotionEvent evt) {
    int dx = convertTrackballDelta(evt.getX());
    int dy = convertTrackballDelta(evt.getY());

    evt.offsetLocation(vncCanvas.mouseX + dx - evt.getX(), vncCanvas.mouseY + dy - evt.getY());

    if (vncCanvas.processPointerEvent(evt, trackballButtonDown)) {
        return true;
    }//  w  w  w  .j  ava 2 s . c o  m
    return VncCanvasActivity.super.onTouchEvent(evt);
}

From source file:com.hxqc.mall.core.views.CustomScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();/*from  w  w  w. j av a  2 s  . c  o  m*/

    MotionEvent vtev = MotionEvent.obtain(ev);

    final int actionMasked = MotionEventCompat.getActionMasked(ev);

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }
    vtev.offsetLocation(0, mNestedYOffset);

    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        if (getChildCount() == 0) {
            return false;
        }
        if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionY = (int) ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (activePointerIndex == -1) {
            DebugLog.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }

        final int y = (int) MotionEventCompat.getY(ev, activePointerIndex);
        int deltaY = mLastMotionY - y;
        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            vtev.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }
        if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
            if (deltaY > 0) {
                deltaY -= mTouchSlop;
            } else {
                deltaY += mTouchSlop;
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mLastMotionY = y - mScrollOffset[1];

            final int oldY = getScrollY();
            final int range = getScrollRange();
            final int overscrollMode = ViewCompat.getOverScrollMode(this);
            boolean canOverscroll = overscrollMode == ViewCompat.OVER_SCROLL_ALWAYS
                    || (overscrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && range > 0);

            // Calling overScrollByCompat will call onOverScrolled, which
            // calls onScrollChanged if applicable.
            if (overScrollByCompat(0, deltaY, 0, getScrollY(), 0, range, 0, 0, true)
                    && !hasNestedScrollingParent()) {
                // Break our velocity if we hit a scroll barrier.
                mVelocityTracker.clear();
            }

            final int scrolledDeltaY = getScrollY() - oldY;
            final int unconsumedY = deltaY - scrolledDeltaY;
            if (dispatchNestedScroll(0, scrolledDeltaY, 0, unconsumedY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                vtev.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            } else if (canOverscroll) {
                ensureGlows();
                final int pulledToY = oldY + deltaY;
                if (pulledToY < 0) {
                    mEdgeGlowTop.onPull((float) deltaY / getHeight(),
                            MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowBottom.isFinished()) {
                        mEdgeGlowBottom.onRelease();
                    }
                } else if (pulledToY > range) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight(),
                            1.f - MotionEventCompat.getX(ev, activePointerIndex) / getWidth());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowTop != null && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        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;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionY = (int) MotionEventCompat.getY(ev,
                MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(vtev);
    }
    vtev.recycle();
    return true;
}