Example usage for android.view MotionEvent getY

List of usage examples for android.view MotionEvent getY

Introduction

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

Prototype

public final float getY(int pointerIndex) 

Source Link

Document

Returns the Y coordinate of this event for the given pointer index (use #getPointerId(int) to find the pointer identifier for this index).

Usage

From source file:com.cssweb.android.view.KlineView.java

/**
 * true?,false??/*from w w  w.j  a  va 2s.c  o  m*/
 */
public boolean onTouchEvent(MotionEvent motionEvent) {
    int count = motionEvent.getPointerCount();
    if (count == 2) {//
        isTrackStatus = false;
        if (motionEvent.getAction() == MotionEvent.ACTION_POINTER_1_DOWN
                || motionEvent.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
            float x0 = motionEvent.getX(0);
            float x1 = motionEvent.getX(1);
            float y0 = motionEvent.getY(0);
            float y1 = motionEvent.getY(1);
            distanceY0 = Math.abs(y1 - y0);
            distanceX0 = Math.abs(x1 - x0);
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_POINTER_1_UP
                || motionEvent.getAction() == MotionEvent.ACTION_POINTER_2_UP) {
            float x0 = motionEvent.getX(0);
            float x1 = motionEvent.getX(1);
            float y0 = motionEvent.getY(0);
            float y1 = motionEvent.getY(1);
            distanceY1 = Math.abs(y1 - y0);
            distanceX1 = Math.abs(x1 - x0);
            if (distanceY1 > distanceY0 && distanceX1 > distanceX0) {
                upHandler();
            } else if (distanceY1 < distanceY0 && distanceX1 < distanceX0) {
                downHandler();
            }
        }
        return false;
    } else if (count == 1) {//??
        switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touchesBegan(motionEvent);
            break;

        case MotionEvent.ACTION_MOVE:
            touchesMoved(motionEvent);
            break;

        case MotionEvent.ACTION_UP:
            touchesEnded(motionEvent);
            break;

        }
    }
    return true;
}

From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView2.java

private void onTouchMove(MotionEvent ev) {
    int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
    if (pointerIndex == -1) {
        pointerIndex = 0;/*from   w ww  .j a  va  2  s . c o  m*/
        mActivePointerId = ev.getPointerId(pointerIndex);
    }

    if (mDataChanged) {
        // Re-sync everything if data has been changed
        // since the scroll operation can query the adapter.
        layoutChildren(false);
    }

    final int y = (int) ev.getY(pointerIndex);

    switch (mTouchMode) {
    case TOUCH_MODE_DOWN:
    case TOUCH_MODE_TAP:
    case TOUCH_MODE_DONE_WAITING:
        // Check if we have moved far enough that it looks more like a
        // scroll than a tap. If so, we'll enter scrolling mode.
        if (startScrollIfNeeded(y)) {
            break;
        }
        // Otherwise, check containment within list bounds. If we're
        // outside bounds, cancel any active presses.
        /*final float x = ev.getX(pointerIndex);
        if (!pointInView(x, y, mTouchSlop)) {
            setPressed(false);
            final View motionView = getChildAt(mMotionPosition - mFirstPosition);
            if (motionView != null) {
                motionView.setPressed(false);
            }
            removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ?
                mPendingCheckForTap : mPendingCheckForLongPress);
            mTouchMode = TOUCH_MODE_DONE_WAITING;
            updateSelectorState();
        }*/
        updateSelectorState();
        break;
    case TOUCH_MODE_DRAGGING:
    case TOUCH_MODE_OVERSCROLL:
        scrollIfNeeded(y);
        break;
    }
}

From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView2.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mLastTouchX = (int) ev.getX(newPointerIndex);
        mLastTouchY = (int) ev.getY(newPointerIndex);
        mMotionCorrection = 0;//from   w  ww . ja  v a  2 s . c om
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }
}

From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java

