Example usage for android.view MotionEvent getX

List of usage examples for android.view MotionEvent getX

Introduction

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

Prototype

public final float getX() 

Source Link

Document

#getX(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:com.android.launcher3.allapps.FullMergeAlgorithm.java

/**
 * Handles the touch events to dismiss all apps when clicking outside the bounds of the
 * recycler view./*from   ww w  .ja v a  2 s . c om*/
 */
private boolean handleTouchEvent(MotionEvent ev) {
    DeviceProfile grid = mLauncher.getDeviceProfile();
    int x = (int) ev.getX();
    int y = (int) ev.getY();

    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (!mContentBounds.isEmpty()) {
            // Outset the fixed bounds and check if the touch is outside all apps
            Rect tmpRect = new Rect(mContentBounds);
            tmpRect.inset(-grid.allAppsIconSizePx / 2, 0);
            if (ev.getX() < tmpRect.left || ev.getX() > tmpRect.right) {
                mBoundsCheckLastTouchDownPos.set(x, y);
                return true;
            }
        } else {
            // Check if the touch is outside all apps
            if (ev.getX() < getPaddingLeft() || ev.getX() > (getWidth() - getPaddingRight())) {
                mBoundsCheckLastTouchDownPos.set(x, y);
                return true;
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mBoundsCheckLastTouchDownPos.x > -1) {
            ViewConfiguration viewConfig = ViewConfiguration.get(getContext());
            float dx = ev.getX() - mBoundsCheckLastTouchDownPos.x;
            float dy = ev.getY() - mBoundsCheckLastTouchDownPos.y;
            float distance = (float) Math.hypot(dx, dy);
            if (distance < viewConfig.getScaledTouchSlop()) {
                // The background was clicked, so just go home
                Launcher launcher = Launcher.getLauncher(getContext());
                launcher.showWorkspace(true);
                return true;
            }
        }
        // Fall through
    case MotionEvent.ACTION_CANCEL:
        mBoundsCheckLastTouchDownPos.set(-1, -1);
        break;
    }
    return false;
}

From source file:com.example.appf.CS3570.java

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

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

    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        if (mother.out != null) {
            mother.out.println("{\"type\": \"DOWN\", \"x\": " + x + ", \"y\": " + y + "}\0");
        }//from w  ww  .  jav a  2  s .  c  om
        break;
    case MotionEvent.ACTION_UP:
        if (mother.out != null) {
            mother.out.println("{\"type\": \"UP\", \"x\": " + x + ", \"y\": " + y + "}\0");
        }
        break;
    case MotionEvent.ACTION_MOVE:

        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        // 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;
        }

        //mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;  // = 180.0f / 320
        //mRenderer.mTetra.rotate(dx, 0, 1, 0);
        //mRenderer.mTetra.rotate(dy, 1, 0, 0);
        //mRenderer.mCamera.rotateY((double)(dx * TOUCH_SCALE_FACTOR));
        //mRenderer.mCamera.rotateX((double)(dy * TOUCH_SCALE_FACTOR));
        if (mother.out != null) {
            Log.e("eee", "{\"type\": \"MOVE\", \"x\": " + x + ", \"y\": " + y + "}");
            mother.out.println("{\"type\": \"MOVE\", \"x\": " + x + ",  \"y\": " + y + "}\0");
        }
        requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.DayPickerViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!mCanPickRange) {
        return super.onInterceptTouchEvent(ev);
    }//from  www .  jav a 2 s  . c o m

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: DOWN");
        }

        mInitialDownX = ev.getX();
        mInitialDownY = ev.getY();

        if (mCheckForLongPress == null) {
            mCheckForLongPress = new CheckForLongPress();
        }

        postDelayed(mCheckForLongPress, ViewConfiguration.getLongPressTimeout());
    } else if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: (UP || CANCEL)");
        }

        if (mCheckForLongPress != null) {
            removeCallbacks(mCheckForLongPress);
        }

        mIsLongPressed = false;
        mInitialDownX = -1;
        mInitialDownY = -1;
    } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        if (Config.DEBUG) {
            Log.i(TAG, "OITE: MOVE");
        }

        if (!isStillALongPress((int) ev.getX(), (int) ev.getY())) {
            if (Config.DEBUG) {
                Log.i(TAG, "OITE: MOVED TOO MUCH, CANCELLING CheckForLongPress Runnable");
            }

            if (mCheckForLongPress != null) {
                removeCallbacks(mCheckForLongPress);
            }
        }
    }

    return mIsLongPressed || super.onInterceptTouchEvent(ev);
}

