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:android.support.v7.widget.FastScroller.java

@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent me) {
    if (mState == STATE_HIDDEN) {
        return;/* w w  w .  jav  a2  s  .c o m*/
    }

    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        boolean insideVerticalThumb = isPointInsideVerticalThumb(me.getX(), me.getY());
        boolean insideHorizontalThumb = isPointInsideHorizontalThumb(me.getX(), me.getY());
        if (insideVerticalThumb || insideHorizontalThumb) {
            if (insideHorizontalThumb) {
                mDragState = DRAG_X;
                mHorizontalDragX = (int) me.getX();
            } else if (insideVerticalThumb) {
                mDragState = DRAG_Y;
                mVerticalDragY = (int) me.getY();
            }
            setState(STATE_DRAGGING);
        }
    } else if (me.getAction() == MotionEvent.ACTION_UP && mState == STATE_DRAGGING) {
        mVerticalDragY = 0;
        mHorizontalDragX = 0;
        setState(STATE_VISIBLE);
        mDragState = DRAG_NONE;
    } else if (me.getAction() == MotionEvent.ACTION_MOVE && mState == STATE_DRAGGING) {
        show();
        if (mDragState == DRAG_X) {
            horizontalScrollTo(me.getX());
        }
        if (mDragState == DRAG_Y) {
            verticalScrollTo(me.getY());
        }
    }
}

From source file:com.microsoft.windowsazure.mobileservices.authentication.LoginManager.java

/**
 * Creates the UI for the interactive authentication process
 *
 * @param startUrl The initial URL for the authentication process
 * @param endUrl   The final URL for the authentication process
 * @param context  The context used to create the authentication dialog
 * @param callback Callback to invoke when the authentication process finishes
 *//*from  ww w .j  av a  2 s  .  c  om*/
private void showLoginUIInternal(final String startUrl, final String endUrl, final Context context,
        LoginUIOperationCallback callback) {
    if (startUrl == null || startUrl == "") {
        throw new IllegalArgumentException("startUrl can not be null or empty");
    }

    if (endUrl == null || endUrl == "") {
        throw new IllegalArgumentException("endUrl can not be null or empty");
    }

    if (context == null) {
        throw new IllegalArgumentException("context can not be null");
    }

    final LoginUIOperationCallback externalCallback = callback;
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    // Create the Web View to show the login page
    final WebView wv = new WebView(context);
    builder.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            if (externalCallback != null) {
                externalCallback.onCompleted(null, new MobileServiceException("User Canceled"));
            }
        }
    });

    wv.getSettings().setJavaScriptEnabled(true);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int webViewHeight = displaymetrics.heightPixels - 100;

    wv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, webViewHeight));

    wv.requestFocus(View.FOCUS_DOWN);
    wv.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
                if (!view.hasFocus()) {
                    view.requestFocus();
                }
            }

            return false;
        }
    });

    // Create a LinearLayout and add the WebView to the Layout
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.addView(wv);

    // Add a dummy EditText to the layout as a workaround for a bug
    // that prevents showing the keyboard for the WebView on some devices
    EditText dummyEditText = new EditText(context);
    dummyEditText.setVisibility(View.GONE);
    layout.addView(dummyEditText);

    // Add the layout to the dialog
    builder.setView(layout);

    final AlertDialog dialog = builder.create();

    wv.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // If the URL of the started page matches with the final URL
            // format, the login process finished

            if (isFinalUrl(url)) {
                if (externalCallback != null) {
                    externalCallback.onCompleted(url, null);
                }

                dialog.dismiss();
            }

            super.onPageStarted(view, url, favicon);
        }

        // Checks if the given URL matches with the final URL's format
        private boolean isFinalUrl(String url) {
            if (url == null) {
                return false;
            }

            return url.startsWith(endUrl);
        }

        // Checks if the given URL matches with the start URL's format
        private boolean isStartUrl(String url) {
            if (url == null) {
                return false;
            }

            return url.startsWith(startUrl);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (isStartUrl(url)) {
                if (externalCallback != null) {
                    externalCallback.onCompleted(null, new MobileServiceException(
                            "Logging in with the selected authentication provider is not enabled"));
                }

                dialog.dismiss();
            }
        }
    });

    wv.loadUrl(startUrl);
    dialog.show();
}

From source file:com.undatech.opaque.RemoteCanvasActivity.java

