Example usage for android.view MotionEvent getRawX

List of usage examples for android.view MotionEvent getRawX

Introduction

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

Prototype

public final float getRawX() 

Source Link

Document

Returns the original raw X coordinate of this event.

Usage

From source file:com.lowworker.android.views.custom_views.photo.PhotoViewPager.java

/**
 * {@inheritDoc}/*from   w ww .j  av a2 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.grottworkshop.gwsswipelayout.SwipeLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabledInAdapterView() || !isEnabled())
        return true;

    if (!isSwipeEnabled())
        return super.onTouchEvent(event);

    int action = event.getActionMasked();
    ViewParent parent = getParent();/*from  ww w  .j a  va  2 s. com*/

    gestureDetector.onTouchEvent(event);
    Status status = getOpenStatus();
    ViewGroup touching = null;
    if (status == Status.Close) {
        touching = getSurfaceView();
    } else if (status == Status.Open) {
        touching = getBottomView();
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mDragHelper.processTouchEvent(event);
        parent.requestDisallowInterceptTouchEvent(true);

        sX = event.getRawX();
        sY = event.getRawY();

        if (touching != null)
            touching.setPressed(true);

        return true;
    case MotionEvent.ACTION_MOVE: {
        if (sX == -1 || sY == -1) {
            // Trick:
            // When in nested mode, we need to send a constructed ACTION_DOWN MotionEvent to mDragHelper, to help
            // it initialize itself.
            event.setAction(MotionEvent.ACTION_DOWN);
            mDragHelper.processTouchEvent(event);
            parent.requestDisallowInterceptTouchEvent(true);
            sX = event.getRawX();
            sY = event.getRawY();
            return true;
        }

        float distanceX = event.getRawX() - sX;
        float distanceY = event.getRawY() - sY;
        float angle = Math.abs(distanceY / distanceX);
        angle = (float) Math.toDegrees(Math.atan(angle));

        boolean doNothing = false;
        if (mDragEdge == DragEdge.Right) {
            boolean suitable = (status == Status.Open && distanceX > 0)
                    || (status == Status.Close && distanceX < 0);
            suitable = suitable || (status == Status.Middle);

            if (angle > 30 || !suitable) {
                doNothing = true;
            }
        }

        if (mDragEdge == DragEdge.Left) {
            boolean suitable = (status == Status.Open && distanceX < 0)
                    || (status == Status.Close && distanceX > 0);
            suitable = suitable || status == Status.Middle;

            if (angle > 30 || !suitable) {
                doNothing = true;
            }
        }

        if (mDragEdge == DragEdge.Top) {
            boolean suitable = (status == Status.Open && distanceY < 0)
                    || (status == Status.Close && distanceY > 0);
            suitable = suitable || status == Status.Middle;

            if (angle < 60 || !suitable) {
                doNothing = true;
            }
        }

        if (mDragEdge == DragEdge.Bottom) {
            boolean suitable = (status == Status.Open && distanceY > 0)
                    || (status == Status.Close && distanceY < 0);
            suitable = suitable || status == Status.Middle;

            if (angle < 60 || !suitable) {
                doNothing = true;
            }
        }

        if (doNothing) {
            parent.requestDisallowInterceptTouchEvent(false);
            return false;
        } else {
            if (touching != null) {
                touching.setPressed(false);
            }
            parent.requestDisallowInterceptTouchEvent(true);
            mDragHelper.processTouchEvent(event);
        }
        break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        sX = -1;
        sY = -1;
        if (touching != null) {
            touching.setPressed(false);
        }
    }
    default:
        parent.requestDisallowInterceptTouchEvent(true);
        mDragHelper.processTouchEvent(event);
    }

    return true;
}