From source file:com.androtex.viewpagerindicator.CirclePageIndicator.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        final int count = mViewPager.getAdapter().getCount();
        final int longSize = (mOrientation == HORIZONTAL) ? getWidth() : getHeight();
        final float halfLongSize = longSize / 2;
        final float halfCircleLongSize = (count * 3 * mRadius) / 2;
        final float pointerValue = (mOrientation == HORIZONTAL) ? event.getX() : event.getY();

        if ((mCurrentPage > 0) && (pointerValue < halfLongSize - halfCircleLongSize)) {
            setCurrentItem(mCurrentPage - 1);
            return true;
        } else if ((mCurrentPage < count - 1) && (pointerValue > halfLongSize + halfCircleLongSize)) {
            setCurrentItem(mCurrentPage + 1);
            return true;
        }//  ww w.  j  a  v a 2  s.c  o m
    }

    return super.onTouchEvent(event);
}

From source file:com.am.pagergradienttab.view.PagerGradientTabStrip.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    final float x = ev.getX();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        break;/*from   w w  w. ja  v a 2s.  c om*/

    case MotionEvent.ACTION_MOVE:
        break;

    case MotionEvent.ACTION_UP:
        for (int i = 0; i < tabs.size(); i++) {
            float l = getPaddingLeft() + intervalWidth * i + itemWidth * i;
            float r = l + itemWidth;
            if (x >= l && x <= r) {
                clickPosition = i;
                break;
            }
        }
        performClick();
        break;
    }

    return true;
}

From source file:com.benefit.buy.library.viewpagerindicator.UnderlinePageIndicator.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//from w ww  .java  2s . co m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }
    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;
        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }
        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;
            if ((mCurrentPage > 0) && (ev.getX() < (halfWidth - sixthWidth))) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < (count - 1)) && (ev.getX() > (halfWidth + sixthWidth))) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }
        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            mViewPager.endFakeDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    return true;
}

From source file:com.pitchedapps.primenumbercalculator.Calculator.java

License:asdf

void addOnTouchListener(View view) {
    Button button = (Button) view;
    button.setOnTouchListener(new View.OnTouchListener() {
        @Override/* w  w  w. j  a va2 s .  c  o m*/
        public boolean onTouch(View v, MotionEvent e) {
            x = (int) e.getX() + v.getLeft();
            y = (int) e.getY() + v.getTop();
            return false;
        }
    });
}

From source file:com.pitchedapps.primenumbercalculator.Calculator.java

License:asdf

void addOnTouchListenerText(View view) {
    TextView text = (TextView) view;//from   w  ww.  j  ava  2 s. c om
    text.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            x = (int) e.getX() + v.getLeft();
            y = (int) e.getY() + v.getTop();
            return false;
        }
    });
}

From source file:com.base.app.widget.indicatorview.UnderlinePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*w w w.jav  a2 s .  c o  m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;
    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;
        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }
        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.android.andryyu.lifehelper.widget.RippleView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        animateRipple(event);/*from   www  . j a v  a 2s. c o m*/
        sendClickEvent(false);
        rippleStatus = RIPPLE_SINGLE;
    }
    if (rippleStatus == RIPPLE_LONG_PRESS) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            rippleStatus = RIPPLE_ACTION_UP;
        } else if (Math.abs(event.getX() - lastLongPressX) >= touchSlop
                || Math.abs(event.getY() - lastLongPressY) >= touchSlop) {
            rippleStatus = RIPPLE_ACTION_MOVE;
        }
    }

    return super.onTouchEvent(event);
}