Example usage for android.view MotionEvent ACTION_UP

List of usage examples for android.view MotionEvent ACTION_UP

Introduction

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

Prototype

int ACTION_UP

To view the source code for android.view MotionEvent ACTION_UP.

Click Source Link

Document

Constant for #getActionMasked : A pressed gesture has finished, the motion contains the final release location as well as any intermediate points since the last down or move event.

Usage

From source file:com.aliasapps.seq.scroller.TwoWayView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!mIsAttached) {
        return false;
    }/*  w  w w.jav  a2 s.  c  om*/

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);

        mScroller.abortAnimation();

        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchPos = (mIsVertical ? y : x);

        final int motionPosition = findMotionRowOrColumn((int) mLastTouchPos);

        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mTouchRemainderPos = 0;

        if (mTouchMode == TOUCH_MODE_FLINGING) {
            return true;
        } else if (motionPosition >= 0) {
            mMotionPosition = motionPosition;
            mTouchMode = TOUCH_MODE_DOWN;
        }

        break;

    case MotionEvent.ACTION_MOVE: {
        if (mTouchMode != TOUCH_MODE_DOWN) {
            break;
        }

        initVelocityTrackerIfNotExists();
        mVelocityTracker.addMovement(ev);

        final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        if (index < 0) {
            Log.e(LOGTAG, "onInterceptTouchEvent could not find pointer with id " + mActivePointerId
                    + " - did TwoWayView receive an inconsistent " + "event stream?");
            return false;
        }

        final float pos;
        if (mIsVertical) {
            pos = MotionEventCompat.getY(ev, index);
        } else {
            pos = MotionEventCompat.getX(ev, index);
        }

        final float diff = pos - mLastTouchPos + mTouchRemainderPos;
        final int delta = (int) diff;
        mTouchRemainderPos = diff - delta;

        if (maybeStartScrolling(delta)) {
            return true;
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        mActivePointerId = INVALID_POINTER;
        mTouchMode = TOUCH_MODE_REST;
        recycleVelocityTracker();
        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);

        break;
    }

    return false;
}

From source file:com.example.aaron.test.MyGLSurfaceView.java