From source file:robert.com.basiclibrary.base.BaseActivity.java

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

    //      /*??OnFinishListener?
    //       * onBottomDragListener.onDragBottom(false);onFinishListener.finish();**/
    //      if (onFinishListener != null) {
    ////w ww . j a v a  2 s.c  om
    //         float maxDragHeight = getResources().getDimension(R.dimen.page_drag_max_height);
    //         float distanceY = e2.getRawY() - e1.getRawY();
    //         if (distanceY < maxDragHeight && distanceY > - maxDragHeight) {
    //
    //            float minDragWidth = getResources().getDimension(R.dimen.page_drag_min_width);
    //            float distanceX = e2.getRawX() - e1.getRawX();
    //            if (distanceX > minDragWidth) {
    //               onFinishListener.finish();
    //               return true;
    //            }
    //         }
    //      }

    //???
    if (onBottomDragListener != null && e1.getRawY() > ScreenUtil.getScreenSize(this)[1]
            - ((int) getResources().getDimension(R.dimen.bottom_drag_height))) {

        float maxDragHeight = getResources().getDimension(R.dimen.bottom_drag_max_height);
        float distanceY = e2.getRawY() - e1.getRawY();
        if (distanceY < maxDragHeight && distanceY > -maxDragHeight) {

            float minDragWidth = getResources().getDimension(R.dimen.bottom_drag_min_width);
            float distanceX = e2.getRawX() - e1.getRawX();
            if (distanceX > minDragWidth) {
                onBottomDragListener.onDragBottom(false);
                return true;
            } else if (distanceX < -minDragWidth) {
                onBottomDragListener.onDragBottom(true);
                return true;
            }
        }
    }

    return false;
}

From source file:com.popdeem.sdk.uikit.activity.PDUIClaimActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    View v = getCurrentFocus();//from   w  ww.  ja va2s  .c om

    if (v != null && (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE)
            && v instanceof EditText && !v.getClass().getName().startsWith("android.webkit.")) {
        int scrcoords[] = new int[2];
        v.getLocationOnScreen(scrcoords);
        float x = ev.getRawX() + v.getLeft() - scrcoords[0];
        float y = ev.getRawY() + v.getTop() - scrcoords[1];

        if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
            hideKeyboard(this);
    }
    return super.dispatchTouchEvent(ev);
}

From source file:il.co.globes.android.swipeListView.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)//  w  w  w.j  a  va  2  s  .c o  m
 */
