Example usage for android.view MotionEvent getRawX

List of usage examples for android.view MotionEvent getRawX

Introduction

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

Prototype

public final float getRawX() 

Source Link

Document

Returns the original raw X coordinate of this event.

Usage

From source file:com.jainbooks.activitys.DashboardActivity.java

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View currentView = getCurrentFocus();

    boolean retVal = super.dispatchTouchEvent(event);
    if (currentView instanceof EditText) {
        View cView = getCurrentFocus();
        int scrCoordinates[] = new int[2];
        cView.getLocationOnScreen(scrCoordinates);
        float xPos = event.getRawX() + cView.getLeft() - scrCoordinates[0];
        float yPos = event.getRawY() + cView.getTop() - scrCoordinates[1];

        if (event.getAction() == MotionEvent.ACTION_UP && (xPos < cView.getLeft() || xPos >= cView.getRight()
                || yPos < cView.getTop() || yPos > cView.getBottom())) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }//from   ww  w.  j a  v a  2  s .c  o m
    }
    return retVal;
}

From source file:com.artemchep.horario.ui.widgets.SwipeBackLayout.java

private void chkDragable() {
    setOnTouchListener(new View.OnTouchListener() {
        @Override/*from w  ww . jav a  2 s  . co m*/
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                lastY = motionEvent.getRawY();
                lastX = motionEvent.getRawX();
            } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                newY = motionEvent.getRawY();
                lastX = motionEvent.getRawX();

                offsetY = Math.abs(newY - lastY);
                lastY = newY;

                offsetX = Math.abs(newX - lastX);
                lastX = newX;

                switch (dragEdge) {
                case TOP:
                case BOTTOM:
                    setEnablePullToBack(offsetY > offsetX);
                case LEFT:
                case RIGHT:
                    setEnablePullToBack(offsetY < offsetX);
                    break;
                }
            }

            return false;
        }
    });
}

From source file:com.waz.zclient.pages.main.conversationlist.views.listview.SwipeListView.java

/**
 * @see android.widget.ListView#onTouchEvent(android.view.MotionEvent)
 *//*from   w w  w  . j a v a2 s . c o  m*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
    // swiping is not enabled, let parent (ListView) eat that event
    if (targetView == null || !targetView.isSwipeable() || touchState == TOUCH_STATE_SCROLLING_Y) {
        return super.onTouchEvent(ev);
    }
    float deltaX = ev.getRawX() - downX;
    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_MOVE:
        velocityTracker.addMovement(ev);
        velocityTracker.computeCurrentVelocity(1000);
        targetView.setOffset(deltaX);
        return true;
    case MotionEvent.ACTION_UP:
        if (velocityTracker == null) {
            break;
        }

        velocityTracker.addMovement(ev);
        velocityTracker.computeCurrentVelocity(1000);

        float velocityX = Math.abs(velocityTracker.getXVelocity());

        if (velocityTracker.getXVelocity() < 0) {
            velocityX = 0;
        }

        float velocityY = Math.abs(velocityTracker.getYVelocity());

        if (targetView != null) {

            boolean flingRight = false;

            if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY * 2 < velocityX
                    && deltaX > 0) {
                flingRight = true;
            }
            if (allowSwipeAway && deltaX > viewWidth / 2) {
                if (targetView != null) {
                    targetView.swipeAway();
                }
            } else if ((!allowSwipeAway && (deltaX > viewWidth / 2 || flingRight))
                    || (allowSwipeAway && (deltaX > viewWidth / 4 || flingRight))) {
                if (targetView.isOpen()) {
                    closeItem();
                } else {
                    openItem();
                }
            } else {
                closeItem();
            }
        }

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        return true;
    }

    return super.onTouchEvent(ev);
}

From source file:com.github.shareme.gwsswwipetodismiss.library.SwipeDismissTouchListener.java

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

    if (mViewWidth < 2) {
        mViewWidth = mView.getWidth();//w  w w  . jav  a2s  .c o  m
    }

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

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

        float deltaX = motionEvent.getRawX() - mDownX;
        float deltaY = motionEvent.getRawY() - mDownY;
        mVelocityTracker.addMovement(motionEvent);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(mVelocityTracker.getXVelocity());
        float velocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (Math.abs(deltaY) < Math.abs(deltaX)) {
            if (Math.abs(deltaX) > Math.round(mViewWidth * SWIPE_SENSITIVITY)) {
                dismiss = true;
                dismissRight = deltaX > 0;
            } else if (Math.abs(deltaX) > mSlop && mMinFlingVelocity <= velocityX
                    && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
                dismiss = true;
                dismissRight = mVelocityTracker.getXVelocity() > 0;
            }
        }
        if (dismiss) {
            // dismiss
            mView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0)
                    .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            performDismiss();
                        }
                    });
        } else {
            // cancel
            mView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null);
        }
        mVelocityTracker = null;
        mTranslationX = 0;
        mDownX = 0;
        mDownY = 0;
        mSwiping = false;
        break;
    }

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

        mVelocityTracker.addMovement(motionEvent);
        float deltaX = motionEvent.getRawX() - mDownX;
        if (Math.abs(deltaX) > mSlop) {
            mSwiping = true;
            mView.getParent().requestDisallowInterceptTouchEvent(true);

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

        if (mSwiping) {
            mTranslationX = deltaX;
            setTranslationX(mView, deltaX);
            // TODO: use an ease-out interpolator or such
            setAlpha(mView, Math.max(0f, Math.min(1f, 1f - Math.abs(deltaX) / SWIPE_SENSITIVITY * mViewWidth)));
            return true;
        }
        break;
    }
    }
    return false;
}

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

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

    if (mViewWidth < 2) {
        mViewWidth = mView.getWidth();//w  w  w . ja  v  a2  s.  c om
    }

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

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

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

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

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

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

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

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

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

From source file:com.appsimobile.appsii.SidebarHotspot.java

private boolean detectSwipe(float x, float y, MotionEvent e) {
    if (mState == STATE_AWAITING_RELEASE && mSwipeInProgress) {
        if (mCallback != null) {
            mSwipeListener.setSwipeLocation(this, (int) e.getX(), (int) e.getY(), (int) e.getRawX(),
                    (int) e.getRawY());
        }/*from  www  . ja  va 2 s  . c  o m*/
        return true;
    }
    if (mState != STATE_GESTURE_IN_PROGRESS)
        return false;
    float deltaX = Math.abs(x - mStartX);
    float deltaY = y - mStartY;
    Gesture action = detectAction(deltaX, deltaY, mMinimumMove);
    if (action != null) {
        mSwipeListener = mCallback.open(this, action, (int) x, (int) y);
        mSwipeInProgress = mSwipeListener != null;
        mState = STATE_AWAITING_RELEASE;
        return true;
    }
    return true;
}