@Override
public boolean onTouchEvent(MotionEvent nativeMotionEvent) {
    if (mKeyboard == null)//I mean, if there isn't any keyboard I'm handling, what's the point?
        return false;
    final int action = MotionEventCompat.getActionMasked(nativeMotionEvent);
    final int pointerCount = MotionEventCompat.getPointerCount(nativeMotionEvent);
    final int oldPointerCount = mOldPointerCount;
    mOldPointerCount = pointerCount;/* w  w w  .  jav a 2  s.  c o m*/
    if (pointerCount > 1)
        mLastTimeHadTwoFingers = SystemClock.elapsedRealtime();//marking the time. Read isAtTwoFingersState()

    if (mTouchesAreDisabledTillLastFingerIsUp) {
        if (mOldPointerCount == 1 && (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP)) {
            mTouchesAreDisabledTillLastFingerIsUp = false;
        }

        return true;
    }
    // TODO: cleanup this code into a multi-touch to single-touch event
    // converter class?
    // If the device does not have distinct multi-touch support panel,
    // ignore all multi-touch
    // events except a transition from/to single-touch.
    if (!mHasDistinctMultitouch && pointerCount > 1 && oldPointerCount > 1) {
        return true;
    }

    // Gesture detector must be enabled only when mini-keyboard is not
    // on the screen.
    if (!mMiniKeyboardVisible && mGestureDetector != null
            && (mGestureDetector.onTouchEvent(nativeMotionEvent))) {
        Log.d(TAG, "Gesture detected!");
        mHandler.cancelKeyTimers();
        dismissKeyPreview();
        return true;
    }

    final long eventTime = nativeMotionEvent.getEventTime();
    final int index = MotionEventCompat.getActionIndex(nativeMotionEvent);
    final int id = nativeMotionEvent.getPointerId(index);
    final int x = (int) nativeMotionEvent.getX(index);
    final int y = (int) nativeMotionEvent.getY(index);

    // Needs to be called after the gesture detector gets a turn, as it
    // may have
    // displayed the mini keyboard
    if (mMiniKeyboard != null && mMiniKeyboardVisible) {
        final int miniKeyboardPointerIndex = nativeMotionEvent.findPointerIndex(mMiniKeyboardTrackerId);
        if (miniKeyboardPointerIndex >= 0 && miniKeyboardPointerIndex < pointerCount) {
            final int miniKeyboardX = (int) nativeMotionEvent.getX(miniKeyboardPointerIndex);
            final int miniKeyboardY = (int) nativeMotionEvent.getY(miniKeyboardPointerIndex);
            MotionEvent translated = generateMiniKeyboardMotionEvent(action, miniKeyboardX, miniKeyboardY,
                    eventTime);
            mMiniKeyboard.onTouchEvent(translated);
            translated.recycle();
        }
        return true;
    }

    if (mHandler.isInKeyRepeat()) {
        // It will keep being in the key repeating mode while the key is
        // being pressed.
        if (action == MotionEvent.ACTION_MOVE) {
            return true;
        }
        final PointerTracker tracker = getPointerTracker(id);
        // Key repeating timer will be canceled if 2 or more keys are in
        // action, and current
        // event (UP or DOWN) is non-modifier key.
        if (pointerCount > 1 && !tracker.isModifier()) {
            mHandler.cancelKeyRepeatTimer();
        }
        // Up event will pass through.
    }

    // TODO: cleanup this code into a multi-touch to single-touch event
    // converter class?
    // Translate mutli-touch event to single-touch events on the device
    // that has no distinct
    // multi-touch panel.
    if (!mHasDistinctMultitouch) {
        // Use only main (id=0) pointer tracker.
        PointerTracker tracker = getPointerTracker(0);
        if (pointerCount == 1 && oldPointerCount == 2) {
            // Multi-touch to single touch transition.
            // Send a down event for the latest pointer.
            tracker.onDownEvent(x, y, eventTime);
        } else if (pointerCount == 2 && oldPointerCount == 1) {
            // Single-touch to multi-touch transition.
            // Send an up event for the last pointer.
            tracker.onUpEvent(tracker.getLastX(), tracker.getLastY(), eventTime);
        } else if (pointerCount == 1 && oldPointerCount == 1) {
            tracker.onTouchEvent(action, x, y, eventTime);
        } else {
            Log.w(TAG, "Unknown touch panel behavior: pointer count is " + pointerCount + " (old "
                    + oldPointerCount + ")");
        }
        return true;
    }

    if (action == MotionEvent.ACTION_MOVE) {
        for (int i = 0; i < pointerCount; i++) {
            PointerTracker tracker = getPointerTracker(nativeMotionEvent.getPointerId(i));
            tracker.onMoveEvent((int) nativeMotionEvent.getX(i), (int) nativeMotionEvent.getY(i), eventTime);
        }
    } else {
        PointerTracker tracker = getPointerTracker(id);
        sendOnXEvent(action, eventTime, x, y, tracker);
    }

    return true;
}

From source file:cc.flydev.launcher.Workspace.java

protected void onWallpaperTap(MotionEvent ev) {
    final int[] position = mTempCell;
    getLocationOnScreen(position);/* ww  w  .j a  v  a2s  .  c  om*/

    int pointerIndex = ev.getActionIndex();
    position[0] += (int) ev.getX(pointerIndex);
    position[1] += (int) ev.getY(pointerIndex);

    mWallpaperManager.sendWallpaperCommand(getWindowToken(),
            ev.getAction() == MotionEvent.ACTION_UP ? WallpaperManager.COMMAND_TAP
                    : WallpaperManager.COMMAND_SECONDARY_TAP,
            position[0], position[1], 0, null);
}