// @Override
// public boolean onTouch(View view, MotionEvent motionEvent)
// {
// if (!isSwipeEnabled())
// {
// return false;
// }
//
// if (viewWidth < 2)
// {
// viewWidth = swipeListView.getWidth();
// }
//
// switch (MotionEventCompat.getActionMasked(motionEvent))
// {
// case MotionEvent.ACTION_DOWN :
// {
// if (paused && downPosition != ListView.INVALID_POSITION)
// {
// return false;
// }
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;
//
// int childCount = swipeListView.getChildCount();
// int[] listViewCoords = new int[2];
// swipeListView.getLocationOnScreen(listViewCoords);
// int x = (int) motionEvent.getRawX() - listViewCoords[0];
// int y = (int) motionEvent.getRawY() - listViewCoords[1];
// View child;
// for (int i = 0; i < childCount; i++)
// {
// child = swipeListView.getChildAt(i);
// child.getHitRect(rect);
//
// int childPosition = swipeListView.getPositionForView(child);
//
// // dont allow swiping if this is on the header or footer or
// // IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
// boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
// && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;
//
// if (allowSwipe && rect.contains(x, y))
// {
// setParentView(child);
// setFrontView(child.findViewById(swipeFrontView));
//
// downX = motionEvent.getRawX();
// downPosition = childPosition;
//
// frontView.setClickable(!opened.get(downPosition));
// frontView.setLongClickable(!opened.get(downPosition));
//
// velocityTracker = VelocityTracker.obtain();
// velocityTracker.addMovement(motionEvent);
// if (swipeBackView > 0)
// {
// setBackView(child.findViewById(swipeBackView));
// }
// break;
// }
// }
// view.onTouchEvent(motionEvent);
// return true;
// }
//
// case MotionEvent.ACTION_UP :
// {
// if (velocityTracker == null || !swiping || downPosition ==
// ListView.INVALID_POSITION)
// {
// break;
// }
//
// float deltaX = motionEvent.getRawX() - downX;
// velocityTracker.addMovement(motionEvent);
// velocityTracker.computeCurrentVelocity(1000);
// float velocityX = Math.abs(velocityTracker.getXVelocity());
// if (!opened.get(downPosition))
// {
// if (swipeMode == SwipeListView.SWIPE_MODE_LEFT &&
// velocityTracker.getXVelocity() > 0)
// {
// velocityX = 0;
// }
// if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT &&
// velocityTracker.getXVelocity() < 0)
// {
// velocityX = 0;
// }
// }
// float velocityY = Math.abs(velocityTracker.getYVelocity());
// boolean swap = false;
// boolean swapRight = false;
// if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity &&
// velocityY * 2 < velocityX)
// {
// swapRight = velocityTracker.getXVelocity() > 0;
// Log.d("SwipeListView", "swapRight: " + swapRight + " - swipingRight: " +
// swipingRight);
// if (swapRight != swipingRight && swipeActionLeft != swipeActionRight)
// {
// swap = false;
// }
// else if (opened.get(downPosition) && openedRight.get(downPosition) &&
// swapRight)
// {
// swap = false;
// }
// else if (opened.get(downPosition) && !openedRight.get(downPosition) &&
// !swapRight)
// {
// swap = false;
// }
// else
// {
// swap = true;
// }
// }
// else if (Math.abs(deltaX) > viewWidth / 2)
// {
// swap = true;
// swapRight = deltaX > 0;
// }
// generateAnimate(frontView, swap, swapRight, downPosition);
// if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE)
// {
// swapChoiceState(downPosition);
// }
//
// velocityTracker.recycle();
// velocityTracker = null;
// downX = 0;
// // change clickable front view
// // if (swap) {
// // frontView.setClickable(opened.get(downPosition));
// // frontView.setLongClickable(opened.get(downPosition));
// // }
// swiping = false;
// break;
// }
//
// case MotionEvent.ACTION_MOVE :
// {
// if (velocityTracker == null || paused || downPosition ==
// ListView.INVALID_POSITION)
// {
// break;
// }
//
// velocityTracker.addMovement(motionEvent);
// velocityTracker.computeCurrentVelocity(1000);
// float velocityX = Math.abs(velocityTracker.getXVelocity());
// float velocityY = Math.abs(velocityTracker.getYVelocity());
//
// float deltaX = motionEvent.getRawX() - downX;
// float deltaMode = Math.abs(deltaX);
//
// int swipeMode = this.swipeMode;
// int changeSwipeMode = swipeListView.changeSwipeMode(downPosition);
// if (changeSwipeMode >= 0)
// {
// swipeMode = changeSwipeMode;
// }
//
// if (swipeMode == SwipeListView.SWIPE_MODE_NONE)
// {
// deltaMode = 0;
// }
// else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH)
// {
// if (opened.get(downPosition))
// {
// if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0)
// {
// deltaMode = 0;
// }
// else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0)
// {
// deltaMode = 0;
// }
// }
// else
// {
// if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0)
// {
// deltaMode = 0;
// }
// else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0)
// {
// deltaMode = 0;
// }
// }
// }
// if (deltaMode > slop && swipeCurrentAction ==
// SwipeListView.SWIPE_ACTION_NONE && velocityY < velocityX)
// {
// swiping = true;
// swipingRight = (deltaX > 0);
// Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " +
// swipingRight);
// if (opened.get(downPosition))
// {
// swipeListView.onStartClose(downPosition, swipingRight);
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
// }
// else
// {
// if (swipingRight && swipeActionRight ==
// SwipeListView.SWIPE_ACTION_DISMISS)
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
// }
// else if (!swipingRight && swipeActionLeft ==
// SwipeListView.SWIPE_ACTION_DISMISS)
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
// }
// else if (swipingRight && swipeActionRight ==
// SwipeListView.SWIPE_ACTION_CHOICE)
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
// }
// else if (!swipingRight && swipeActionLeft ==
// SwipeListView.SWIPE_ACTION_CHOICE)
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
// }
// else
// {
// swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
// }
// swipeListView.onStartOpen(downPosition, swipeCurrentAction,
// swipingRight);
// }
// swipeListView.requestDisallowInterceptTouchEvent(true);
// MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
// cancelEvent.setAction(MotionEvent.ACTION_CANCEL
// | (MotionEventCompat.getActionIndex(motionEvent) <<
// MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
// swipeListView.onTouchEvent(cancelEvent);
// if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE)
// {
// backView.setVisibility(View.GONE);
// }
// }
//
// if (swiping && downPosition != ListView.INVALID_POSITION)
// {
// if (opened.get(downPosition))
// {
// deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset :
// -viewWidth + leftOffset;
// }
// move(deltaX);
// return true;
// }
// break;
// }
// }
// return false;
// }
//

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        return false;
    }

    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
    case MotionEvent.ACTION_DOWN: {
        if (paused && downPosition != ListView.INVALID_POSITION) {
            return false;
        }
        swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;

        int childCount = swipeListView.getChildCount();
        int[] listViewCoords = new int[2];
        swipeListView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = swipeListView.getChildAt(i);
            child.getHitRect(rect);

            int childPosition = swipeListView.getPositionForView(child);

            // dont allow swiping if this is on the header or footer or
            // IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
            boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
                    && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;

            if (allowSwipe && rect.contains(x, y)) {
                setParentView(child);
                setFrontView(child.findViewById(swipeFrontView));

                downX = motionEvent.getRawX();
                downPosition = childPosition;

                frontView.setClickable(!opened.get(downPosition));
                frontView.setLongClickable(!opened.get(downPosition));

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        float deltaX = motionEvent.getRawX() - downX;
        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        if (!opened.get(downPosition)) {
            if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) {
                velocityX = 0;
            }
            if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) {
                velocityX = 0;
            }
        }
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        boolean swap = false;
        boolean swapRight = false;
        if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY * 2 < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            Log.d("SwipeListView", "swapRight: " + swapRight + " - swipingRight: " + swipingRight);
            if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) {
                swap = false;
            } else if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) {
                swap = false;
            } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) {
                swap = false;
            } else {
                swap = true;
            }
        } else if (Math.abs(deltaX) > viewWidth / 2) {
            swap = true;
            swapRight = deltaX > 0;
        }
        generateAnimate(frontView, swap, swapRight, downPosition);
        if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
            swapChoiceState(downPosition);
        }

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        // change clickable front view
        // if (swap) {
        // frontView.setClickable(opened.get(downPosition));
        // frontView.setLongClickable(opened.get(downPosition));
        // }
        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());

        float deltaX = motionEvent.getRawX() - downX;
        float deltaMode = Math.abs(deltaX);

        int swipeMode = this.swipeMode;
        int changeSwipeMode = swipeListView.changeSwipeMode(downPosition);
        if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
        }

        if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
        } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (opened.get(downPosition)) {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                    deltaMode = 0;
                }
            } else {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                    deltaMode = 0;
                }
            }
        }
        if (deltaMode > slop && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
                && velocityY < velocityX) {
            swiping = true;
            swipingRight = (deltaX > 0);
            Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            if (opened.get(downPosition)) {
                swipeListView.onStartClose(downPosition, swipingRight);
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
                if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
                swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight);
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat
                    .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
                backView.setVisibility(View.GONE);
            }
        }

        if (swiping && downPosition != ListView.INVALID_POSITION) {
            if (opened.get(downPosition)) {
                deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset;
            }
            move(deltaX);
            return true;
        }
        break;
    }
    }

    closeOtherOpenedItems();
    view.onTouchEvent(motionEvent);
    return true;

    // return false;
}