/**
 * Initializes the on-screen keys for meta keys and arrow keys.
 *//* w w w.  j  a v  a 2  s  .  c  o m*/
private void initializeOnScreenKeys() {

    layoutKeys = (RelativeLayout) findViewById(R.id.layoutKeys);

    keyStow = (ImageButton) findViewById(R.id.keyStow);
    setKeyStowDrawableAndVisibility();
    keyStow.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (layoutKeys.getVisibility() == View.VISIBLE) {
                extraKeysHidden = true;
                setExtraKeysVisibility(View.GONE, false);
            } else {
                extraKeysHidden = false;
                setExtraKeysVisibility(View.VISIBLE, true);
            }
            layoutKeys.offsetTopAndBottom(prevBottomOffset);
            setKeyStowDrawableAndVisibility();
        }
    });

    // Define action of tab key and meta keys.
    keyTab = (ImageButton) findViewById(R.id.keyTab);
    keyTab.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent e) {
            RemoteKeyboard k = canvas.getKeyboard();
            int key = KeyEvent.KEYCODE_TAB;
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                myVibrator.vibrate(Constants.SHORT_VIBRATION);
                keyTab.setImageResource(R.drawable.tabon);
                k.repeatKeyEvent(key, new KeyEvent(e.getAction(), key));
                return true;
            } else if (e.getAction() == MotionEvent.ACTION_UP) {
                keyTab.setImageResource(R.drawable.taboff);
                resetOnScreenKeys(0);
                k.stopRepeatingKeyEvent();
                return true;
            }
            return false;
        }
    });

    keyEsc = (ImageButton) findViewById(R.id.keyEsc);
    keyEsc.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent e) {
            RemoteKeyboard k = canvas.getKeyboard();
            int key = 111; /* KEYCODE_ESCAPE */
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                myVibrator.vibrate(Constants.SHORT_VIBRATION);
                keyEsc.setImageResource(R.drawable.escon);
                k.repeatKeyEvent(key, new KeyEvent(e.getAction(), key));
                return true;
            } else if (e.getAction() == MotionEvent.ACTION_UP) {
                keyEsc.setImageResource(R.drawable.escoff);
                resetOnScreenKeys(0);
                k.stopRepeatingKeyEvent();
                return true;
            }
            return false;
        }
    });

    keyCtrl = (ImageButton) findViewById(R.id.keyCtrl);
    keyCtrl.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            boolean on = canvas.getKeyboard().onScreenCtrlToggle();
            keyCtrlToggled = false;
            if (on)
                keyCtrl.setImageResource(R.drawable.ctrlon);
            else
                keyCtrl.setImageResource(R.drawable.ctrloff);
        }
    });

    keyCtrl.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View arg0) {
            myVibrator.vibrate(Constants.SHORT_VIBRATION);
            boolean on = canvas.getKeyboard().onScreenCtrlToggle();
            keyCtrlToggled = true;
            if (on)
                keyCtrl.setImageResource(R.drawable.ctrlon);
            else
                keyCtrl.setImageResource(R.drawable.ctrloff);
            return true;
        }
    });

    keySuper = (ImageButton) findViewById(R.id.keySuper);
    keySuper.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            boolean on = canvas.getKeyboard().onScreenSuperToggle();
            keySuperToggled = false;
            if (on)
                keySuper.setImageResource(R.drawable.superon);
            else
                keySuper.setImageResource(R.drawable.superoff);
        }
    });

    keySuper.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View arg0) {
            myVibrator.vibrate(Constants.SHORT_VIBRATION);
            boolean on = canvas.getKeyboard().onScreenSuperToggle();
            keySuperToggled = true;
            if (on)
                keySuper.setImageResource(R.drawable.superon);
            else
                keySuper.setImageResource(R.drawable.superoff);
            return true;
        }
    });

    keyAlt = (ImageButton) findViewById(R.id.keyAlt);
    keyAlt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            boolean on = canvas.getKeyboard().onScreenAltToggle();
            keyAltToggled = false;
            if (on)
                keyAlt.setImageResource(R.drawable.alton);
            else
                keyAlt.setImageResource(R.drawable.altoff);
        }
    });

    keyAlt.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View arg0) {
            myVibrator.vibrate(Constants.SHORT_VIBRATION);
            boolean on = canvas.getKeyboard().onScreenAltToggle();
            keyAltToggled = true;
            if (on)
                keyAlt.setImageResource(R.drawable.alton);
            else
                keyAlt.setImageResource(R.drawable.altoff);
            return true;
        }
    });

    keyShift = (ImageButton) findViewById(R.id.keyShift);
    keyShift.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            boolean on = canvas.getKeyboard().onScreenShiftToggle();
            keyShiftToggled = false;
            if (on)
                keyShift.setImageResource(R.drawable.shifton);
            else
                keyShift.setImageResource(R.drawable.shiftoff);
        }
    });

    keyShift.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View arg0) {
            myVibrator.vibrate(Constants.SHORT_VIBRATION);
            boolean on = canvas.getKeyboard().onScreenShiftToggle();
            keyShiftToggled = true;
            if (on)
                keyShift.setImageResource(R.drawable.shifton);
            else
                keyShift.setImageResource(R.drawable.shiftoff);
            return true;
        }
    });

    // Define action of arrow keys.
    keyUp = (ImageButton) findViewById(R.id.keyUpArrow);
    keyUp.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent e) {
            RemoteKeyboard k = canvas.getKeyboard();
            int key = KeyEvent.KEYCODE_DPAD_UP;
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                myVibrator.vibrate(Constants.SHORT_VIBRATION);
                keyUp.setImageResource(R.drawable.upon);
                k.repeatKeyEvent(key, new KeyEvent(e.getAction(), key));
                return true;
            } else if (e.getAction() == MotionEvent.ACTION_UP) {
                keyUp.setImageResource(R.drawable.upoff);
                resetOnScreenKeys(0);
                k.stopRepeatingKeyEvent();
                return true;
            }
            return false;
        }
    });

    keyDown = (ImageButton) findViewById(R.id.keyDownArrow);
    keyDown.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent e) {
            RemoteKeyboard k = canvas.getKeyboard();
            int key = KeyEvent.KEYCODE_DPAD_DOWN;
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                myVibrator.vibrate(Constants.SHORT_VIBRATION);
                keyDown.setImageResource(R.drawable.downon);
                k.repeatKeyEvent(key, new KeyEvent(e.getAction(), key));
                return true;
            } else if (e.getAction() == MotionEvent.ACTION_UP) {
                keyDown.setImageResource(R.drawable.downoff);
                resetOnScreenKeys(0);
                k.stopRepeatingKeyEvent();
                return true;
            }
            return false;
        }
    });

    keyLeft = (ImageButton) findViewById(R.id.keyLeftArrow);
    keyLeft.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent e) {
            RemoteKeyboard k = canvas.getKeyboard();
            int key = KeyEvent.KEYCODE_DPAD_LEFT;
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                myVibrator.vibrate(Constants.SHORT_VIBRATION);
                keyLeft.setImageResource(R.drawable.lefton);
                k.repeatKeyEvent(key, new KeyEvent(e.getAction(), key));
                return true;
            } else if (e.getAction() == MotionEvent.ACTION_UP) {
                keyLeft.setImageResource(R.drawable.leftoff);
                resetOnScreenKeys(0);
                k.stopRepeatingKeyEvent();
                return true;
            }
            return false;
        }
    });

    keyRight = (ImageButton) findViewById(R.id.keyRightArrow);
    keyRight.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent e) {
            RemoteKeyboard k = canvas.getKeyboard();
            int key = KeyEvent.KEYCODE_DPAD_RIGHT;
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                myVibrator.vibrate(Constants.SHORT_VIBRATION);
                keyRight.setImageResource(R.drawable.righton);
                k.repeatKeyEvent(key, new KeyEvent(e.getAction(), key));
                return true;
            } else if (e.getAction() == MotionEvent.ACTION_UP) {
                keyRight.setImageResource(R.drawable.rightoff);
                resetOnScreenKeys(0);
                k.stopRepeatingKeyEvent();
                return true;
            }
            return false;
        }
    });
}

