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.juick.android.MessagesFragment.java

public Boolean maybeInterceptTouchEventFromActivity(MotionEvent event) {
    if (rightScrollBound == 0) {
        Log.w("JAGP", "rightScrollBound == 0");
        return null;
    }//from  w w  w .  j  av  a2s.c om
    if (getActivity() == null || true)
        return null;
    int action = event.getAction();
    int actionMasked = event.getActionMasked();
    if (fragInParent == null)
        fragInParent = getActivity().findViewById(R.id.messagesfragment);
    if (action == MotionEvent.ACTION_DOWN || actionMasked == MotionEvent.ACTION_POINTER_DOWN) {
        if (mScrollState == SCROLL_STATE_IDLE && fragInParent.getAnimation() == null) {
            if (!canStartScroll(event)) {
                mIsUnableToDrag = true;
                return null;
            }
            if (event.getPointerCount() == 1) {
                Log.w("JAGP", "action_down 1");
                navigationOpenMode = fragInParent.getLeft() > 0;
                mLastMotionX = mInitialMotionX = event.getX();
                mLastMotionY = event.getY();
                currentScrollX = 0;
                mActivePointerId = MotionEventCompat.getPointerId(event, 0);
                mIsBeingDragged = false;
                mIsUnableToDrag = false;
            } else {
                Log.w("JAGP", "action_down 2");
                if (mIsBeingDragged) {
                    if (!navigationOpenMode) {
                        Log.w("JAGP", "action_down 3");
                        setScrollState(SCROLL_STATE_IDLE);
                        scrollToX(0, 500);
                    }
                }
                mIsUnableToDrag = true;
            }
        } else {
            Log.w("JAGP", "!(mScrollState == SCROLL_STATE_IDLE && fragInParent.getAnimation() == null)");
        }
    }
    if (action == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_POINTER_UP) {
        if (mIsUnableToDrag)
            return null;
        if (mIsBeingDragged) {
            final int activePointerId = mActivePointerId;
            if (activePointerId == INVALID_POINTER) {
                // If we don't have a valid id, the touch down wasn't on content.
                return null;
            }
            int pointerIndex = MotionEventCompat.findPointerIndex(event, activePointerId);
            if (pointerIndex == event.getActionIndex()) {
                if (!navigationOpenMode) {
                    // commenting needed.
                    if (lastToXDelta != null) {
                        if (Math.abs(lastToXDelta) < rightScrollBound / 2) {
                            Log.w("JAGP", "action_up 1");
                            setScrollState(SCROLL_STATE_SETTLING);
                            scrollToX(0, 500);
                        } else {
                            Log.w("JAGP", "action_up 2");
                            setScrollState(SCROLL_STATE_SETTLING);
                            scrollToX((int) -rightScrollBound, 200);
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    Log.w("JAGP", "action_up 3");
                                    ((MainActivity) getActivity()).openNavigationMenu(false);
                                    lastToXDelta = 0;
                                    fragInParent.clearAnimation();
                                }
                            }, 200);
                        }
                    }
                } else {
                    Log.w("JAGP", "action_up 4");
                    mIsBeingDragged = false;
                    setScrollState(SCROLL_STATE_SETTLING);
                    scrollToX((int) rightScrollBound, 200);
                    mActivePointerId = INVALID_POINTER;
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Log.w("JAGP", "action_up 5");
                            ((MainActivity) getActivity()).closeNavigationMenu(false, true);

                            lastToXDelta = 0;
                            fragInParent.clearAnimation();
                        }
                    }, 200);

                }
                return null;
            }
        }
    }
    if (action == MotionEvent.ACTION_MOVE) {
        if (mIsUnableToDrag)
            return null;
        if (mScrollState == SCROLL_STATE_SETTLING)
            return null;

        /*
         * 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.
        */
        MotionEvent ev = event;
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            return null;
        }

        int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
        float x = MotionEventCompat.getX(ev, pointerIndex);
        float dx = x - mLastMotionX;
        float xDiff = Math.abs(dx);
        float yDiff = Math.abs(MotionEventCompat.getY(ev, pointerIndex) - mLastMotionY);

        if (!mIsBeingDragged) {
            if (isListAnyPressed()) {
                //Log.w("JAGP","action_move 1");
                mIsUnableToDrag = true;
            } else if (xDiff > mTouchSlop && xDiff > yDiff) {
                mIsBeingDragged = true;
                setScrollState(SCROLL_STATE_DRAGGING);
                mLastMotionX = x;
                //Log.w("JAGP","action_move 2");
                return null;
            } 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.
                    //Log.w("JAGP","action_move 3");
                    mIsUnableToDrag = true;
                }
            }
        }

        if (mIsBeingDragged) {
            //Log.w("JAGP","action_move 4");
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final int width = getListView().getWidth();
            final int widthWithMargin = width;

            final float leftBound = -widthWithMargin;
            if (navigationOpenMode) {
                if (scrollX < 0) {
                    scrollX = 0;
                } else if (scrollX > rightScrollBound) { // prevent too much to left
                    scrollX = rightScrollBound;
                }
            } else {
                if (scrollX > 0) {
                    scrollX = 0;
                } else if (scrollX > rightScrollBound) {
                    scrollX = rightScrollBound;
                }
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollToX((int) scrollX, 1);
            return null;
        }

    }
    return null;
}