From source file:com.lybeat.lilyplayer.widget.media.IjkVideoView.java

private void initGestureScanner(final Context context) {
    slop = ViewConfiguration.get(context).getScaledTouchSlop();
    screenWidth = ScreenUtil.getScreenWidth(context);
    gestureDetector = new GestureDetectorCompat(context, new LilyGestureListener());
    setOnTouchListener(new OnTouchListener() {
        @Override/*from w w  w . j  a  v  a  2 s  .c  o m*/
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_UP:
                if (adjustProgress) {
                    adjustProgress = false;
                    int newPosition = (int) (getCurrentPosition() + scrollX / screenWidth * 1000 * 90);
                    if (newPosition < 0) {
                        seekTo(0);
                    } else {
                        seekTo(newPosition);
                    }
                    start();
                    mAdjustProgressView.setVisibility(GONE);
                    if (mMediaController.isShowing()) {
                        showAllBoard();
                    }
                }
                scrollX = 0;
                scrollY = 0;
                level = 0;
                adjustVolume = false;
                adjustBrightness = false;
                break;
            case MotionEvent.ACTION_DOWN:
                lastX = motionEvent.getRawX();
                lastY = motionEvent.getRawY();
                if (motionEvent.getRawX() > screenWidth / 2 + 150) {
                    adjustVolume = true;
                    adjustBrightness = false;
                } else if (motionEvent.getRawX() < screenWidth / 2 - 150) {
                    adjustBrightness = true;
                    adjustVolume = false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (adjustProgress) {
                    break;
                }
                if (Math.abs(motionEvent.getRawY() - lastY) > Math.abs(motionEvent.getRawX() - lastX)) {

                    AudioManager audioManager = (AudioManager) mAppContext
                            .getSystemService(Context.AUDIO_SERVICE);
                    if (motionEvent.getRawY() > lastY) {
                        level += motionEvent.getRawY() - lastY;
                        if (level > slop) {
                            if (adjustVolume) {
                                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                                        AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
                            } else if (adjustBrightness) {
                                float brightness = ScreenUtil.getScreenBrightness((Activity) context);
                                if (brightness <= 0.1f) {
                                    ScreenUtil.setScreenBrightness((Activity) context, 0.0f);
                                } else {
                                    ScreenUtil.setScreenBrightness((Activity) context,
                                            ScreenUtil.getScreenBrightness((Activity) context) - 0.1f);
                                }
                            }
                            level = 0;
                        }
                    } else {
                        level += lastY - motionEvent.getRawY();
                        if (level > slop) {
                            if (adjustVolume) {
                                audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                                        AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
                            } else if (adjustBrightness) {
                                float brightness = ScreenUtil.getScreenBrightness((Activity) context);
                                if (brightness >= 0.9f) {
                                    ScreenUtil.setScreenBrightness((Activity) context, 1.0f);
                                } else {
                                    ScreenUtil.setScreenBrightness((Activity) context,
                                            ScreenUtil.getScreenBrightness((Activity) context) + 0.1f);
                                }
                            }
                            level = 0;
                        }
                    }
                }
                lastX = motionEvent.getRawX();
                lastY = motionEvent.getRawY();
                break;
            }

            return gestureDetector.onTouchEvent(motionEvent);
        }
    });
}