From source file:com.am.pullview.swiperefresh.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;/*w w w.j ava2s.  c  o  m*/
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (yDiff > mTouchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    handled = true;
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (mPrevY > eventY) {
                        offsetTop = yDiff - mTouchSlop;
                    }
                    updateContentOffsetTop((int) (offsetTop));
                    if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(mCancel);
                    } else {
                        updatePositionTimeout();
                    }
                    mPrevY = event.getY();
                    handled = true;
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
        }
        break;
    }
    return handled;
}

From source file:cn.sdgundam.comicatsdgo.extension.SwipeRefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    boolean handled = false;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mCurrPercentage = 0;//from w w  w. ja v  a2s .  co m
        mDownEvent = MotionEvent.obtain(event);
        mPrevY = mDownEvent.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        if (mDownEvent != null && !mReturningToStart) {
            final float eventY = event.getY();
            float yDiff = eventY - mDownEvent.getY();
            if (yDiff > mTouchSlop) {
                // User velocity passed min velocity; trigger a refresh
                if (yDiff > mDistanceToTriggerSync) {
                    // User movement passed distance; trigger a refresh
                    startRefresh();
                    break;
                } else {
                    // Just track the user's movement
                    setTriggerPercentage(
                            mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    float offsetTop = yDiff;
                    if (mPrevY > eventY) {
                        offsetTop = yDiff - mTouchSlop;
                    }
                    updateContentOffsetTop((int) (offsetTop));
                    if (mPrevY > eventY && (mTarget.getTop() < mTouchSlop)) {
                        // If the user puts the view back at the top, we
                        // don't need to. This shouldn't be considered
                        // cancelling the gesture as the user can restart from the top.
                        removeCallbacks(mCancel);
                    }
                    //                            else {
                    //                                updatePositionTimeout();
                    //                            }
                    mPrevY = event.getY();
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        if (mDownEvent != null) {
            mDownEvent.recycle();
            mDownEvent = null;
            updatePositionImmediately();
        }
        break;
    }
    return handled;
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    ImageView view = (ImageView) v;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        savedMatrix.set(matrix);//from   w w  w .j a  va 2 s.c o  m
        start.set(event.getX(), event.getY());
        mode = DRAG;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        oldDist = spacing(event);
        if (oldDist > 10f) {
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        break;
    case MotionEvent.ACTION_MOVE:
        if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
            start_x = event.getX() - start.x;
            start_y = event.getY() - start.y;
        } else if (mode == ZOOM) {
            float newDist = spacing(event);
            if (newDist > 10f) {
                matrix.set(savedMatrix);
                float scale = newDist / oldDist;
                mAspect = scale;
                matrix.postScale(scale, scale, mid.x, mid.y);
            }
        }
        break;
    }

    view.setImageMatrix(matrix);
    view.invalidate();
    return true;
}

From source file:co.vn.e_alarm.customwiget.SlidingLayer.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!mEnabled || !mIsDragging && !mLastTouchAllowed && !allowSlidingFromHereX(ev, mInitialX)
            && !allowSlidingFromHereY(ev, mInitialY)) {
        return false;
    }/*from  ww w  .j av  a 2 s.  co  m*/

    final int action = ev.getAction();

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL
            || action == MotionEvent.ACTION_OUTSIDE) {
        mLastTouchAllowed = false;
    } else {
        mLastTouchAllowed = true;
    }

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

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        completeScroll();
        // Remember where the motion event started
        mLastX = mInitialX = ev.getX();
        mLastY = mInitialY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        break;
    case MotionEvent.ACTION_MOVE:
        if (!mIsDragging) {
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (pointerIndex == -1) {
                mActivePointerId = INVALID_POINTER;
                break;
            }
            final float x = MotionEventCompat.getX(ev, pointerIndex);
            final float xDiff = Math.abs(x - mLastX);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = Math.abs(y - mLastY);
            if (xDiff > mTouchSlop && xDiff > yDiff) {
                mIsDragging = true;
                mLastX = x;
                setDrawingCacheEnabled(true);
            } else if (yDiff > mTouchSlop && yDiff > xDiff) {
                mIsDragging = true;
                mLastY = y;
                setDrawingCacheEnabled(true);
            }
        }
        if (mIsDragging) {
            // Scroll to follow the motion event
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (activePointerIndex == -1) {
                mActivePointerId = INVALID_POINTER;
                break;
            }
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final float deltaX = mLastX - x;
            final float deltaY = mLastY - y;
            mLastX = x;
            mLastY = y;
            final float oldScrollX = getScrollX();
            final float oldScrollY = getScrollY();
            float scrollX = oldScrollX + deltaX;
            float scrollY = oldScrollY + deltaY;

            /*
             * Not need change : Done
             */
            final float leftBound, rightBound;
            final float bottomBound, topBound;
            switch (mScreenSide) {
            case STICK_TO_LEFT:
                topBound = bottomBound = rightBound = 0;
                leftBound = getWidth(); // How far left we can scroll
                break;
            case STICK_TO_MIDDLE:
                topBound = getHeight();
                bottomBound = -getHeight();
                leftBound = getWidth();
                rightBound = -getWidth();
                break;
            case STICK_TO_RIGHT:
                rightBound = -getWidth();
                topBound = bottomBound = leftBound = 0;
                break;
            case STICK_TO_TOP:
                topBound = getHeight();
                bottomBound = rightBound = leftBound = 0;
                break;
            case STICK_TO_BOTTOM:
                topBound = rightBound = leftBound = 0;
                bottomBound = -getHeight();
                break;
            default:
                topBound = bottomBound = rightBound = leftBound = 0;
                break;
            }
            if (scrollX > leftBound) {
                scrollX = leftBound;
            } else if (scrollX < rightBound) {
                scrollX = rightBound;
            }
            if (scrollY > topBound) {
                scrollY = topBound;
            } else if (scrollY < bottomBound) {
                scrollY = bottomBound;
            }

            // TODO top/bottom bounds
            // Keep the precision
            mLastX += scrollX - (int) scrollX;
            mLastY += scrollY - (int) scrollY;
            scrollTo((int) scrollX, (int) scrollY);
        }
        break;
    //--- Doan nay dang kiem tra
    case MotionEvent.ACTION_UP:
        if (mIsDragging) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocityX = (int) VelocityTrackerCompat.getXVelocity(velocityTracker,
                    mActivePointerId);
            final int initialVelocityY = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                    mActivePointerId);
            final int scrollX = getScrollX();
            final int scrollY = getScrollY();
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float y = MotionEventCompat.getY(ev, activePointerIndex);
            final int totalDeltaX = (int) (x - mInitialX);
            final int totalDeltaY = (int) (y - mInitialY);

            /*
            * Here is condition to check when to change state of layer. open / close.
            */
            boolean nextStateOpened = determineNextStateOpened(mIsOpen, scrollX, scrollY, initialVelocityX,
                    initialVelocityY, totalDeltaX, totalDeltaY);
            switchLayer(nextStateOpened, true, true, initialVelocityX, initialVelocityY);

            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mIsOpen && closeOnTapEnabled) {
            closeLayer(true);
        } else if (!mIsOpen && openOnTapEnabled) {
            openLayer(true);
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsDragging) {
            switchLayer(mIsOpen, true, true);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastX = MotionEventCompat.getX(ev, index);
        mLastY = MotionEventCompat.getY(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        mLastY = MotionEventCompat.getY(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (mActivePointerId == INVALID_POINTER) {
        mLastTouchAllowed = false;
    }
    return true;
}

From source file:com.netease.qa.emmagee.service.EmmageeService.java

/**
 * create a floating window to show real-time data.
 *///w  ww .  java 2  s . co m
private void createFloatingWindow() {
    SharedPreferences shared = getSharedPreferences("float_flag", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = shared.edit();
    editor.putInt("float", 1);
    editor.commit();
    windowManager = (WindowManager) getApplicationContext().getSystemService("window");
    wmParams = ((MyApplication) getApplication()).getMywmParams();
    wmParams.type = 2002;
    wmParams.flags |= 8;
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;
    wmParams.x = 0;
    wmParams.y = 0;
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.format = 1;
    windowManager.addView(viFloatingWindow, wmParams);
    viFloatingWindow.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            x = event.getRawX();
            y = event.getRawY() - statusBarHeight;
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchStartX = event.getX();
                mTouchStartY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:
                updateViewPosition();
                mTouchStartX = mTouchStartY = 0;
                break;
            }
            return true;
        }
    });

    btnWifi.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                btnWifi = (Button) viFloatingWindow.findViewById(R.id.wifi);
                String buttonText = (String) btnWifi.getText();
                String wifiText = getResources().getString(R.string.open_wifi);
                if (buttonText.equals(wifiText)) {
                    wifiManager.setWifiEnabled(true);
                    btnWifi.setText(R.string.close_wifi);
                } else {
                    wifiManager.setWifiEnabled(false);
                    btnWifi.setText(R.string.open_wifi);
                }
            } catch (Exception e) {
                Toast.makeText(viFloatingWindow.getContext(), getString(R.string.wifi_fail_toast),
                        Toast.LENGTH_LONG).show();
                Log.e(LOG_TAG, e.toString());
            }
        }
    });
}