From source file:com.csounds.examples.tests.MultiTouchXYActivity.java

/** Called when the activity is first created. */
@Override//from w  ww  .  j  a  v  a 2s  .  co m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    for (int i = 0; i < touchIds.length; i++) {
        touchIds[i] = -1;
        touchX[i] = -1;
        touchY[i] = -1;
    }
    /*      File outputFile = new File(DIR_NAME);
    File outputDir = this.getCacheDir(); // context being the Activity pointer
    try {
       outputFile = File.createTempFile("temp", "txt", outputDir);
    } catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    ContextWrapper cw = new ContextWrapper(this);
    File directory = cw.getExternalFilesDir(null);
    txtfile = new File(directory,"temp.txt");
     */
    multiTouchView = new View(this);
    setContentView(R.layout.multitouchxy);
    multiTouchView = (RelativeLayout) findViewById(R.id.multitouchxy);
    Button loadPresetButton = (Button) findViewById(R.id.load_button);
    this.createLoadListener(loadPresetButton);
    multiTouchView.setOnTouchListener(new OnTouchListener() {

        /* (non-Javadoc)
         * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
         */
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction() & MotionEvent.ACTION_MASK;
            switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:

                for (int i = 0; i < event.getPointerCount(); i++) {
                    int pointerId = event.getPointerId(i);
                    int id = getTouchId(pointerId);

                    if (id == -1) {

                        id = getTouchIdAssignment();

                        if (id != -1) {
                            touchIds[id] = pointerId;
                            touchX[id] = event.getX(i) / multiTouchView.getWidth();
                            touchY[id] = 1 - (event.getY(i) / multiTouchView.getHeight());
                            //TODO calculte interval Y
                            int a = Math.round(id / 440);

                            System.out.println("y " + a);

                            if (touchXPtr[id] != null) {
                                //TODO swtich touchid
                                Log.d("touchXtouchY",
                                        "touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]
                                                + " multitouchview width " + multiTouchView.getWidth()
                                                + " multitouchview width " + multiTouchView.getHeight());

                                // get the key from the touch coordinates
                                float[] touchArray = new float[2];
                                touchArray = evaluateTouchKey(touchX[id], touchY[id]);
                                touchX[id] = touchArray[0];
                                touchY[id] = touchArray[1];

                                Log.d("touchXtouchY",
                                        "touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]);
                                touchXPtr[id].SetValue(0, touchX[id]);
                                touchYPtr[id].SetValue(0, touchY[id]);
                                csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id));
                            }
                            //write file
                            /*            System.out.println(String.format(
                                  "i1.%d 0 -2 %d", id, a));
                            writeToFile(String.format(
                                  "i1.%d 0 -2 %d", id, a));
                             */
                        }
                    }

                }

                break;
            case MotionEvent.ACTION_MOVE:

                for (int i = 0; i < event.getPointerCount(); i++) {
                    int pointerId = event.getPointerId(i);
                    int id = getTouchId(pointerId);

                    if (id != -1) {
                        touchX[id] = event.getX(i) / multiTouchView.getWidth();
                        touchY[id] = 1 - (event.getY(i) / multiTouchView.getHeight());

                    }

                    //write file
                    /*         System.out.println(String.format(
                          "i1.%d 0 -2 %d", id, id));
                    writeToFile(String.format(
                          "i1.%d 0 -2 %d", id, id));
                     */
                }

                break;
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP: {
                int activePointerIndex = event.getActionIndex();
                int pointerId = event.getPointerId(activePointerIndex);

                int id = getTouchId(pointerId);
                if (id != -1) {
                    touchIds[id] = -1;
                    csoundObj.sendScore(String.format("i-1.%d 0 0 %d", id, id));
                }
                //write file
                /*         System.out.println(String.format(
                      "i1.%d 0 -2 %d", id, id));
                writeToFile(String.format(
                      "i1.%d 0 -2 %d", id, id));
                 */
            }
                break;
            }

            return true;
        }

    });

    setContentView(multiTouchView);

    String csd = getResourceFileAsString(R.raw.multitouch_xy_kx);
    File f = createTempFile(csd);

    csoundObj.addBinding(this);

    csoundObj.startCsound(f);
}

