Example usage for android.view MotionEvent getActionIndex

List of usage examples for android.view MotionEvent getActionIndex

Introduction

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

Prototype

public final int getActionIndex() 

Source Link

Document

For #ACTION_POINTER_DOWN or #ACTION_POINTER_UP as returned by #getActionMasked , this returns the associated pointer index.

Usage

From source file:com.blestep.sportsbracelet.view.TimelineChartView.java

/**
 * {@inheritDoc}// w w  w .j a  v a 2 s  .co m
 */
@Override
public boolean onTouchEvent(final MotionEvent event) {

    final int action = event.getActionMasked();
    final int index = event.getActionIndex();
    final int pointerId = event.getPointerId(index);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        // Initialize velocity tracker
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } else {
            mVelocityTracker.clear();
        }
        mVelocityTracker.addMovement(event);
        mScroller.forceFinished(true);
        mState = STATE_INITIALIZE;

        mInitialTouchOffset = mCurrentOffset;
        mInitialTouchX = event.getX();
        mInitialTouchY = event.getY();
        return true;

    case MotionEvent.ACTION_MOVE:
        mVelocityTracker.addMovement(event);
        float diffX = event.getX() - mInitialTouchX;
        float diffY = event.getY() - mInitialTouchY;
        if (Math.abs(diffX) > mTouchSlop || mState >= STATE_MOVING) {
            mCurrentOffset = mInitialTouchOffset + diffX;
            if (mCurrentOffset < 0) {
                onOverScroll();
                mCurrentOffset = 0;
            } else if (mCurrentOffset > mMaxOffset) {
                onOverScroll();
                mCurrentOffset = mMaxOffset;
            }
            mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
            mState = STATE_MOVING;
            ViewCompat.postInvalidateOnAnimation(this);
        } else if (Math.abs(diffY) > mTouchSlop && mState < STATE_MOVING) {
            return false;
        }
        return true;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mState >= STATE_MOVING) {
            final int velocity = (int) VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId);
            mScroller.forceFinished(true);
            mState = STATE_FLINGING;
            mScroller.fling((int) mCurrentOffset, 0, velocity, 0, 0, (int) mMaxOffset, 0, 0);
            ViewCompat.postInvalidateOnAnimation(this);
        } else {
            // Reset scrolling state
            mState = STATE_IDLE;
        }
        return true;
    }
    return false;
}

From source file:com.android.inputmethod.latin.suggestions.SuggestionStripView.java

@Override
public boolean onInterceptTouchEvent(final MotionEvent me) {
    if (mStripVisibilityGroup.isShowingImportantNoticeStrip()) {
        return false;
    }/*from   w w w  . j  a  v a 2s. co m*/
    // Detecting sliding up finger to show {@link MoreSuggestionsView}.
    if (!mMoreSuggestionsView.isShowingInParent()) {
        mLastX = (int) me.getX();
        mLastY = (int) me.getY();
        return mMoreSuggestionsSlidingDetector.onTouchEvent(me);
    }
    if (mMoreSuggestionsView.isInModalMode()) {
        return false;
    }

    final int action = me.getAction();
    final int index = me.getActionIndex();
    final int x = (int) me.getX(index);
    final int y = (int) me.getY(index);
    if (Math.abs(x - mOriginX) >= mMoreSuggestionsModalTolerance
            || mOriginY - y >= mMoreSuggestionsModalTolerance) {
        // Decided to be in the sliding suggestion mode only when the touch point has been moved
        // upward. Further {@link MotionEvent}s will be delivered to
        // {@link #onTouchEvent(MotionEvent)}.
        mNeedsToTransformTouchEventToHoverEvent = AccessibilityUtils.getInstance().isTouchExplorationEnabled();
        mIsDispatchingHoverEventToMoreSuggestions = false;
        return true;
    }

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
        // Decided to be in the modal input mode.
        mMoreSuggestionsView.setModalMode();
    }
    return false;
}

From source file:com.androidinspain.deskclock.alarms.AlarmActivity.java