From source file:com.android.internal.widget.ViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//from   www . j a va 2  s  . c o m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

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

    // Always take care of the touch gesture being complete.
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        // Release the drag.
        if (DEBUG)
            Log.v(TAG, "Intercept done!");
        mIsBeingDragged = false;
        mIsUnableToDrag = false;
        mActivePointerId = INVALID_POINTER;
        if (mVelocityTracker != null) {
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
        return false;
    }

    // Nothing more to do here if we have decided whether or not we
    // are dragging.
    if (action != MotionEvent.ACTION_DOWN) {
        if (mIsBeingDragged) {
            if (DEBUG)
                Log.v(TAG, "Being dragged, intercept returning true!");
            return true;
        }
        if (mIsUnableToDrag) {
            if (DEBUG)
                Log.v(TAG, "Unable to drag, intercept returning false!");
            return false;
        }
    }

    switch (action) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionY is set to the y value
        * of the down event.
        */
        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);
        final float x = ev.getX(pointerIndex);
        final float dx = x - mLastMotionX;
        final float xDiff = Math.abs(dx);
        final float y = ev.getY(pointerIndex);
        final float yDiff = Math.abs(y - mInitialMotionY);
        if (DEBUG)
            Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

        if (dx != 0 && !isGutterDrag(mLastMotionX, dx) && canScroll(this, false, (int) dx, (int) x, (int) y)) {
            // Nested view has scrollable area under this point. Let it be handled there.
            mLastMotionX = x;
            mLastMotionY = y;
            mIsUnableToDrag = true;
            return false;
        }
        if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
            if (DEBUG)
                Log.v(TAG, "Starting drag!");
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
            mLastMotionX = dx > 0 ? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
            mLastMotionY = y;
            setScrollingCacheEnabled(true);
        } else if (yDiff > mTouchSlop) {
            // The finger has moved enough in the vertical
            // direction to be counted as a drag...  abort
            // any attempt to drag horizontally, to work correctly
            // with children that have scrolling containers.
            if (DEBUG)
                Log.v(TAG, "Starting unable to drag!");
            mIsUnableToDrag = true;
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            if (performDrag(x)) {
                postInvalidateOnAnimation();
            }
        }
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = ev.getPointerId(0);
        mIsUnableToDrag = false;

        mScroller.computeScrollOffset();
        if (mScrollState == SCROLL_STATE_SETTLING
                && Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {
            // Let the user 'catch' the pager as it animates.
            mScroller.abortAnimation();
            mPopulatePending = false;
            populate();
            mIsBeingDragged = true;
            requestParentDisallowInterceptTouchEvent(true);
            setScrollState(SCROLL_STATE_DRAGGING);
        } else {
            completeScroll(false);
            mIsBeingDragged = false;
        }

        if (DEBUG)
            Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                    + "mIsUnableToDrag=" + mIsUnableToDrag);
        break;
    }

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

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

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mIsBeingDragged;
}

From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView2.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }//from   ww  w .j av a  2 s  .  c  o m

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int actionMasked = ev.getActionMasked();
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        onTouchDown(ev);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        onTouchMove(ev);
        break;
    }

    case MotionEvent.ACTION_UP: {
        onTouchUp(ev);
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        onTouchCancel();
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        final int x = (int) mLastTouchX;
        final int y = (int) mLastTouchY;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View child = getChildAt(motionPosition - mFirstPosition);
            //mMotionViewOriginalTop = child.getTop();
            mMotionPosition = motionPosition;
        }
        mTouchRemainderY = y;
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        // New pointers take over dragging duties
        final int index = ev.getActionIndex();
        final int id = ev.getPointerId(index);
        final int x = (int) ev.getX(index);
        final int y = (int) ev.getY(index);
        mMotionCorrection = 0;
        mActivePointerId = id;
        mLastTouchX = x;
        mLastTouchY = y;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View child = getChildAt(motionPosition - mFirstPosition);
            //mMotionViewOriginalTop = child.getTop();
            mMotionPosition = motionPosition;
        }
        mTouchRemainderY = y;
        break;
    }
    }

    return true;
}

From source file:com.appunite.list.AbsHorizontalListView.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mMotionX = (int) ev.getX(newPointerIndex);
        mMotionY = (int) ev.getY(newPointerIndex);
        mMotionCorrection = 0;//from   ww w .ja v  a2  s.c  o  m
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }
}

From source file:cn.ismartv.tvrecyclerview.widget.RecyclerView.java

private void onPointerUp(MotionEvent e) {
    final int actionIndex = MotionEventCompat.getActionIndex(e);
    if (e.getPointerId(actionIndex) == mScrollPointerId) {
        // Pick a new pointer to pick up the slack.
        final int newIndex = actionIndex == 0 ? 1 : 0;
        mScrollPointerId = e.getPointerId(newIndex);
        mInitialTouchX = mLastTouchX = (int) (e.getX(newIndex) + 0.5f);
        mInitialTouchY = mLastTouchY = (int) (e.getY(newIndex) + 0.5f);
    }//from   ww  w .  ja  va 2 s . com
}