From source file:com.android.systemui.statusbar.phone.NotificationPanelView.java

private boolean handleQsTouch(MotionEvent event) {
    final int action = event.getActionMasked();
    // screen statusBar animator by yangfan 
    if (mStatusBarState == StatusBarState.KEYGUARD) {
        if (event.getY() < getResources().getDimensionPixelOffset(R.dimen.status_bar_height))
            //modify by mare 2017/3/2
            //=====================>
            //?true??action????
            return false;
        //<=====================
        //modify by mare 2017/3/2
    }//www .j  av  a 2 s. c o  m
    // screen statusBar animator by yangfan 
    if (action == MotionEvent.ACTION_DOWN && getExpandedFraction() == 1f
            && mStatusBar.getBarState() != StatusBarState.KEYGUARD && !mQsExpanded && mQsExpansionEnabled) {

        // Down in the empty area while fully expanded - go to QS.
        mQsTracking = true;
        mConflictingQsExpansionGesture = true;
        onQsExpansionStarted();
        mInitialHeightOnTouch = mQsExpansionHeight;
        mInitialTouchY = event.getX();
        mInitialTouchX = event.getY();
    }
    if (!isFullyCollapsed()) {
        handleQsDown(event);
    }
    if (!mQsExpandImmediate && mQsTracking) {
        onQsTouch(event);
        if (!mConflictingQsExpansionGesture) {
            return true;
        }
    }
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mConflictingQsExpansionGesture = false;
    }
    if (action == MotionEvent.ACTION_DOWN && isFullyCollapsed() && mQsExpansionEnabled) {
        mTwoFingerQsExpandPossible = true;
    }
    if (mTwoFingerQsExpandPossible && isOpenQsEvent(event)
            && event.getY(event.getActionIndex()) < mStatusBarMinHeight) {
        MetricsLogger.count(mContext, COUNTER_PANEL_OPEN_QS, 1);
        mQsExpandImmediate = true;
        requestPanelHeightUpdate();

        // Normally, we start listening when the panel is expanded, but here we need to start
        // earlier so the state is already up to date when dragging down.
        setListening(true);
    }
    return false;
}

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