@Override
public boolean onTouch(View view, MotionEvent event) {
    if (mAlarmHandled) {
        LOGGER.v("onTouch ignored: %s", event);
        return false;
    }//from   w w w .j a va 2 s  . com

    final int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN) {
        LOGGER.v("onTouch started: %s", event);

        // Track the pointer that initiated the touch sequence.
        mInitialPointerIndex = event.getPointerId(event.getActionIndex());

        // Stop the pulse, allowing the last pulse to finish.
        mPulseAnimator.setRepeatCount(0);
    } else if (action == MotionEvent.ACTION_CANCEL) {
        LOGGER.v("onTouch canceled: %s", event);

        // Clear the pointer index.
        mInitialPointerIndex = MotionEvent.INVALID_POINTER_ID;

        // Reset everything.
        resetAnimations();
    }

    final int actionIndex = event.getActionIndex();
    if (mInitialPointerIndex == MotionEvent.INVALID_POINTER_ID
            || mInitialPointerIndex != event.getPointerId(actionIndex)) {
        // Ignore any pointers other than the initial one, bail early.
        return true;
    }

    final int[] contentLocation = { 0, 0 };
    mContentView.getLocationOnScreen(contentLocation);

    final float x = event.getRawX() - contentLocation[0];
    final float y = event.getRawY() - contentLocation[1];

    final int alarmLeft = mAlarmButton.getLeft() + mAlarmButton.getPaddingLeft();
    final int alarmRight = mAlarmButton.getRight() - mAlarmButton.getPaddingRight();

    final float snoozeFraction, dismissFraction;
    if (mContentView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
        snoozeFraction = getFraction(alarmRight, mSnoozeButton.getLeft(), x);
        dismissFraction = getFraction(alarmLeft, mDismissButton.getRight(), x);
    } else {
        snoozeFraction = getFraction(alarmLeft, mSnoozeButton.getRight(), x);
        dismissFraction = getFraction(alarmRight, mDismissButton.getLeft(), x);
    }
    setAnimatedFractions(snoozeFraction, dismissFraction);

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) {
        LOGGER.v("onTouch ended: %s", event);

        mInitialPointerIndex = MotionEvent.INVALID_POINTER_ID;
        if (snoozeFraction == 1.0f) {
            snooze();
        } else if (dismissFraction == 1.0f) {
            dismiss();
        } else {
            if (snoozeFraction > 0.0f || dismissFraction > 0.0f) {
                // Animate back to the initial state.
                AnimatorUtils.reverse(mAlarmAnimator, mSnoozeAnimator, mDismissAnimator);
            } else if (mAlarmButton.getTop() <= y && y <= mAlarmButton.getBottom()) {
                // User touched the alarm button, hint the dismiss action.
                hintDismiss();
            }

            // Restart the pulse.
            mPulseAnimator.setRepeatCount(ValueAnimator.INFINITE);
            if (!mPulseAnimator.isStarted()) {
                mPulseAnimator.start();
            }
        }
    }

    return true;
}

From source file:com.haarman.listviewanimations.itemmanipulation.SwipeDismissListViewTouchListener.java