From source file:com.android.contacts.quickcontact.QuickContactActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        TouchPointManager.getInstance().setPoint((int) ev.getRawX(), (int) ev.getRawY());
    }//from  ww  w .  j  av a 2s .  co m
    return super.dispatchTouchEvent(ev);
}

From source file:cn.trinea.android.common.view.CycleViewPager.java

private boolean pointInRect(MotionEvent event, Rect rect) {
    final float x = event.getRawX();
    final float y = event.getRawY();
    Log.d(TAG, "pointInRect x = " + x + ", y = " + y + ", rect = " + rect);
    if (rect != null && mCurItem == mSpecTab) {
        return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
    }/*from  w  w w  .ja v a 2  s .  c  o m*/
    return false;
}

From source file:cz.metaverse.android.bilingualreader.helper.BookPanelOnTouchListener.java

/**
 * The entry method for any touch-related event.
 * @param view The WebView where the swipe took place
 * @param event The MotionEvent of the swipe
 *///from w w w  .ja  va 2 s  .c o  m
@Override
public boolean onTouch(View view, MotionEvent event) {
    // Provide data to the GestureDetector.
    gestureDetector.onTouchEvent(event);

    /*
     * Handle Double Tap Swipe in real time.
     *  This handling isn't in onDoubleTapEvent() because that method has a limit how many times
     *  it gets called after which it isn't called at all until the user lifts the finger in which
     *  case it gets called one last time. That is not workable for on-the-fly resizing of panels.
     */
    if (isDoubleTapSwipe) {
        float absDiffX = Math.abs(doubleTapOriginX - event.getX());
        float absDiffY = Math.abs(doubleTapOriginY - event.getY());

        // If the swipe was over predefined threshold value high and it was higher than wider,
        // or if the swipe was over predefined threshold value wide and it was wider than higher.
        if (doubleTapSwipeEscapedBounds ||
        // Bounds for PORTRAIT orientation.
                (absDiffY > threshold_swipe_up_or_down_change_panel_size_px && absDiffY > absDiffX
                        && doubleTapSwipe_orientation == Configuration.ORIENTATION_PORTRAIT)
                ||
                // Bounds for LANDSCAPE orientation.
                (absDiffX > threshold_swipe_left_right_change_panel_size_px && absDiffX > absDiffY
                        && doubleTapSwipe_orientation != Configuration.ORIENTATION_PORTRAIT)) {

            if (!doubleTapSwipeEscapedBounds) {
                // This is the first time doubleTapSwipe escaped it's bounds
                // - we're officially in the set-panels-size mode.
                doubleTapSwipeEscapedBounds = true;

                // Find out and save the relevant dimensions of our view/display
                Window window = activity.getWindow();

                if (doubleTapSwipe_orientation == Configuration.ORIENTATION_PORTRAIT) {
                    doubleTapSwipe_contentStartsAtHeight = window.findViewById(Window.ID_ANDROID_CONTENT)
                            .getTop();
                    doubleTapSwipe_viewHeight = window.getDecorView().getHeight();
                } else {
                    doubleTapSwipe_contentStartsAtHeight = window.findViewById(Window.ID_ANDROID_CONTENT)
                            .getLeft();
                    doubleTapSwipe_viewHeight = window.getDecorView().getWidth();
                }
            }

            // Compute the panels weight
            float useCoordinate = (doubleTapSwipe_orientation == Configuration.ORIENTATION_PORTRAIT)
                    ? event.getRawY()
                    : event.getRawX();

            newPanelsWeight = (useCoordinate - doubleTapSwipe_contentStartsAtHeight)
                    / (doubleTapSwipe_viewHeight - doubleTapSwipe_contentStartsAtHeight);

            // If the weight is close to 0.5, let it stick to it.
            if (Math.abs(0.5 - newPanelsWeight) < PANEL_RESIZE_SNAP_WEIGHT_AROUND_HALF) {
                newPanelsWeight = 0.5f;
            }

            // Assure that the weight is between the allowed minimum and maximum.
            newPanelsWeight = Func.minMaxRange(Func.PANEL_WEIGHT_MIN, newPanelsWeight, Func.PANEL_WEIGHT_MAX);

            // Change relative panel size on the fly.
            governor.changePanelsWeight(newPanelsWeight);

            //Log.v(LOG, "[" + panelPosition + "] doubleTapSwipe " + newPanelsWeight);
            //+ " = (" + event.getRawY() + " - " + contentViewTop + ") / " + (height - contentViewTop));
        }
    }

    /*
     * Handle ACTION_UP event for scrolling, because onScroll (almost?) never gets
     * called with the last MotionEvent.ACTION_UP.
     * Don't forget to mirror any changes made here in the onScroll() method as well, to be safe.
     */
    if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP) {
        Log.d(LOG, "[" + panelPosition + "] OnTouchListener --> onTouch ACTION_UP - scrollY: "
                + webView.getScrollY());

        // Only if it's not DoubleTapSwipe, that is handled separately.
        if (!isDoubleTapSwipe) {
            // Evaluate if the scroll that has just ended constitutes some gesture.
            handleScrollEnd(event);
        }
    }

    // Modify the event so that it is passed on in a state where the X coordinate hasn't changed at all.
    // This prevents the "over scroll glow effect" on pages that have some (hidden) horizontal scroll.
    //  -- Disabled because it causes the wrong scroll position to be loaded after opening a page
    //     sometimes (but not every time), for some unknown reason.
    //event.setLocation(touchOriginX, event.getY());

    view.performClick(); // Android system mandates we pass the baton to the onClick listener now.

    return view.onTouchEvent(event);
}