protected void onWallpaperTap(MotionEvent ev) {
    final int[] position = mTempCell;
    getLocationOnScreen(position);//from w  w w. ja v a  2 s.  c  o m

    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:es.ugr.swad.swadroid.gui.SwipeListViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }/* w  w w .j a  va  2 s.  com*/

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (mPaused) {
            return false;
        }

        // TODO: ensure this is a finger, and set a flag

        // Find the child view that was touched (perform a hit test)
        Rect rect = new Rect();
        int childCount = mListView.getChildCount();
        int[] listViewCoords = new int[2];
        mListView.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 = mListView.getChildAt(i);
            child.getHitRect(rect);
            if (rect.contains(x, y)) {
                mDownView = child;
                break;
            }
        }

        if ((mDownView != null) && (mListView != null)) {
            mDownX = motionEvent.getRawX();
            mDownPosition = mListView.getPositionForView(mDownView);

            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(500); // 1000 by defaut but it was too much
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean swipe = false;
        boolean swipeRight = false;

        if (Math.abs(deltaX) > mViewWidth / 2) {
            swipe = true;
            swipeRight = deltaX > 0;
        } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
            swipe = true;
            swipeRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (swipe) {
            // sufficient swipe value
            final View downView = mDownView; // mDownView gets null'd before animation ends
            final int downPosition = mDownPosition;
            final boolean toTheRight = swipeRight;
            ++mDismissAnimationRefCount;
            mDownView.animate().translationX(swipeRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performSwipeAction(downView, downPosition, toTheRight,
                                    toTheRight ? dismissRight : dismissLeft);
                        }
                    });
        } else {
            // cancel
            mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker = null;
        mDownX = 0;
        mDownView = null;
        mDownPosition = ListView.INVALID_POSITION;
        mSwiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null || mPaused) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        if (Math.abs(deltaX) > mSlop) {
            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) {
            mCallback.onStartSwipe();
            mDownView.setTranslationX(deltaX);
            mDownView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:com.nononsenseapps.feeder.ui.SwipeDismissTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // offset because the view is translated during swipe
    motionEvent.offsetLocation(mTranslationX, 0);

    if (mViewWidth < 2) {
        mViewWidth = mView.getWidth();/*from w  w w .  ja v a 2 s . c o  m*/
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        // TODO: ensure this is a finger, and set a flag
        mDownX = motionEvent.getRawX();
        mDownY = motionEvent.getRawY();
        if (mCallbacks.canDismiss(mToken)) {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(motionEvent);
        }
        return false;
    }

    case MotionEvent.ACTION_UP: {
        if (mVelocityTracker == null) {
            break;
        }

        float deltaX = motionEvent.getRawX() - mDownX;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX && absVelocityY < absVelocityX && mSwiping) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }
        if (dismiss) {
            // dismiss
            mSwipingView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth)
                    //.alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss();
                        }
                    });
        } else if (mSwiping) {
            // cancel
            mSwipingView.animate().translationX(0)
                    //.alpha(1)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mCallbacks.onSwipeCancelled();
                        }
                    });
        }
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        notNotifiedSwipeStart = true;
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mVelocityTracker == null) {
            break;
        }

        mSwipingView.animate().translationX(0)
                //.alpha(1)
                .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mCallbacks.onSwipeCancelled();
                    }
                });
        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        notNotifiedSwipeStart = true;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mVelocityTracker == null) {
            break;
        }

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) {
            mSwiping = true;
            mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop);
            mView.getParent().requestDisallowInterceptTouchEvent(true);

            // Cancel listview's touch
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            mView.onTouchEvent(cancelEvent);
            cancelEvent.recycle();
        }

        if (mSwiping) {
            if (notNotifiedSwipeStart) {
                notNotifiedSwipeStart = false;
                mCallbacks.onSwipeStarted(deltaX > 0);
            }
            mTranslationX = deltaX;
            mSwipingView.setTranslationX(deltaX - mSwipingSlop);
            //mView.setAlpha(mInterpolator.getInterpolation(1f - 1f * Math.abs(deltaX) / mViewWidth));
            //                    mView.setAlpha(Math.max(0f, Math.min(1f,
            //                            1f - 2f * Math.abs(deltaX) / mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

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  a v  a  2 s  . c  om*/

    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.csounds.examples.tests.SynthActivity.java

/** Called when the activity is first created. */
@Override//from  w  w w  . j  a  v  a2 s .  c om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    for (int i = 0; i < touchIds.length; i++) {
        touchIds[i] = -1;
        touchX[i] = -1;
        touchY[i] = -1;
    }
    multiTouchView = new View(this);

    setContentView(R.layout.synth2);
    multiTouchView = (RelativeLayout) findViewById(R.id.synth);
    octPlusButton = (Button) findViewById(R.id.plusoctavebutton);
    octMinusButton = (Button) findViewById(R.id.minusoctavebutton);
    diode4 = (ToggleButton) findViewById(R.id.diode4);
    diode3 = (ToggleButton) findViewById(R.id.diode3);
    LcdScreenView lcd = (LcdScreenView) findViewById(R.id.lcd_screen);
    new DrumMachineLcdUpdater(lcd.getModel());
    this.createLoadListener((View) lcd);
    keyboardLayout = (RelativeLayout) findViewById(R.id.keyboard);
    keyboardLayout.setOnTouchListener(new OnTouchListener() {

        /* (non-Javadoc)
         * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
         */
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction() & MotionEvent.ACTION_MASK;
            float[] touchArrayDown = new float[2];
            switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:

                for (int i = 0; i < event.getPointerCount(); i++) {
                    int pointerId = event.getPointerId(i);
                    int id = getTouchId(pointerId);

                    if (id == -1) {

                        id = getTouchIdAssignment();

                        if (id != -1) {
                            touchIds[id] = pointerId;
                            touchX[id] = event.getX(i) / keyboardLayout.getWidth();
                            touchY[id] = 1 - (event.getY(i) / keyboardLayout.getHeight());
                            //TODO calculte interval Y

                            if (touchXPtr[id] != null) {
                                //TODO swtich touchid
                                //                           Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id] + " multitouchview width " + multiTouchView.getWidth() + " multitouchview width " + multiTouchView.getHeight());
                                //                           Log.d("touchXtouchY","keyboardLayout width " + keyboardLayout.getWidth() + " keyboardLayout width " + keyboardLayout.getHeight());
                                // get the key from the touch coordinates
                                float[] touchArray = new float[2];
                                touchArray = evaluateTouchKey(touchX[id], touchY[id]);
                                touchArrayDown[0] = touchX[id];
                                touchArrayDown[1] = touchY[id];
                                touchX[id] = touchArray[0];
                                touchY[id] = touchArray[1];

                                //                           Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]);
                                touchXPtr[id].SetValue(0, touchX[id]);
                                touchYPtr[id].SetValue(0, touchY[id]);
                                csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id));
                            }

                        }
                    }

                }

                break;
            case MotionEvent.ACTION_MOVE:

                for (int i = 0; i < event.getPointerCount(); i++) {
                    int pointerId = event.getPointerId(i);
                    int id = getTouchId(pointerId);

                    if (id != -1) {
                        touchIds[id] = pointerId;
                        touchX[id] = event.getX(i) / keyboardLayout.getWidth();
                        touchY[id] = 1 - (event.getY(i) / keyboardLayout.getHeight());
                        //TODO calculte interval Y

                        if (touchXPtr[id] != null) {
                            //TODO swtich touchid
                            //                           Log.d("aaa","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id] + " multitouchview width " + multiTouchView.getWidth() + " multitouchview width " + multiTouchView.getHeight());
                            //                           Log.d("aaa","keyboardLayout width " + keyboardLayout.getWidth() + " keyboardLayout width " + keyboardLayout.getHeight());
                            // get the key from the touch coordinates
                            float[] touchArray = new float[2];

                            touchArray = evaluateTouchKey(touchX[id], touchY[id]);

                            touchX[id] = touchArray[0];
                            touchY[id] = touchArray[1];

                            //                           Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]);
                            touchXPtr[id].SetValue(0, touchX[id]);
                            touchYPtr[id].SetValue(0, touchY[id]);
                            csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id));
                        }

                    }

                }
                break;

            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP: {
                int activePointerIndex = event.getActionIndex();
                int pointerId = event.getPointerId(activePointerIndex);

                int id = getTouchId(pointerId);
                if (id != -1) {
                    touchIds[id] = -1;
                    csoundObj.sendScore(String.format("i-1.%d 0 0 %d", id, id));
                }
                //write file
                /*         System.out.println(String.format(
                      "i1.%d 0 -2 %d", id, id));
                writeToFile(String.format(
                      "i1.%d 0 -2 %d", id, id));
                 */
            }
                break;
            }

            return true;
        }

    });

    OnTouchListener octBtnOk = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (octNbr < 12)
                    octNbr += 12;
                break;
            }
            return false;
        }
    };
    octPlusButton.setOnTouchListener(octBtnOk);

    OnTouchListener octBtnMinus = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (octNbr > -36)
                    octNbr -= 12;

                break;
            }
            return false;
        }
    };

    octMinusButton.setOnTouchListener(octBtnMinus);

    String csd = getResourceFileAsString(R.raw.multitouch_xy_kx);
    File f = createTempFile(csd);

    csoundObj.addBinding(this);

    csoundObj.startCsound(f);
    //   initknobs();

    initSeekBar(diode3, diode4);

}