private boolean handleMoveEvent(MotionEvent motionEvent) {
    if (mPaused || (mVelocityTracker == null)) {
        return false;
    }/*from  w  w  w.  j  a  v a  2s .  c om*/

    mVelocityTracker.addMovement(motionEvent);
    float deltaX = motionEvent.getRawX() - mDownX;
    float deltaY = motionEvent.getRawY() - mDownY;
    if (mTouchChildTouched && !mDisallowSwipe && Math.abs(deltaX) > mSlop
            && Math.abs(deltaX) > Math.abs(deltaY)) {
        mSwiping = true;
        mListView.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
        cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mListView.onTouchEvent(cancelEvent);
    }

    if (mSwiping) {
        if (!mSwipeInitiated) {
            Log.d("SwipeDismissListViewTouchListener", "swipe/begin");
        }
        mSwipeInitiated = true;
        ViewHelper.setTranslationX(mCurrentDismissData.view, deltaX);
        ViewHelper.setAlpha(mCurrentDismissData.view,
                Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        for (View v : getAllTreeChildViews(mCurrentDismissData.view)) {
            ViewHelper.setTranslationX(v, deltaX);
            ViewHelper.setAlpha(v, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
        }
        return true;
    }
    return false;
}

From source file:au.gov.ga.worldwind.androidremote.client.Remote.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (controlling) {
        velocityTracker.addMovement(event);
        velocityTracker.computeCurrentVelocity(1);

        boolean down = event.getActionMasked() == MotionEvent.ACTION_DOWN
                || event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN;
        boolean up = event.getActionMasked() == MotionEvent.ACTION_UP
                || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP;
        Finger[] fingers = new Finger[event.getPointerCount()];
        for (int i = 0; i < event.getPointerCount(); i++) {
            fingers[i] = new Finger(event.getPointerId(i), event.getX(i), event.getY(i),
                    velocityTracker.getXVelocity(i), velocityTracker.getYVelocity(i),
                    !(event.getActionIndex() == i && up));
        }//from   ww  w.j  a v  a 2 s  . com

        FingerMessage<?> message = up ? new UpMessage(fingers)
                : down ? new DownMessage(fingers) : new MoveMessage(fingers);
        communicator.sendMessage(message);
    }
    return super.onTouchEvent(event);
}

From source file:co.adrianblan.fastbrush.MyGLSurfaceView.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    switch (e.getAction()) {

    case MotionEvent.ACTION_DOWN:
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } else {/*w w  w .j  a  v  a  2s.  co  m*/
            mVelocityTracker.clear();
        }

        queueEvent(new Runnable() {
            @Override
            public void run() {
                mRenderer.touchHasStarted();
            }
        });

        // No break is intentional

    case MotionEvent.ACTION_MOVE:

        mVelocityTracker.addMovement(e);

        // Compute velocity in pixels per second
        mVelocityTracker.computeCurrentVelocity(1);

        final ArrayList<TouchData> touchDataList = new ArrayList<>(e.getHistorySize() + 1);
        Vector2 viewportPosition;

        Vector2 viewportVelocity = new Vector2(
                VelocityTrackerCompat.getXVelocity(mVelocityTracker, e.getActionIndex()),
                VelocityTrackerCompat.getYVelocity(mVelocityTracker, e.getActionIndex()));

        // Add previous touch coordinates
        for (int i = 0; i < e.getHistorySize(); i++) {
            viewportPosition = new Vector2(e.getHistoricalX(i), e.getHistoricalY(i));

            touchDataList.add(new TouchData(mRenderer.viewportToWorld(viewportPosition), viewportVelocity,
                    e.getHistoricalSize(i), e.getHistoricalPressure(i)));
        }

        // Add current touch coordinates
        viewportPosition = new Vector2(e.getX(), e.getY());
        touchDataList.add(new TouchData(mRenderer.viewportToWorld(viewportPosition), viewportVelocity,
                e.getSize(), e.getPressure()));

        // Ensure we call switchMode() on the OpenGL thread.
        // queueEvent() is a method of GLSurfaceView that will do this for us.
        queueEvent(new Runnable() {
            @Override
            public void run() {
                mRenderer.addTouchData(touchDataList);
            }
        });

        requestRender();
        break;

    case MotionEvent.ACTION_UP:

        queueEvent(new Runnable() {
            @Override
            public void run() {
                mRenderer.touchHasEnded();
            }
        });
        requestRender();
        break;
    }
    return true;
}

From source file:org.mozilla.gecko.ui.PanZoomController.java

private boolean onTouchEnd(MotionEvent event) {
    switch (mState) {
    case NOTHING:
    case FLING:/*from  ww w.  j  av  a  2s.  c  o m*/
        // should never happen
        Log.e(LOG_NAME, "Received impossible touch end while in " + mState);
        return false;
    case TOUCHING:
        mState = PanZoomState.NOTHING;
        // TODO: send click to gecko
        return false;
    case PANNING:
    case PANNING_HOLD:
        mState = PanZoomState.FLING;
        fling(System.currentTimeMillis());
        return true;
    case PINCHING:
        int points = event.getPointerCount();
        if (points == 1) {
            // last touch up
            mState = PanZoomState.NOTHING;
        } else if (points == 2) {
            int pointRemovedIndex = event.getActionIndex();
            int pointRemainingIndex = 1 - pointRemovedIndex; // kind of a hack
            mState = PanZoomState.TOUCHING;
            mX.touchPos = event.getX(pointRemainingIndex);
            mY.touchPos = event.getY(pointRemainingIndex);
        } else {
            // still pinching, do nothing
        }
        return true;
    }
    Log.e(LOG_NAME, "Unhandled case " + mState + " in onTouchEnd");
    return false;
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

private void handleDown(MotionEvent event) {
    int actionIndex = event.getActionIndex();
    float eventX = event.getX(actionIndex);
    float eventY = event.getY(actionIndex);
    switchToState(STATE_START, eventX, eventY);
    if (!trySwitchToFirstTouchState(eventX, eventY)) {
        mDragging = false;// ww  w  .j a  v a  2 s. c  o  m
    } else {
        mPointerId = event.getPointerId(actionIndex);
        updateGlowPosition(eventX, eventY);
    }
}

From source file:info.bartowski.easteregg.MLand.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    L("touch: %s", ev);
    final int actionIndex = ev.getActionIndex();
    final float x = ev.getX(actionIndex);
    final float y = ev.getY(actionIndex);
    int playerIndex = (int) (getNumPlayers() * (x / getWidth()));
    if (mFlipped)
        playerIndex = getNumPlayers() - 1 - playerIndex;
    switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        poke(playerIndex, x, y);// w  ww.  j  av a  2s . com
        return true;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        unpoke(playerIndex);
        return true;
    }
    return false;
}

