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:Main.java

public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, VelocityTracker tracker) {

    // Check the dot product of current velocities.
    // If the pointer that left was opposing another velocity vector, clear.
    tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
    final int upIndex = ev.getActionIndex();
    final int id1 = ev.getPointerId(upIndex);
    final float x1 = tracker.getXVelocity(id1);
    final float y1 = tracker.getYVelocity(id1);
    for (int i = 0, count = ev.getPointerCount(); i < count; i++) {
        if (i == upIndex)
            continue;

        final int id2 = ev.getPointerId(i);
        final float x = x1 * tracker.getXVelocity(id2);
        final float y = y1 * tracker.getYVelocity(id2);

        final float dot = x + y;
        if (dot < 0) {
            tracker.clear();/*from   w ww.  jav  a2 s .  c  o  m*/
            break;
        }
    }
}

From source file:me.wizos.loread.view.webview.NestedScrollWebView.java

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = ev.getActionIndex();
    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;
        mLastMotionY = (int) ev.getY(newPointerIndex);
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }//from w  w  w . j a v a2 s .c  o m
}

From source file:com.android.inputmethod.accessibility.KeyboardAccessibilityDelegate.java

/**
 * Get a key that a hover event is on.//  w w w  .  j  av a2s.  co m
 *
 * @param event The hover event.
 * @return key The key that the <code>event</code> is on.
 */
protected final Key getHoverKeyOf(final MotionEvent event) {
    final int actionIndex = event.getActionIndex();
    final int x = (int) event.getX(actionIndex);
    final int y = (int) event.getY(actionIndex);
    return mKeyDetector.detectHitKey(x, y);
}

From source file:com.google.blockly.android.ui.PendingDrag.java

/**
 * Compares if {@code event} on {@code touchedView} is a continuation of the event stream
 * tracked by this PendingDrag.  This includes whether the event stream has had sufficient
 * regular updates, at least more often than {@link #MAX_MOTION_EVENT_MILLISECONDS_DELTA}
 * (in an effort to disregard it from dropped previous streams with dropped
 * {@link MotionEvent#ACTION_UP} and {@link MotionEvent#ACTION_CANCEL}).  If that threshold
 * is exceeded (for matching view and pointer id), the PendingDrag will no longer be alive
 * ({@link #isAlive()}, and not match any future events.
 * <p/>//  www. j  a v a 2s . c o  m
 * If the event is a match and alive, it will pass the event through a {@link GestureDetector}
 * to determine if the event triggers a click (or other interesting gestures in the future).
 * Check {@link #isClick()} to determine whether a click was detected.
 * <p/>
 * This method should only be called from {@link Dragger#onTouchBlockImpl}.
 *
 * @param event The event to compare to.
 * @param touchedView The view that received the touch event.
 * @return Whether the event was a match and the drag is still alive.
 */
boolean isMatchAndProcessed(MotionEvent event, BlockView touchedView) {
    if (!mAlive) {
        return false;
    }

    final int pointerId = event.getPointerId(event.getActionIndex());
    long curEventTime = event.getEventTime();
    long deltaMs = curEventTime - mLatestEventTime;
    if (deltaMs < MAX_MOTION_EVENT_MILLISECONDS_DELTA) {
        if (pointerId == mPointerId && touchedView == mTouchedView) {
            mLatestEventTime = curEventTime;
            mGestureDetector.onTouchEvent(event);
            return true;
        }
    } else {
        mAlive = false; // Exceeded threshold and expired.
    }

    return false; // Not a pointer & view match or died.
}

From source file:com.arthurpitman.common.SlidingPanel.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float y = event.getY();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(event.getActionIndex());

    switch (action) {
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_DOWN:
        startTouchY = y;//ww w. j  av a  2  s .c  o  m
        startTouchScrollY = getScrollY();

        // if touch didn't occur on the actual control, ignore it
        float touchBoundary = viewHeight - collapsedHeight - startTouchScrollY;
        if (y < touchBoundary) {
            return false;
        }

        // start tracking velocity
        if (velocityTracker == null) {
            velocityTracker = VelocityTracker.obtain();
        } else {
            velocityTracker.clear();
        }
        velocityTracker.addMovement(event);
        break;

    case MotionEvent.ACTION_MOVE:
        // determine if a valid touch has started
        if (Math.abs(y - startTouchY) > touchSlop) {
            touchState = TOUCH_STARTED;
        }
        if (velocityTracker != null) {
            velocityTracker.addMovement(event);
        }

        // scroll as appropriate
        if (touchState == TOUCH_STARTED) {
            final int scrollDelta = (int) (startTouchY - y);
            scrollTo(0, Math.max(0, Math.min(viewHeight, startTouchScrollY + scrollDelta)));
        }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (touchState == TOUCH_STARTED) {
            final int currentScrollY = getScrollY();

            // get velocity
            float velocity = 0;
            if (velocityTracker != null) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = VelocityTrackerCompat.getYVelocity(velocityTracker, pointerId);
                velocityTracker.recycle();
                velocityTracker = null;
            }

            // snap to final scroll position
            int target = startTouchScrollY;
            if ((Math.abs(velocity) > minimumflingVelocity) || (Math.abs(y - startTouchY) > (viewHeight / 2))) {
                if (velocity < 0) {
                    target = viewHeight;
                } else {
                    target = 0;
                }
            }
            scroller.startScroll(0, currentScrollY, 0, target - currentScrollY);
            invalidate();
            touchState = TOUCH_NONE;
        }
        break;
    }
    return true;
}