@Override
public boolean onTouchEvent(MotionEvent e) {

    float x = e.getX();
    float y = e.getY();

    int cc = 0;//from w  w w . j a  va  2s. c  o m

    /*
            float xGL=(width1/2-x)/(float)(height1/1.85);
            float yGL=( height1/2+30-y)/(float)(height1/1.85);
    */

    float xGL = (width1 / 2 - x) / (float) (height1 / 2);
    float yGL = (height1 / 2 - y) / (float) (height1 / 2);

    workspace = -(width1 - 115) / (height1 * 2) + .15f - mRenderer.slider;

    mRenderer.tempFun(xGL, yGL);

    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mRenderer.tToggle = 1;

        for (int i = 0; i < maxBots; i++) {
            if (tList[i].getOn() == 1) {
                cc = cc + 1;
                if (Math.abs(tList[i].getX() * getScale() + mRenderer.dragX * mRenderer.scale - xGL) < .1f
                        && Math.abs(
                                tList[i].getY() * getScale() + mRenderer.dragY * mRenderer.scale - yGL) < .1f) {
                    if (state[i] == 0) {
                        state[i] = 1;
                    } else {
                        state[i] = 0;
                    }
                    v.vibrate(50);
                }
            }
        }

        if (antispam > 1) {

            if (xGL < .85 && xGL > .65 && yGL > -.95 && yGL < -.75) {
                mRenderer.scale = mRenderer.scale + .5f;
                mRenderer.textList.get(1).setText("Scale: " + truncateDecimal(mRenderer.scale, 1) + "x");
                mRenderer.textList.get(2).setText(truncateDecimal(2 / mRenderer.scale, 2) + " ft");
                v.vibrate(75);
                break;
            }
            if (xGL < 1.2f && xGL > .95f && yGL > -.85f && yGL < -.75 && mRenderer.scale > .5f) {
                mRenderer.scale = mRenderer.scale - .5f;
                mRenderer.textList.get(1).setText("Scale: " + truncateDecimal(mRenderer.scale, 1) + "x");
                mRenderer.textList.get(2).setText(truncateDecimal(2 / mRenderer.scale, 2) + " ft");
                v.vibrate(75);
                break;
            }

            if (mRenderer.commit.blocked == false && xGL < mRenderer.commit.left - mRenderer.slider
                    && xGL > mRenderer.commit.right - mRenderer.slider && yGL > mRenderer.commit.down
                    && yGL < mRenderer.commit.up)
                if (mRenderer.commit.active == true) {
                    mRenderer.commit.active = false;
                    newAction = true;
                    v.vibrate(50);
                } else {
                    mRenderer.commit.active = true;
                    if (mRenderer.customFormation.active) {
                        mRenderer.justifyRelativePositions();
                        mRenderer.resetScaleAndRotation();
                        mRenderer.formation.send = true;
                    }
                    newAction = true;
                    v.vibrate(50);
                }

            //Turn on voronoi toggle
            if (buttonPush(xGL, yGL, mRenderer.vorToggle))
                if (mRenderer.getvToggle() == 1) {
                    mRenderer.setvToggle(0);
                    v.vibrate(500);
                } else {
                    mRenderer.setvToggle(1);
                    v.vibrate(500);
                }
            vFlag = mRenderer.getvToggle();

            //Turn on Free Draw Toggle
            if (buttonPush(xGL, yGL, mRenderer.freeDrawToggle))
                if (mRenderer.getfToggle() == 1) {
                    mRenderer.setfToggle(0);
                    fFlag = mRenderer.getfToggle();
                } else {
                    mRenderer.setfToggle(1);
                    mRenderer.eraseFreeLine();
                    freeDrawCount = 0;
                    previousy = 0;
                    previousx = 0;
                    connectable = 0;
                    firstPointFreeDraw[0] = -1000;
                    firstPointFreeDraw[1] = -1000;
                    fFlag = mRenderer.getfToggle();
                }

            //Toggable swarm for Path Follower
            /*if (mRenderer.getfToggle()==1 && xGL<mRenderer.swarmToggle.left- mRenderer.slider&& xGL>mRenderer.swarmToggle.right-mRenderer.slider&& yGL > mRenderer.swarmToggle.down && yGL < mRenderer.swarmToggle.up)
                if (mRenderer.swarmToggle.active == true) {
                    mRenderer.swarmToggle.active =  false;
                    v.vibrate(50);
                } else {
                    mRenderer.swarmToggle.active =  true;
                    v.vibrate(50);
                }
            */ //THIS FEATURE CAN BE ADDED LATER IF WE WANT

            //Turn on Way Point Toggle
            if (buttonPush(xGL, yGL, mRenderer.wayPointToggle))
                if (mRenderer.getpToggle() == 1) {
                    mRenderer.setpToggle(0);
                    pFlag2 = 0;
                    mRenderer.setpToggle2(pFlag2);
                    mRenderer.tToggle = 1;
                    pX = 0;
                    pY = 0;
                } else {
                    mRenderer.tToggle = 0;
                    mRenderer.setpToggle(1);
                    mRenderer.voronoiDeploymentToggle.active = false;
                    pX = 0;
                    pY = 0;
                }
            pFlag = mRenderer.getpToggle();

            //Turn on AndroneToggle
            if (buttonPush(xGL, yGL, mRenderer.ardronePrefToggle))
                if (mRenderer.getAPToggle() == 1) {
                    mRenderer.setAPToggle(0);
                    mRenderer.SINFO_FLAG = true;
                } else {
                    mRenderer.setAPToggle(1);
                    mRenderer.SINFO_FLAG = false;
                }

            if (buttonPush(xGL, yGL, mRenderer.gaussianTrackToggle)) {
                if (mRenderer.gaussianTrackToggle.active == true) {
                    mRenderer.gaussianTrackToggle.active = false;

                } else {
                    mRenderer.gaussianTrackToggle.active = true;

                }
                v.vibrate(50);
            }

            if (buttonPush(xGL, yGL, mRenderer.formation)) {
                if (mRenderer.formation.active == true) {
                    mRenderer.formation.active = false;
                    mRenderer.formation.send = false;
                } else {
                    mRenderer.formation.active = true;
                    mRenderer.formation.send = true;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.formationToggleTransform)) {
                if (mRenderer.formationToggleTransform.active == true) {
                    mRenderer.formationToggleTransform.active = false;
                } else {
                    mRenderer.formationToggleTransform.active = true;
                    mRenderer.customFormation.active = false;
                    mRenderer.commit.blocked = true;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.customFormation)) {
                if (mRenderer.customFormation.active == true) {
                    mRenderer.customFormation.active = false;
                    mRenderer.commit.blocked = true;
                } else {
                    mRenderer.customFormation.active = true;
                    mRenderer.resetFormation();
                    mRenderer.refreshCenter();
                    mRenderer.formationToggleTransform.active = false;
                    mRenderer.commit.blocked = false;
                    mRenderer.commit.active = false;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.ardroneAddToggle)) {
                dummyFlag = 1;
            }

            if (buttonPush(xGL, yGL, mRenderer.addToggle)) {
                mRenderer.formation.val1 = mRenderer.formation.val1 + 1;
                mRenderer.refreshFormationtext();
                mRenderer.formation.send = true;
            }

            if (buttonPush(xGL, yGL, mRenderer.subtractToggle)) {
                mRenderer.formation.val1 = mRenderer.formation.val1 - 1;
                mRenderer.refreshFormationtext();
                mRenderer.formation.send = true;
            }

            if (buttonPush(xGL, yGL, mRenderer.freeBoundarytoggle)) {
                if (mRenderer.freeBoundarytoggle.active == true) {
                    mRenderer.freeBoundarytoggle.active = false;
                    freeBoundaryFlag = true;
                    closedBoundaryFlag = false;
                } else {
                    mRenderer.freeBoundarytoggle.active = true;
                    closedBoundaryFlag = true;
                    freeBoundaryFlag = false;
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.obstacleLineToggle)) {
                System.out.println("obstacle Line Pushed");

                addObstacleFlag = true;
                mRenderer.makeObstacle();
                mRenderer.fToggle = 0;
                fFlag = mRenderer.fToggle;
            }

            //IF GAUSS TOGGLE SELECTED
            if (buttonPush(xGL, yGL, mRenderer.gaussToggle))
                if (mRenderer.getgToggle() == 1) {
                    mRenderer.setgToggle(0);
                    gFlag2 = 0;
                    mRenderer.setgToggle2(gFlag2);
                } else {
                    mRenderer.setgToggle(1);
                }
            gFlag = mRenderer.getgToggle();

            //IF GAUSSPATH TOGGLE SELECTED
            /*if (xGL<mRenderer.temptoggle.left- mRenderer.slider&& xGL>mRenderer.temptoggle.right-mRenderer.slider&& yGL > mRenderer.temptoggle.down && yGL < mRenderer.temptoggle.up)
                if (mRenderer.getgpToggle() == 1) {
                    mRenderer.setgpToggle(0);
                    mRenderer.eraseGaussLine();
                    v.vibrate(50);
                } else {
                    mRenderer.setgpToggle(1);
                    mRenderer.makeGaussPoints();
                    v.vibrate(50);
                }*/
            gpFlag = mRenderer.getgpToggle();

            //Toggle for voronoi deployment
            if (buttonPush(xGL, yGL, mRenderer.voronoiDeploymentToggle))
                if (mRenderer.voronoiDeploymentToggle.active == true) {
                    mRenderer.voronoiDeploymentToggle.active = false;
                    newAction = true;
                } else {
                    newAction = true;
                    mRenderer.voronoiDeploymentToggle.active = true;
                    mRenderer.setpToggle(0);
                    mRenderer.setpToggle2(0);
                }

            if (buttonPush(xGL, yGL, mRenderer.dragToggle)) {
                addedTurtleNumber = 0;
                addedTurtleX = .1f;
                addedTurtleY = .1f;
                for (int j = 41; j < 44; j++) {
                    if (tList[j].getOn() != 1) {
                        addedTurtleNumber = j;
                    }
                }
                if (addedTurtleNumber == 0) {
                    addTurtleFlag = false;
                } else {
                    addTurtleFlag = true;

                }
            }

            if (buttonPush(xGL, yGL, mRenderer.addTurtle)) {
                addedTurtleNumber = 0;
                addedTurtleX = .1f;
                addedTurtleY = .1f;
                for (int j = 1; j < 11; j++) {
                    if (tList[j].getOn() != 1) {
                        addedTurtleNumber = j;
                    }
                }
                if (addedTurtleNumber == 0) {
                    addTurtleFlag = false;
                } else {
                    addTurtleFlag = true;

                }
            }

            if (buttonPush(xGL, yGL, mRenderer.dragRobotToggle)) {
                if (mRenderer.dragRobotToggle.active == false) {
                    mRenderer.dragRobotToggle.active = true;
                } else {
                    mRenderer.dragRobotToggle.active = false;
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.gaussianPathToggle)) {
                if (mRenderer.gaussianPathToggle.active == true) {
                    mRenderer.gaussianPathToggle.active = false;
                    mRenderer.gaussPathPlay.active = false;
                } else {
                    mRenderer.gaussianPathToggle.active = true;
                    if (mRenderer.getfToggle() == 1) {//
                        mRenderer.gaussPathPlay.active = true;
                        mRenderer.updateGauss(
                                mRenderer.pathArray.pose[0].x * mRenderer.scale
                                        + mRenderer.dragX * mRenderer.scale,
                                mRenderer.pathArray.pose[0].y * mRenderer.scale
                                        + mRenderer.dragY * mRenderer.scale,
                                0);
                        gaussianPathArrayIteration = 1;
                        deltaX = (mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x)
                                / (float) Math.sqrt(Math
                                        .pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x, 2)
                                        + Math.pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y,
                                                2));
                        deltaY = (mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y)
                                / (float) Math.sqrt(Math
                                        .pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x, 2)
                                        + Math.pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y,
                                                2));

                    }
                }
            }

            if (xGL < mRenderer.clearAll.left - mRenderer.slider
                    && xGL > mRenderer.clearAll.right - mRenderer.slider && yGL > mRenderer.clearAll.down
                    && yGL < mRenderer.clearAll.up) {
                if (mRenderer.clearAll.active == true) {
                    mRenderer.clearAll.active = false;
                    v.vibrate(50);
                } else {
                    mRenderer.clearAll.active = true;
                    v.vibrate(50);
                }
            }

            if (xGL < mRenderer.resetToggle.left - mRenderer.slider
                    && xGL > mRenderer.resetToggle.right - mRenderer.slider && yGL > mRenderer.resetToggle.down
                    && yGL < mRenderer.resetToggle.up) {
                if (mRenderer.resetToggle.active == true) {
                    mRenderer.resetToggle.active = false;
                    v.vibrate(50);
                } else {
                    mRenderer.resetToggle.active = true;
                    v.vibrate(50);
                }
            }

            /*                    if (xGL<-(width1-90)/height1+.05f && xGL>-(width1-90)/height1 && yGL >-(height1-10)/(height1)-mRenderer.slider  && yGL < -(height1-10)/(height1)+05f-mRenderer.slider ){
                    
            }*/

            //Clear button
            if (xGL < mRenderer.clear.left - mRenderer.slider && xGL > mRenderer.clear.right - mRenderer.slider
                    && yGL > mRenderer.clear.down && yGL < mRenderer.clear.up) {
                if (mRenderer.getgToggle() == 1) {
                    gInd = 0;
                    mRenderer.clearGauss();

                }
                if (mRenderer.dragToggle.active == true) {
                    obsticle.on = 0;
                }

                deleteTurtleNumber = -1;
                for (int i = 0; i < maxBots; i++) {
                    if (tList[i].getState() == 1) {
                        deleteTurtleNumber = i;
                    }
                }
                if (deleteTurtleNumber != -1) {
                    deleteTurtleFlag = true;
                } else {
                    deleteTurtleFlag = false;
                }

                v.vibrate(75);
                mRenderer.clear.active = true;

            }

        }

        if (pFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL - mRenderer.dragX * mRenderer.scale;
            pY = yGL - mRenderer.dragY * mRenderer.scale;
            pFlag2 = 1;
            mRenderer.setpToggle2(pFlag2);
            mRenderer.setWayPointValues(pX, pY);
        }

        if (gFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            //pX=xGL;
            //pY=yGL;
            gFlag2 = 1;
            mRenderer.setgToggle2(gFlag2);

            //System.out.println("GAUSSIAN INDEX: " +gInd);
            //mRenderer.setWayPointValues(xGL, yGL);
            //mRenderer.setGaussValues(xGL, yGL, gInd);
            //mRenderer.setGaussScale(1f);
            if (gInd < 99) {
                //mRenderer.addGaussStuff(xGL, yGL, 1f,gInd);
                //TEMP FIX
                mRenderer.addGaussStuff(xGL, yGL, 1f, 0);
            }
            gInd++;
        }

        if (mRenderer.gToggle == 0 && mRenderer.fToggle == 0 && mRenderer.directionalDrag.active == true
                && (mRenderer.directionalDrag.active == true
                        && xGL < mRenderer.directionalDrag.left - mRenderer.slider
                        && xGL > mRenderer.directionalDrag.right - mRenderer.slider
                        && yGL > mRenderer.directionalDrag.down && yGL < mRenderer.directionalDrag.up)) {
            v.vibrate(50);
            if (xGL > mRenderer.directionalDrag.left
                    + (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX - .01f;
            }
            if (xGL < mRenderer.directionalDrag.left
                    + 2 * (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX + .01f;
            }

            if (yGL > mRenderer.directionalDrag.up
                    + (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY - .01f;
            }
            if (yGL < mRenderer.directionalDrag.up
                    + 2 * (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY + .01f;
            }
        }

    case MotionEvent.ACTION_POINTER_DOWN:
        //System.out.println("GAUSS 0");
        if (e.getActionIndex() == 1) {

            if (gFlag == 1) {
                float gaussX = e.getX(1);

                float gaussY = e.getY(1);

                float gauss_xGL = (width1 / 2 - gaussX) / (float) (height1 / 1.85);
                float gauss_yGL = (height1 / 2 + 30 - gaussY) / (float) (height1 / 1.85);

                float gauss_dx = gauss_xGL - xGL;
                float gauss_dy = gauss_yGL - yGL;

                float dgauss = (float) Math.sqrt(Math.pow(gauss_dx, 2) + Math.pow(gauss_dy, 2));

                gaussScale = dgauss / .2f;
                //System.out.println("SCALE");
                //mRenderer.addGaussStuff(xGL, yGL, gaussScale,gInd-1);
                //mRenderer.setGaussScale(gInd-1, gaussScale);
                //TEMP FIX
                mRenderer.setGaussScale(0, gaussScale);
            }
        }

    case MotionEvent.ACTION_MOVE:

        mRenderer.tToggle = 1;
        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        if (mRenderer.dragRobotToggle.active) {
            for (int i = 0; i < maxBots; i++) {
                if (tList[i].getOn() == 1) {
                    if (Math.abs(tList[i].getX() * getScale() + mRenderer.dragX * mRenderer.scale - xGL) < .1f
                            && Math.abs(tList[i].getY() * getScale() + mRenderer.dragY * mRenderer.scale
                                    - yGL) < .1f) {
                        addedTurtleNumber = i;
                        addedTurtleX = xGL / getScale() - mRenderer.dragX;
                        addedTurtleY = yGL / getScale() - mRenderer.dragY;
                        addTurtleFlag = true;
                        v.vibrate(50);
                    }
                }
            }

        }
        //pX+(float)Math.cos(formation.val4)*.3f+dragX*scale, pY+(float)Math.sin(formation.val4)*.3f+dragY*scale, 0);
        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(mRenderer.formation.x * mRenderer.scale
                    + (float) Math.cos(mRenderer.formation.val4) * .3f * mRenderer.scale
                    + mRenderer.dragX * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale
                            + (float) Math.sin(mRenderer.formation.val4) * .3f * mRenderer.scale
                            + mRenderer.dragY * mRenderer.scale - yGL) < .15f) {
                mRenderer.formation.val4 = (float) Math.atan2(
                        yGL - (mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale),
                        xGL - (mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale));
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.rotation_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.rotation_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(
                    mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale
                            - yGL) < .15f) {
                pX = xGL - mRenderer.scale * mRenderer.dragX;
                pY = yGL - mRenderer.scale * mRenderer.dragY;
                mRenderer.setWayPointValues(pX, pY);
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.center_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.center_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale
                    + mRenderer.formation.val3 * .2f * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale
                            - yGL) < .15f) {
                mRenderer.formation.val3 = (xGL
                        - (mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale)) / .2f
                        / mRenderer.scale;
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.scale_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.scale_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.customFormation.active) {
            for (int i = 0; i < mRenderer.formation.val1; i++) {
                if (target(convertToScreenPosition(mRenderer.formation_locations.x[i], true), xGL,
                        convertToScreenPosition(mRenderer.formation_locations.y[i], false), yGL, .15f)) {
                    //mRenderer.formation.x=mRenderer.formation.x*mRenderer.formation.val1-mRenderer.formation_locations.x[i];
                    //mRenderer.formation.y=mRenderer.formation.y*mRenderer.formation.val1-mRenderer.formation_locations.y[i];

                    mRenderer.formation_locations.x[i] = convertToGlobalPosition(xGL, true);
                    mRenderer.formation_locations.y[i] = convertToGlobalPosition(yGL, false);

                    //mRenderer.formation.x=mRenderer.formation.x+mRenderer.formation_locations.x[i];
                    //mRenderer.formation.x=mRenderer.formation.x/mRenderer.formation.val1;

                    //mRenderer.formation.y=mRenderer.formation.y+mRenderer.formation_locations.y[i];
                    //mRenderer.formation.y=mRenderer.formation.y/mRenderer.formation.val1;
                    mRenderer.refreshCenter();
                    break;
                }
            }
        }

        if (mRenderer.dragToggle.active == true && xGL > workspace && xGL < mapLeft && yGL < mapTop
                && yGL > mapBottom) {
            obsticle.x = xGL / getScale() - mRenderer.dragX;
            obsticle.y = yGL / getScale() - mRenderer.dragY;
            obsticle.Aw = 1;
            obsticle.on = 1;
            newObstacle = true;
        }

        if (mRenderer.gToggle == 0 && mRenderer.fToggle == 0 && mRenderer.directionalDrag.active == true
                && (mRenderer.directionalDrag.active == true
                        && xGL < mRenderer.directionalDrag.left - mRenderer.slider
                        && xGL > mRenderer.directionalDrag.right - mRenderer.slider
                        && yGL > mRenderer.directionalDrag.down && yGL < mRenderer.directionalDrag.up)) {
            v.vibrate(50);
            if (xGL > mRenderer.directionalDrag.left
                    + (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX - .01f;
                System.out.println("LEFT");

            }
            if (xGL < mRenderer.directionalDrag.left
                    + 2 * (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX + .01f;
                System.out.println("RIGHT");
            }

            if (yGL > mRenderer.directionalDrag.up
                    + (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY - .01f;
                System.out.println("UP");
            }
            if (yGL < mRenderer.directionalDrag.up
                    + 2 * (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY + .01f;
                System.out.println("DOWN");
            }
        }

        if (fFlag == 1 && (Math.abs(xGL - previousx) > .03f || Math.abs(yGL - previousy) > .03f)
                && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            if (previousx != 0 && previousy != 0) {
                if (firstPointFreeDraw[0] == -1000) {
                    firstPointFreeDraw[0] = previousx;
                    firstPointFreeDraw[1] = previousy;
                } else if (xGL > firstPointFreeDraw[0] + .1f || xGL < firstPointFreeDraw[0] - .1f
                        || yGL > firstPointFreeDraw[1] + .1f || yGL < firstPointFreeDraw[1] - .1f) {
                    connectable = 1;
                } else if (connectable == 1) {
                    fFlag = 0;
                    setFreeDrawCoordinates(firstPointFreeDraw[0], firstPointFreeDraw[1], previousx, previousy,
                            true);
                    v.vibrate(50);
                }

                if (fFlag == 1) {
                    setFreeDrawCoordinates(xGL - mRenderer.dragX * mRenderer.scale,
                            yGL - mRenderer.dragY * mRenderer.scale,
                            previousx - mRenderer.dragX * mRenderer.scale,
                            previousy - mRenderer.dragY * mRenderer.scale, false);
                }

            }

            previousx = xGL;
            previousy = yGL;
        }

        // reverse direction of rotation above the mid-line
        if (y > getHeight() / 2) {
            dx = dx * -1;
        }

        // reverse direction of rotation to left of the mid-line
        if (x < getWidth() / 2) {
            dy = dy * -1;
        }

        if (yGL > -.15f && yGL < .15f && xGL > -(width1 - 115) / (height1 * 2) - mRenderer.slider
                && xGL < -(width1 - 115) / (height1 * 2) - mRenderer.slider + .15f) {
            mRenderer.slider = -(width1 - 115) / (height1 * 2) - xGL;
            if (mRenderer.slider < .1f) {
                mRenderer.slider = 0;
            } else if (mRenderer.slider > -.1f + (width1 - 115) / (height1 * 2)) {
                mRenderer.slider = (width1 - 115) / (height1 * 2);
            }
        }

        mRenderer.setAngle(mRenderer.getAngle() + ((dx + dy) * TOUCH_SCALE_FACTOR)); // = 180.0f / 320
        requestRender();

        if (pFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL - mRenderer.scale * mRenderer.dragX;
            pY = yGL - mRenderer.scale * mRenderer.dragY;
            pFlag2 = 1;
            mRenderer.setpToggle2(pFlag2);
            mRenderer.setWayPointValues(pX, pY);
        }

        //moving gauss
        if (gFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL;
            pY = yGL;
            gFlag2 = 1;
            mRenderer.setpToggle2(gFlag2);
            //TEMP FIX
            mRenderer.updateGauss(xGL, yGL, 0);
            //mRenderer.updateGauss(xGL, yGL, gInd-1);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar.right - .04f - mRenderer.slider && yGL > mRenderer.dial.down
                && yGL < mRenderer.dial.up) {
            mRenderer.dial1 = -(xGL - mRenderer.bar.left - mRenderer.slider) / .6f;
            if (mRenderer.dial1 < 0.05) {
                mRenderer.dial1 = 0;
            } else if (mRenderer.dial1 > 1) {
                mRenderer.dial1 = 1;
            } else if (mRenderer.dial1 < .15) {
                mRenderer.dial1 = .1f;
            } else if (mRenderer.dial1 < .25) {
                mRenderer.dial1 = .2f;
            } else if (mRenderer.dial1 < .35) {
                mRenderer.dial1 = .3f;
            } else if (mRenderer.dial1 < .45) {
                mRenderer.dial1 = .4f;
            } else if (mRenderer.dial1 < .55) {
                mRenderer.dial1 = .5f;
            } else if (mRenderer.dial1 < .65) {
                mRenderer.dial1 = .6f;
            } else if (mRenderer.dial1 < .75) {
                mRenderer.dial1 = .7f;
            } else if (mRenderer.dial1 < .85) {
                mRenderer.dial1 = .8f;
            } else if (mRenderer.dial1 < .95) {
                mRenderer.dial1 = .9f;
            } else {
                mRenderer.dial1 = 1;
            }
            mRenderer.textListGaussian.get(1).setText(" " + truncateDecimal(mRenderer.dial1, 1));
            mRenderer.dial.active = true;
            v.vibrate(50);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar_2.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar_2.right - .04f - mRenderer.slider && yGL > mRenderer.dial_2.down
                && yGL < mRenderer.dial_2.up) {
            mRenderer.dial2 = -(xGL - mRenderer.bar_2.left - mRenderer.slider) / .6f;
            if (mRenderer.dial2 < 0.05) {
                mRenderer.dial2 = 0;
            } else if (mRenderer.dial2 > 1) {
                mRenderer.dial2 = 1;
            } else if (mRenderer.dial2 < .15) {
                mRenderer.dial2 = .1f;
            } else if (mRenderer.dial2 < .25) {
                mRenderer.dial2 = .2f;
            } else if (mRenderer.dial2 < .35) {
                mRenderer.dial2 = .3f;
            } else if (mRenderer.dial2 < .45) {
                mRenderer.dial2 = .4f;
            } else if (mRenderer.dial2 < .55) {
                mRenderer.dial2 = .5f;
            } else if (mRenderer.dial2 < .65) {
                mRenderer.dial2 = .6f;
            } else if (mRenderer.dial2 < .75) {
                mRenderer.dial2 = .7f;
            } else if (mRenderer.dial2 < .85) {
                mRenderer.dial2 = .8f;
            } else if (mRenderer.dial2 < .95) {
                mRenderer.dial2 = .9f;
            } else {
                mRenderer.dial2 = 1;
            }
            mRenderer.textListGaussian.get(3).setText(" " + truncateDecimal(mRenderer.dial2, 1));
            mRenderer.dial_2.active = true;
            v.vibrate(50);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar_3.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar_3.right - .04f - mRenderer.slider && yGL > mRenderer.dial_3.down
                && yGL < mRenderer.dial_3.up) {
            mRenderer.dial3 = -(xGL - mRenderer.bar_3.left - mRenderer.slider) / .6f;
            if (mRenderer.dial3 < 0.05) {
                mRenderer.dial3 = 0;
            } else if (mRenderer.dial3 > 1) {
                mRenderer.dial3 = 1;
            } else if (mRenderer.dial3 < .15) {
                mRenderer.dial3 = .1f;
            } else if (mRenderer.dial3 < .25) {
                mRenderer.dial3 = .2f;
            } else if (mRenderer.dial3 < .35) {
                mRenderer.dial3 = .3f;
            } else if (mRenderer.dial3 < .45) {
                mRenderer.dial3 = .4f;
            } else if (mRenderer.dial3 < .55) {
                mRenderer.dial3 = .5f;
            } else if (mRenderer.dial3 < .65) {
                mRenderer.dial3 = .6f;
            } else if (mRenderer.dial3 < .75) {
                mRenderer.dial3 = .7f;
            } else if (mRenderer.dial3 < .85) {
                mRenderer.dial3 = .8f;
            } else if (mRenderer.dial3 < .95) {
                mRenderer.dial3 = .9f;
            } else {
                mRenderer.dial3 = 1;
            }
            mRenderer.textListGaussian.get(5).setText(" " + truncateDecimal(mRenderer.dial3, 1));
            mRenderer.dial_3.active = true;
            v.vibrate(50);
        }

    case MotionEvent.ACTION_UP:

        mRenderer.tToggle = 1;
        //mRenderer.clear.active = false;

    }

    mPreviousX = x;
    mPreviousY = y;

    antispam = 0;
    return true;
}

From source file:com.aidy.bottomdrawerlayout.AllDrawerLayout.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    Log.i(TAG, "onTouchEvent()");
    final int action = ev.getAction();
    boolean wantTouchEvents = true;
    try {//from  w  ww.  ja  v  a  2s .  co m

        mLeftDragger.processTouchEvent(ev);
        mRightDragger.processTouchEvent(ev);
        mTopDragger.processTouchEvent(ev);
        mBottomDragger.processTouchEvent(ev);

        switch (action & MotionEventCompat.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            Log.i(TAG, "onTouchEvent() -- ACTION_DOWN");
            final float x = ev.getX();
            final float y = ev.getY();
            mInitialMotionX = x;
            mInitialMotionY = y;
            mDisallowInterceptRequested = false;
            mChildrenCanceledTouch = false;
            break;
        }

        case MotionEvent.ACTION_UP: {
            Log.i(TAG, "onTouchEvent() -- ACTION_UP");
            final float x = ev.getX();
            final float y = ev.getY();
            boolean peekingOnly = true;
            final View touchedView = mLeftDragger.findTopChildUnder((int) x, (int) y);
            if (touchedView != null && isContentView(touchedView)) {
                final float dx = x - mInitialMotionX;
                final float dy = y - mInitialMotionY;
                final int slop = mLeftDragger.getTouchSlop();
                if (dx * dx + dy * dy < slop * slop) {
                    // Taps close a dimmed open drawer but only if it isn't
                    // locked open.
                    final View openDrawer = findOpenDrawer();
                    if (openDrawer != null) {
                        peekingOnly = getDrawerLockMode(openDrawer) == LOCK_MODE_LOCKED_OPEN;
                    }
                }
            }
            closeDrawers(peekingOnly);
            mDisallowInterceptRequested = false;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            Log.i(TAG, "onTouchEvent() -- ACTION_CANCEL");
            closeDrawers(true);
            mDisallowInterceptRequested = false;
            mChildrenCanceledTouch = false;
            break;
        }
        }
    } catch (IllegalArgumentException e) {
        // TODO: handle exception
    }
    boolean result = wantTouchEvents;
    Log.i(TAG, "onTouchEvent() -- result = " + result);
    return result;
}

From source file:bw.com.yunifangstore.view.LazyViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (mFakeDragging) {
        // A fake drag is in progress already, ignore this real one
        // but still eat the touch events.
        // (It is likely that the user is multi-touching the screen.)
        return true;
    }//from ww w  . j  a  v a 2 s  . co m

    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }

    if (mAdapter == null || mAdapter.getCount() == 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

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

    final int action = ev.getAction();
    boolean needsInvalidate = false;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        completeScroll();

        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (!mIsBeingDragged) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastMotionX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (DEBUG)
                Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                if (DEBUG)
                    Log.v(TAG, "Starting drag!");
                mIsBeingDragged = true;
                mLastMotionX = x;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = mLastMotionX - x;
            mLastMotionX = x;
            float oldScrollX = getScrollX();
            float scrollX = oldScrollX + deltaX;
            final int width = getWidth();
            final int widthWithMargin = width + mPageMargin;

            final int lastItemIndex = mAdapter.getCount() - 1;
            final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
            final float rightBound = Math.min(mCurItem + 1, lastItemIndex) * widthWithMargin;
            if (scrollX < leftBound) {
                if (leftBound == 0) {
                    float over = -scrollX;
                    needsInvalidate = mLeftEdge.onPull(over / width);
                }
                scrollX = leftBound;
            } else if (scrollX > rightBound) {
                if (rightBound == lastItemIndex * widthWithMargin) {
                    float over = scrollX - rightBound;
                    needsInvalidate = mRightEdge.onPull(over / width);
                }
                scrollX = rightBound;
            }
            // Don't lose the rounded component
            mLastMotionX += scrollX - (int) scrollX;
            scrollTo((int) scrollX, getScrollY());
            if (mOnPageChangeListener != null) {
                final int position = (int) scrollX / widthWithMargin;
                final int positionOffsetPixels = (int) scrollX % widthWithMargin;
                final float positionOffset = (float) positionOffsetPixels / widthWithMargin;
                mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            mPopulatePending = true;
            final int widthWithMargin = getWidth() + mPageMargin;
            final int scrollX = getScrollX();
            final int currentPage = scrollX / widthWithMargin;
            int nextPage = initialVelocity > 0 ? currentPage : currentPage + 1;
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged) {
            setCurrentItemInternal(mCurItem, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
            needsInvalidate = mLeftEdge.onRelease() | mRightEdge.onRelease();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        invalidate();
    }
    return false;
}

From source file:com.android.launcher2.PagedView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onTouchEvent(ev);

    acquireVelocityTrackerAndAddMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        /*/*www  .ja v  a2 s . co  m*/
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mDownMotionX = mLastMotionX = ev.getX();
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            pageBeginMoving();
        }
        break;

    case MotionEvent.ACTION_MOVE:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float deltaX = mLastMotionX + mLastMotionXRemainder - x;

            mTotalMotionX += Math.abs(deltaX);

            // Only scroll and update mLastMotionX if we have moved some discrete amount.  We
            // keep the remainder because we are actually testing if we've moved from the last
            // scrolled position (which is discrete).
            if (Math.abs(deltaX) >= 1.0f) {
                mTouchX += deltaX;
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
                if (!mDeferScrollUpdate) {
                    scrollBy((int) deltaX, 0);
                    if (DEBUG)
                        Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
                } else {
                    invalidate();
                }
                mLastMotionX = x;
                mLastMotionXRemainder = deltaX - (int) deltaX;
            } else {
                awakenScrollBars();
            }
        } else {
            determineScrollingStart(ev);
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final int activePointerId = mActivePointerId;
            final int pointerIndex = ev.findPointerIndex(activePointerId);
            final float x = ev.getX(pointerIndex);
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
            final int deltaX = (int) (x - mDownMotionX);
            final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
            boolean isSignificantMove = Math.abs(deltaX) > pageWidth * SIGNIFICANT_MOVE_THRESHOLD;

            mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);

            boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING
                    && Math.abs(velocityX) > mFlingThresholdVelocity;

            // In the case that the page is moved far to one direction and then is flung
            // in the opposite direction, we use a threshold to determine whether we should
            // just return to the starting page, or if we should skip one further.
            boolean returnToOriginalPage = false;
            if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD
                    && Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
                returnToOriginalPage = true;
            }

            int finalPage;
            // We give flings precedence over large moves, which is why we short-circuit our
            // test for a large move if a fling has been registered. That is, a large
            // move to the left and fling to the right will register as a fling to the right.
            if (((isSignificantMove && deltaX > 0 && !isFling) || (isFling && velocityX > 0))
                    && mCurrentPage > 0) {
                finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
                snapToPageWithVelocity(finalPage, velocityX);
            } else if (((isSignificantMove && deltaX < 0 && !isFling) || (isFling && velocityX < 0))
                    && mCurrentPage < getChildCount() - 1) {
                finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
                snapToPageWithVelocity(finalPage, velocityX);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
            // at this point we have not moved beyond the touch slop
            // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
            // we can just page
            int nextPage = Math.max(0, mCurrentPage - 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
            // at this point we have not moved beyond the touch slop
            // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
            // we can just page
            int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else {
            onUnhandledTap(ev);
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;

    case MotionEvent.ACTION_CANCEL:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            snapToDestination();
        }
        mTouchState = TOUCH_STATE_REST;
        mActivePointerId = INVALID_POINTER;
        releaseVelocityTracker();
        break;

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

    return true;
}

From source file:com.appsummary.luoxf.mylibrary.swipbackhelper.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will
 * dispatch callback events as needed before returning. The parent view's
 * onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *///from w  w w.j  a  v a 2  s. co m
public void processTouchEvent(MotionEvent ev) {

    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);

        final View toCapture = findTopChildUnder((int) x, (int) y);

        // Catch a settling view if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        if (mDragState == STATE_SETTLING) {
            setDragState(STATE_DRAGGING);
        } else if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgeTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
            setDragState(STATE_JUDGING);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_JUDGING) {

            final int i = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[mActivePointerId];
            final float dy = y - mInitialMotionY[mActivePointerId];

            reportNewEdgeDrags(dx, dy, mActivePointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag.
                break;
            }

            final View toCapture = findTopChildUnder((int) x, (int) y);
            int slop = checkTouchSlop(toCapture, dx, dy);
            if (slop == -1)
                cancel();
            else if (slop > 0 && tryCaptureViewForDrag(toCapture, mActivePointerId)) {
                break;
            }
            saveLastMotion(ev);
        }

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

            final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            //????????
            if (index == -1)
                break;

            final float x = MotionEventCompat.getX(ev, index);
            final float y = MotionEventCompat.getY(ev, index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);
            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        releaseViewForPointerUp();
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        dispatchViewReleased(0, 0);
        cancel();
        break;
    }
    }
}