From source file:research.sg.edu.edapp.kb.KbSoftKeyboard.java

/**
 * Called by the framework when your view for creating input needs to
 * be generated.  This will be called the first time your input method
 * is displayed, and every time it needs to be re-created such as due to
 * a configuration change.//from  w w w . j a  v  a2  s .c  o  m
 */
@Override
public View onCreateInputView() {
    mInputView = (KbLatinKeyboardView) getLayoutInflater().inflate(R.layout.input, null);
    mInputView.setKeyboard(mQwertyKeyboard);
    mInputView.setOnKeyboardActionListener(this);

    /****************************************************edit 2**************************************************************/

    featuredb = new DataDBHelper(this);
    KbSoftKeyboard gd = new KbSoftKeyboard();
    sf = new Save_feature();
    swipeWord = new GetSwipeWord(this);
    //gDetect.setOnDoubleTapListener(gd);
    mInputView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int index = event.getActionIndex();
            int action = event.getActionMasked();
            int pointerId = event.getPointerId(index);

            //check for actions of motion event
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //retrieve key at current
                char ch = getkeylabel(event);
                if (ch > 0)
                    swipe += ch;
                // test+=swipe.charAt(0);
                //set up start timer for measuring duration
                //  start=System.currentTimeMillis();
                //setup velocity tracker
                if (mvel == null) {
                    // Retrieve a new VelocityTracker object to watch the velocity of a motion.
                    mvel = VelocityTracker.obtain();
                } else {
                    // Reset the velocity tracker back to its initial state.
                    mvel.clear();
                }
            }

            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                mvel.addMovement(event);
                mvel.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                x_vel += abs(VelocityTrackerCompat.getXVelocity(mvel, pointerId));
                y_vel += abs(VelocityTrackerCompat.getYVelocity(mvel, pointerId));
                n_event += 1;
                //  Log.d("", "X velocity: " +  x_vel);
                //  Log.d("", "Y velocity: " +  y_vel);

            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                //record time when finger lifted up
                //           end=System.currentTimeMillis();
                //calculate duration
                //         duration=(end-start)/100;
                //calculate velocity pixels per sec
                //  Log.d("", "X velocity: " +  x_vel);
                // Log.d("", "Y velocity: " +  y_vel);

                velocity = Math.sqrt(x_vel * x_vel + y_vel * y_vel);
                //obtain pressure
                pressure += event.getPressure();
                np_event += 1;
                swipe2 = get_final_string(swipe);
                swipe = "";
                if (swipe2 == null)
                    swipe2 = "";
                //print generated string
                System.out.println(swipe + "\n 2nd " + swipe2);
            }

            if (((int) old_x) == 0 & ((int) old_y) == 0) {
                old_x = event.getX();
                old_y = event.getY();
                swipe += getkeylabel(event);
            } else if (((int) olddir_x) == 0 & ((int) olddir_y) == 0) {
                olddir_x = event.getX() - old_x;
                olddir_y = event.getY() - old_y;
                old_x = event.getX();
                old_y = event.getY();
            } else {
                check_change(event);
            }

            // Return false to avoid consuming the touch event
            return false;
        }
    });

    //***********************************************edit 2 end*****************************************************************

    //Saved my life
    if (mInputView.isPreviewEnabled())
        mInputView.setPreviewEnabled(false);
    return mInputView;
}