From source file:com.jwork.spycamera.MainFragment.java

@Override
public boolean onTouch(View view, MotionEvent event) {
    log.v(this, "onTouch(action:" + event.getAction() + "|actionindex:" + event.getActionIndex() + "|downtime:"
            + event.getDownTime() + "|eventtime:" + event.getEventTime() + ")");
    if (view == layoutBlack) {
        sgdBlack.onTouchEvent(event);//from  ww  w  .  j a  v a 2  s .c om
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            controller.blackScreenClick();
        }
        return true;
    } else if (view == svPreview) {
        sgdPreview.onTouchEvent(event);
        return true;
    }
    return false;
}

From source file:ch.jeda.platform.android.CanvasFragment.java

@Override
public boolean onTouch(final View view, final MotionEvent event) {
    int index;/*  w w  w . j  a v a  2s . c  o  m*/
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        index = event.getActionIndex();
        this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_DOWN, event.getPointerId(index),
                (int) event.getX(index), (int) event.getY(index)));
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        index = event.getActionIndex();
        this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_UP, event.getPointerId(index),
                (int) event.getX(index), (int) event.getY(index)));
        break;
    case MotionEvent.ACTION_MOVE:
        for (index = 0; index < event.getPointerCount(); ++index) {
            this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_MOVED,
                    event.getPointerId(index), (int) event.getX(index), (int) event.getY(index)));
        }

        break;
    }
    return true;
}

From source file:com.intel.xdk.multitouch.MultiTouch.java

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    //get convenience reference to activity
    activity = cordova.getActivity();/*from w w  w. j  a  v  a 2 s.co  m*/

    activity.runOnUiThread(new Runnable() {
        public void run() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                try {
                    Method m = WebView.class.getMethod("setWebContentsDebuggingEnabled", boolean.class);
                    m.invoke(WebView.class, true);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //WebView.setWebContentsDebuggingEnabled(true);
            }
        }
    });

    touchy = new View.OnTouchListener() {

        private SparseArray<Long> pointerId2Identifier = new SparseArray<Long>();

        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            if (isMultitouchEnabled) {
                int pointerCount = ev.getPointerCount();

                //get the correct action
                int maskedAction = ev.getActionMasked();
                int idx = ev.getActionIndex();
                try {
                    String js = null;
                    if (maskedAction == MotionEvent.ACTION_POINTER_DOWN
                            || maskedAction == MotionEvent.ACTION_DOWN) {
                        int x = (int) ev.getX(idx);
                        int y = (int) ev.getY(idx);
                        int h = (int) v.getHeight();
                        int w = (int) v.getWidth();
                        int id = ev.getPointerId(idx);
                        //make a timestamp identifier and store it
                        long identifier = System.currentTimeMillis();
                        pointerId2Identifier.put(id, identifier);
                        js = String.format("{id:%d,x:%d,y:%d,h:%d,w:%d,type:'touchstart'},", identifier, x, y,
                                h, w);
                        queueMultitouchData(js, id, maskedAction);
                    } else if (maskedAction == MotionEvent.ACTION_POINTER_UP
                            || maskedAction == MotionEvent.ACTION_UP) {
                        int x = (int) ev.getX(idx);
                        int y = (int) ev.getY(idx);
                        int h = (int) v.getHeight();
                        int w = (int) v.getWidth();
                        int id = ev.getPointerId(idx);
                        js = String.format("{id:%d,x:%d,y:%d,h:%d,w:%d,type:'touchend'},",
                                pointerId2Identifier.get(id), x, y, h, w);
                        pointerId2Identifier.remove(id);
                        queueMultitouchData(js, id, maskedAction);
                    } else if (maskedAction == MotionEvent.ACTION_MOVE) {
                        //send all events if it is a move
                        for (int i = 0; i < pointerCount; i++) {
                            int x = (int) ev.getX(i);
                            int y = (int) ev.getY(i);
                            int h = (int) v.getHeight();
                            int w = (int) v.getWidth();
                            int id = ev.getPointerId(i);
                            js = String.format("{id:%d,x:%d,y:%d,h:%d,w:%d,type:'touchmove'},",
                                    pointerId2Identifier.get(id), x, y, h, w);
                            queueMultitouchData(js, id, maskedAction);
                        }
                    } else {
                        Log.e("[intel.xdk]", "got a MotionEvent that is not up/down/move:" + ev);
                    }
                    //Log.i("[intel.xdk]", "onTouchEvent:"+js);
                } catch (Exception e) {
                    Log.e("[intel.xdk]", "Got an exception back from WebView: ", e);
                }

                return true;
            } else {
                return false;
            }
        }
    };
    webView.setOnTouchListener(touchy);

    lock = this;
    messages = new ArrayList<String>(0);
}