From source file:com.hippo.widget.BothScrollView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*/*from w w  w.  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.
     */

    /*
    * Shortcut the most recurring case: the user is in the dragging
    * state and he is moving his finger.  We want to intercept this
    * motion.
    */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
        return true;
    }

    /*
     * Don't try to intercept touch if we can't scroll anyway.
     */
    if (getScrollX() == 0 && !canScrollHorizontally(1) && getScrollY() == 0 && !canScrollVertically(1)) {
        return false;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    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);
        if (pointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent");
            break;
        }

        final int x = (int) ev.getX(pointerIndex);
        final int y = (int) ev.getY(pointerIndex);
        final int xDiff = Math.abs(x - mLastMotionX);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (xDiff > mTouchSlop || yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionX = x;
            mLastMotionY = y;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (!inChild(x, y)) {
            mIsBeingDragged = false;
            recycleVelocityTracker();
            break;
        }

        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = x;
        mLastMotionY = y;
        mActivePointerId = ev.getPointerId(0);

        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);

        /*
        * If being flinged and user touches the screen, initiate drag;
        * otherwise don't.  mScroller.isFinished should be false when
        * being flinged.
        */
        mIsBeingDragged = !mScroller.isFinished();
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                getVerticalScrollRange())) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = ev.getActionIndex();
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = ev.findPointerIndex(mActivePointerId);
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        break;
    }

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