From source file:co.vn.e_alarm.customwiget.SlidingLayer.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    if (!mEnabled) {
        return false;
    }//from ww w  . j  av a 2s  .  co m

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        /*
         * Return event touch to children view.
         */
        mIsDragging = false;
        mIsUnableToDrag = false;

        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsDragging) {
            return true;
        } else if (mIsUnableToDrag) {
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE:
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            break;
        }

        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        if (pointerIndex == -1) {
            mActivePointerId = INVALID_POINTER;
            break;
        }

        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float dx = x - mLastX;
        final float xDiff = Math.abs(dx);
        final float y = MotionEventCompat.getY(ev, pointerIndex);
        final float dy = y - mLastY;
        final float yDiff = Math.abs(y - mLastY);

        if (xDiff > mTouchSlop && xDiff > yDiff && allowDragingX(dx, mInitialX)) {
            // mIsDragging = true;
            mLastX = x;
            setDrawingCacheEnabled(true);
        } else if (yDiff > mTouchSlop && yDiff > xDiff && allowDragingY(dy, mInitialY)) {
            // mIsDragging = true;
            mLastY = y;
            setDrawingCacheEnabled(true);
        }
        float matY = (float) Math.abs(corY - ev.getRawY());
        float matX = (float) Math.abs(corX - ev.getRawX());
        if (matY - 100 > 0) {
            mIsDragging = true;
            setDrawingCacheEnabled(true);
            // Dang di chuyen len
        } else if (matX - 100 > 0) {
            mIsDragging = false;
        }
        if (matY == 0) {
            setDrawingCacheEnabled(false);
        }

        /*   }else{
        mIsDragging = false;
        setDrawingCacheEnabled(true);
           }*/
        /* }else if(corX +150 > ev.getRawX()){
        mIsDragging = false;
        setDrawingCacheEnabled(true);
        }else if(corX +150 < ev.getRawX()){
        mIsDragging = false;
        setDrawingCacheEnabled(true);
        }*/
        // double corY = ev.getY();

        break;

    case MotionEvent.ACTION_DOWN:
        corX = ev.getRawX();
        corY = ev.getRawY();
        mActivePointerId = ev.getAction() & (Build.VERSION.SDK_INT >= 8 ? MotionEvent.ACTION_POINTER_INDEX_MASK
                : MotionEventCompat.ACTION_POINTER_INDEX_MASK);
        mLastX = mInitialX = MotionEventCompat.getX(ev, mActivePointerId);
        mLastY = mInitialY = MotionEventCompat.getY(ev, mActivePointerId);
        if (allowSlidingFromHereX(ev, mInitialX)) {
            mIsDragging = false;
            mIsUnableToDrag = false;
            // If nobody else got the focus we use it to close the layer
            return super.onInterceptTouchEvent(ev);
        } else if (allowSlidingFromHereY(ev, mInitialY)) {
            mIsDragging = false;
            mIsUnableToDrag = false;
            // If nobody else got the focus we use it to close the layer
            return super.onInterceptTouchEvent(ev);
        } else {
            mIsUnableToDrag = true;
        }
        break;
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

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

    return mIsDragging;
}