From source file:com.example.android.bluetoothchat.BluetoothFragment.java

private void setMainView(int mode) {

    switch (mode) {
    case MODE_SEND_PHONENUM:
        mainMode = MODE_SEND_PHONENUM;/*from   w  ww .  jav  a  2 s. co  m*/
        cardGameLayout.setVisibility(View.GONE);
        gestureOverlayView.setVisibility(View.VISIBLE);
        gestureOverlayView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int pointerIndex = event.getActionIndex();
                int pointerID = event.getPointerId(pointerIndex);
                float touchXValue = event.getX(pointerIndex);
                float touchYValue = event.getY(pointerIndex);
                touchX.setText(String.valueOf(touchXValue));
                touchY.setText(String.valueOf(touchYValue));

                if (touchYValue <= 0) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {//? ? ? ??   .
                        signal = phoneNum;
                        sendSignal(signal);
                    }
                }
                return false;
            }
        });
        break;
    case MODE_SEND_DRAW:
        mainMode = MODE_SEND_DRAW;
        cardGameLayout.setVisibility(View.INVISIBLE);
        gestureOverlayView.setVisibility(View.INVISIBLE);
        sendDrawLayout = (RelativeLayout) getActivity().findViewById(R.id.send_draw_layout);
        sendDrawLayout.setVisibility(View.VISIBLE);
        Log.d(TAG, sendDrawLayout.getWidth() + " isjsafdffs");
        drawView = new DrawView(getActivity(), sendDrawLayout, getActivity(), mChatService);
        sendDrawLayout.addView(drawView);
        break;
    case MODE_CARD_GAME:
        mainMode = MODE_CARD_GAME;
        gestureOverlayView.setVisibility(View.GONE);
        cardGameLayout.setVisibility(View.VISIBLE);
        //ViewFlipper 
        break;
    case MODE_BOMB_GAME:
        mainMode = MODE_BOMB_GAME;
        break;
    }
}

From source file:com.github.crvv.wubinput.wubi.dictionary.suggestions.SuggestionStripView.java

@Override
public boolean onTouchEvent(final MotionEvent me) {
    // In the sliding input mode. {@link MotionEvent} should be forwarded to
    // {@link MoreSuggestionsView}.
    final int index = me.getActionIndex();
    final int x = mMoreSuggestionsView.translateX((int) me.getX(index));
    final int y = mMoreSuggestionsView.translateY((int) me.getY(index));
    me.setLocation(x, y);/*from w w w  . ja  va2  s  .  c  o  m*/
    if (!mNeedsToTransformTouchEventToHoverEvent) {
        mMoreSuggestionsView.onTouchEvent(me);
        return true;
    }
    // In sliding suggestion mode with accessibility mode on, a touch event should be
    // transformed to a hover event.
    final int width = mMoreSuggestionsView.getWidth();
    final int height = mMoreSuggestionsView.getHeight();
    final boolean onMoreSuggestions = (x >= 0 && x < width && y >= 0 && y < height);
    if (!onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) {
        // Just drop this touch event because dispatching hover event isn't started yet and
        // the touch event isn't on {@link MoreSuggestionsView}.
        return true;
    }
    final int hoverAction;
    if (onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) {
        // Transform this touch event to a hover enter event and start dispatching a hover
        // event to {@link MoreSuggestionsView}.
        mIsDispatchingHoverEventToMoreSuggestions = true;
        hoverAction = MotionEvent.ACTION_HOVER_ENTER;
    } else if (me.getActionMasked() == MotionEvent.ACTION_UP) {
        // Transform this touch event to a hover exit event and stop dispatching a hover event
        // after this.
        mIsDispatchingHoverEventToMoreSuggestions = false;
        mNeedsToTransformTouchEventToHoverEvent = false;
        hoverAction = MotionEvent.ACTION_HOVER_EXIT;
    } else {
        // Transform this touch event to a hover move event.
        hoverAction = MotionEvent.ACTION_HOVER_MOVE;
    }
    me.setAction(hoverAction);
    mMoreSuggestionsView.onHoverEvent(me);
    return true;
}