From source file:com.androzic.MapFragment.java

@SuppressLint("ClickableViewAccessibility")
@Override/*from ww  w.  j  ava 2 s  . c  om*/
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        zoom100X = (int) event.getRawX();
        zoom100Y = (int) event.getRawY();
        zoomHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                zoom100X = 0;
                zoom100Y = 0;
            }
        }, 2000);
        break;
    case MotionEvent.ACTION_UP:
        int dx = Math.abs((int) event.getRawX() - zoom100X);
        int dy = (int) event.getRawY() - zoom100Y;
        int h = v.getHeight();
        int w = v.getWidth() >> 1;
        if (dy > h * 0.8 && dy < h * 2 && dx < w) {
            wait(new Waitable() {
                @Override
                public void waitFor() {
                    synchronized (map) {
                        if (application.setZoom(1.)) {
                            map.updateMapInfo();
                            map.updateMapCenter();
                        }
                    }
                }
            });
            zoom100X = 0;
            zoom100Y = 0;
        }
        break;
    }
    return false;
}

From source file:com.albedinsky.android.support.ui.widget.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will dispatch callback events
 * as needed before returning. The parent view's onTouchEvent implementation should call this.
 *
 * @param ev The touch event received by the parent view
 *//*from w w  w .j  a va 2s .  com*/