From source file:com.test.slidebutton.SlideButton.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (slidable == false)
        return super.onTouchEvent(event);

    int action = MotionEventCompat.getActionMasked(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        startX = (int) event.getRawX();
        startY = (int) event.getRawY();
        break;//w  w  w  .ja  va2  s .c om
    case MotionEvent.ACTION_MOVE:
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();

        offsetX = lastX - startX;

        if (!this.canMove) {
            if (Math.abs(offsetX) > touchSlop && Math.abs(offsetX) > Math.abs(lastY - startY)) {
                this.canMove = true;
                return true;
            } else {
                return super.onTouchEvent(event);
            }
        } else {
            sliderCurrentLeft = offsetX + sliderMoveStartLeft;
            if (sliderCurrentLeft > max_left) {
                sliderCurrentLeft = max_left;
            } else if (sliderCurrentLeft < min_left) {
                sliderCurrentLeft = min_left;
            }
            alpha = (int) (255 * ((sliderCurrentLeft - min_left) / (float) (max_left - min_left)));
            caculateColor();
            invalidateView();
        }
        break;
    case MotionEvent.ACTION_UP:
        lastX = (int) event.getRawX();
        boolean toRight = (sliderCurrentLeft > max_left / 2 ? true : false);
        if (this.canMove) {
            this.canMove = false;
        } else {//
            toRight = !toRight;
        }
        moveTo(toRight);
        break;
    default:
        break;
    }
    return true;
}

From source file:org.coursera.android.shift.ShiftIconFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.shift_menu, container, false);

    mHead = layout.findViewById(R.id.head);
    final RelativeLayout.LayoutParams iconParams;
    iconParams = (RelativeLayout.LayoutParams) mHead.getLayoutParams();

    final GestureDetector gestureDetector = new GestureDetector(getActivity(), new SingleTapConfirm());

    mHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override/*from   ww w .j  a  va 2  s .co m*/
        public boolean onTouch(View v, MotionEvent event) {
            final int yLimit = layout.getMeasuredHeight() - mHead.getMeasuredHeight();
            final int xLimit = layout.getMeasuredWidth() - mHead.getMeasuredWidth();

            // single tap
            if (gestureDetector.onTouchEvent(event)) {
                mShiftLauncherView.showShiftMenu(getActivity());
                return true;
            }

            // drag icon
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = iconParams.leftMargin;
                initialY = iconParams.topMargin;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                return true;
            case MotionEvent.ACTION_CANCEL:
                return false;
            case MotionEvent.ACTION_UP:
                return true;
            case MotionEvent.ACTION_MOVE:
                int newX = initialX + (int) (event.getRawX() - initialTouchX);
                int newY = initialY + (int) (event.getRawY() - initialTouchY);

                iconParams.leftMargin = newX < 0 ? 0 : Math.min(newX, xLimit);
                iconParams.topMargin = newY < 0 ? 0 : Math.min(newY, yLimit);

                layout.updateViewLayout(mHead, iconParams);
                return true;
            }
            return false;
        }
    });
    return layout;
}

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

/**
 * create a floating window to show real-time data.
 *//*from  w  ww .ja v a  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());
            }
        }
    });
}

From source file:com.lixplor.rocketpulltorefresh.RefreshLayout.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mDownRawY = event.getRawY();//from w  w w .  jav a  2s.  c om
        mLastTouchRawY = mDownRawY;
        break;
    case MotionEvent.ACTION_MOVE:
        float moveRawX = event.getRawX();
        float moveRawY = event.getRawY();
        float dy = moveRawY - mLastTouchRawY;
        float scrollY = getScrollY();

        // move entire layout to display header or footer
        scrollWithLimit(dy, scrollY);

        if (scrollY < 0) {
            performScrollDown(scrollY, moveRawX, moveRawY);
        } else if (scrollY > 0) {
            performScrollUp(scrollY, moveRawX, moveRawY);
        }
        mLastTouchRawY = moveRawY;
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        performTouchFinish();
        break;
    }
    return true;
}