public void processTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal down event.

            final View toCapture = findTopChildUnder((int) x, (int) y);
            tryCaptureViewForDrag(toCapture, pointerId);

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is under this
            // point, we'll swap to controlling it with this pointer instead.
            // (This will still work if we're "catching" a settling view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, index);
            final float y = MotionEventCompat.getY(ev, index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = (int) (y - mLastMotionY[mActivePointerId]);

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            final int pointerCount = MotionEventCompat.getPointerCount(ev);
            for (int i = 0; i < pointerCount; i++) {
                final int pointerId = MotionEventCompat.getPointerId(ev, i);
                final float x = MotionEventCompat.getX(ev, i);
                final float y = MotionEventCompat.getY(ev, i);
                final float dx = x - mInitialMotionX[pointerId];
                final float dy = y - mInitialMotionY[pointerId];

                reportNewEdgeDrags(dx, dy, pointerId);
                if (mDragState == STATE_DRAGGING) {
                    // Callback might have started an edge drag.
                    break;
                }

                final View toCapture = findTopChildUnder((int) x, (int) y);
                if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = MotionEventCompat.getPointerCount(ev);
            for (int i = 0; i < pointerCount; i++) {
                final int id = MotionEventCompat.getPointerId(ev, i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = MotionEventCompat.getX(ev, i);
                final float y = MotionEventCompat.getY(ev, i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the view, release it.
                releaseViewForPointerUp();
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp();
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}

From source file:com.android.launcher3.Workspace.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mXDown = ev.getX();/*from w  w w .  j  av  a  2s.c  om*/
        mYDown = ev.getY();
        mTouchDownTime = System.currentTimeMillis();
        break;
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_REST) {
            final CellLayout currentPage = (CellLayout) getChildAt(mCurrentPage);
            if (currentPage != null && !currentPage.lastDownOnOccupiedCell()) {
                onWallpaperTap(ev);
            }
        }
    }
    return super.onInterceptTouchEvent(ev);
}

From source file:cn.edu.bjtu.svnteen.nourriture.uilib.ViewDragHelper.java

/**
 * Process a touch event received by the parent view. This method will
 * dispatch callback events as needed before returning. The parent view's
 * onTouchEvent implementation should call this.
 * /*w  w  w .j  a v  a 2  s  .co  m*/
 * @param ev The touch event received by the parent view
 */
public void processTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);

    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }

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

    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        final View toCapture = findTopChildUnder((int) x, (int) y);

        saveInitialMotion(x, y, pointerId);

        // Since the parent is already directly processing this touch
        // event,
        // there is no reason to delay for a slop before dragging.
        // Start immediately if possible.
        tryCaptureViewForDrag(toCapture, pointerId);

        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);

        saveInitialMotion(x, y, pointerId);

        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            // If we're idle we can do anything! Treat it like a normal
            // down event.

            final View toCapture = findTopChildUnder((int) x, (int) y);
            tryCaptureViewForDrag(toCapture, pointerId);

            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (isCapturedViewUnder((int) x, (int) y)) {
            // We're still tracking a captured view. If the same view is
            // under this
            // point, we'll swap to controlling it with this pointer
            // instead.
            // (This will still work if we're "catching" a settling
            // view.)

            tryCaptureViewForDrag(mCapturedView, pointerId);
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (mDragState == STATE_DRAGGING) {
            final int index = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, index);
            final int idx = (int) (x - mLastMotionX[mActivePointerId]);
            final int idy = 0;

            dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);

            saveLastMotion(ev);
        } else {
            // Check to see if any pointer is now over a draggable view.
            final int pointerCount = MotionEventCompat.getPointerCount(ev);
            for (int i = 0; i < pointerCount; i++) {
                final int pointerId = MotionEventCompat.getPointerId(ev, i);
                final float x = MotionEventCompat.getX(ev, i);
                final float y = MotionEventCompat.getY(ev, i);
                final float dx = x - mInitialMotionX[pointerId];
                //final float dy = y - mInitialMotionY[pointerId];
                final int dy = 0; // ???bug????                        // ??????   by huqian

                reportNewEdgeDrags(dx, dy, pointerId);
                if (mDragState == STATE_DRAGGING) {
                    // Callback might have started an edge drag.
                    break;
                }

                final View toCapture = findTopChildUnder((int) x, (int) y);
                if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) {
                    break;
                }
            }
            saveLastMotion(ev);
        }
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) {
            // Try to find another pointer that's still holding on to
            // the captured view.
            int newActivePointer = INVALID_POINTER;
            final int pointerCount = MotionEventCompat.getPointerCount(ev);
            for (int i = 0; i < pointerCount; i++) {
                final int id = MotionEventCompat.getPointerId(ev, i);
                if (id == mActivePointerId) {
                    // This one's going away, skip.
                    continue;
                }

                final float x = MotionEventCompat.getX(ev, i);
                final float y = MotionEventCompat.getY(ev, i);
                if (findTopChildUnder((int) x, (int) y) == mCapturedView
                        && tryCaptureViewForDrag(mCapturedView, id)) {
                    newActivePointer = mActivePointerId;
                    break;
                }
            }

            if (newActivePointer == INVALID_POINTER) {
                // We didn't find another pointer still touching the
                // view, release it.
                releaseViewForPointerUp(ev);
            }
        }
        clearMotionHistory(pointerId);
        break;
    }

    case MotionEvent.ACTION_UP: {
        if (mDragState == STATE_DRAGGING) {
            releaseViewForPointerUp(ev);
        }
        cancel();
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        if (mDragState == STATE_DRAGGING) {
            dispatchViewReleased(0, 0);
        }
        cancel();
        break;
    }
    }
}