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.ahutjw.indicator.TitlePageIndicator.java

@Override
public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from w  ww .  j a  v  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;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        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.along.altmcssd.pda.widget.viewpagerindicator.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   w  w  w  .  j  a  va2s. 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;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                // Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        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.cssweb.android.view.KlineMini.java

public void touchesMoved(MotionEvent event) {
    //Log.i("========currentPositionX==========", currentPositionX+">>>>>>>>>>");
    //Log.i("========currentPositionY==========", currentPositionY+">>>>>>>>>>");
    currentPositionX = event.getX();
    currentPositionY = event.getY();//w  w  w.j  a  v  a  2 s  . c  o  m
    float deltaX = startPositionX - currentPositionX;
    float deltaY = startPositionY - currentPositionY;
    //Log.i("========Math.abs(delta)==========", Math.abs(deltaX)+">>>>>>>>>>"+Math.abs(deltaY));
    if (Math.abs(deltaX) < 8 && Math.abs(deltaY) < 8) {
        isTouchMoved = false;
    } else {
        isTouchMoved = true;
    }
    if (quoteData == null)
        return;
    int mouseX = (int) event.getX();
    int mouseY = (int) event.getY();
    if (mouseX <= klineX || mouseY <= axisLabelHeight || mouseY >= height - axisLabelHeight - zbmenuHeight)
        return;

    if (isTrackStatus) {//??
        isSingleMoved = false;//???
        double sep = (shapeWidth + spaceWidth);
        visualPos = (int) ((mouseX - klineX) / sep + 1);
        if (visualPos >= this.actualDataLen - actualPos) {
            visualPos = this.actualDataLen - actualPos;
        } else if (visualPos < 1)
            visualPos = 1;
        int idx = actualPos + visualPos - 1;

        if (mouseX > klineX) {
            trackLineV = (int) (visualPos * (spaceWidth + shapeWidth) - shapeWidth / 2);
        }

        if (idx < actualDataLen) {
            isTrackNumber = idx;
        } else {
            isTrackNumber = this.actualDataLen - 1;
        }
        Log.i("@@@@@@@@@@isTrackNumber@@@@@@@@@@", isTrackNumber + ">>>>>>>>>");
        Log.i("@@@@@@@@@@idx@@@@@@@@@@", idx + ">>>>>>>>>");
        Log.i("@@@@@@@@@@actualPos@@@@@@@@@@", actualPos + ">>>>>>>>>");
        Log.i("@@@@@@@@@@visualPos@@@@@@@@@@", visualPos + ">>>>>>>>>");
        this.invalidate();
    } else {//K?
        isSingleMoved = true;
        if (deltaX < -50 && deltaY < 10) {
            moveRight(1);
        } else if (deltaX > 50 && deltaY < 10) {
            moveLeft(1);
        }
    }
}

From source file:android.hqs.view.pager.indicator.TitlePageIndicator.java

public boolean onTouchEvent(android.view.MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*w  w w .  java2 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;
            final float leftThird = halfWidth - sixthWidth;
            final float rightThird = halfWidth + sixthWidth;
            final float eventX = ev.getX();

            if (eventX < leftThird) {
                if (mCurrentPage > 0) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                }
            } else if (eventX > rightThird) {
                if (mCurrentPage < count - 1) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            } else {
                //Middle third
                if (mCenterItemClickListener != null && action != MotionEvent.ACTION_CANCEL) {
                    mCenterItemClickListener.onCenterItemClick(mCurrentPage);
                }
            }
        }

        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.appsummary.luoxf.myappsummary.navigationtabstrip.NavigationTabStrip.java

@Override
public boolean onTouchEvent(final MotionEvent event) {
    // Return if animation is running
    if (mAnimator.isRunning())
        return true;
    // If is not idle state, return
    if (mScrollState != ViewPager.SCROLL_STATE_IDLE)
        return true;

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // Action down touch
        mIsActionDown = true;//from   w w  w.  j  av  a  2 s. c  o  m
        if (!mIsViewPagerMode)
            break;
        // Detect if we touch down on tab, later to move
        mIsTabActionDown = (int) (event.getX() / mTabSize) == mIndex;
        break;
    case MotionEvent.ACTION_MOVE:
        // If tab touched, so move
        if (mIsTabActionDown) {
            mViewPager.setCurrentItem((int) (event.getX() / mTabSize), true);
            break;
        }
        if (mIsActionDown)
            break;
    case MotionEvent.ACTION_UP:
        // Press up and set tab index relative to current coordinate
        if (mIsActionDown)
            setTabIndex((int) (event.getX() / mTabSize));
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_OUTSIDE:
    default:
        // Reset action touch variables
        mIsTabActionDown = false;
        mIsActionDown = false;
        break;
    }

    return true;
}

From source file:com.android.documentsui.DocumentsActivity.java

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    mRoots = DocumentsApplication.getRootsCache(this);

    setResult(Activity.RESULT_CANCELED);
    setContentView(R.layout.activity);/*  w  ww .jav a  2 s .  c  o m*/

    final Resources res = getResources();
    mShowAsDialog = res.getBoolean(R.bool.show_as_dialog);

    if (mShowAsDialog) {
        // backgroundDimAmount from theme isn't applied; do it manually
        final WindowManager.LayoutParams a = getWindow().getAttributes();
        a.dimAmount = 0.6f;
        getWindow().setAttributes(a);

        getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().setFlags(~0, WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        // Inset ourselves to look like a dialog
        final Point size = new Point();
        getWindowManager().getDefaultDisplay().getSize(size);

        final int width = (int) res.getFraction(R.dimen.dialog_width, size.x, size.x);
        final int height = (int) res.getFraction(R.dimen.dialog_height, size.y, size.y);
        final int insetX = (size.x - width) / 2;
        final int insetY = (size.y - height) / 2;

        final Drawable before = getWindow().getDecorView().getBackground();
        final Drawable after = new InsetDrawable(before, insetX, insetY, insetX, insetY);
        getWindow().getDecorView().setBackground(after);

        // Dismiss when touch down in the dimmed inset area
        getWindow().getDecorView().setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    final float x = event.getX();
                    final float y = event.getY();
                    if (x < insetX || x > v.getWidth() - insetX || y < insetY || y > v.getHeight() - insetY) {
                        finish();
                        return true;
                    }
                }
                return false;
            }
        });

    } else {
        // Non-dialog means we have a drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer_glyph,
                R.string.drawer_open, R.string.drawer_close);

        mDrawerLayout.setDrawerListener(mDrawerListener);
        mDrawerLayout.setDrawerShadow(R.drawable.ic_drawer_shadow, GravityCompat.START);

        mRootsContainer = findViewById(R.id.container_roots);
    }

    mDirectoryContainer = (DirectoryContainerView) findViewById(R.id.container_directory);

    if (icicle != null) {
        mState = icicle.getParcelable(EXTRA_STATE);
    } else {
        buildDefaultState();
    }

    // Hide roots when we're managing a specific root
    if (mState.action == ACTION_MANAGE) {
        if (mShowAsDialog) {
            findViewById(R.id.dialog_roots).setVisibility(View.GONE);
        } else {
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }
    }

    if (mState.action == ACTION_CREATE) {
        final String mimeType = getIntent().getType();
        final String title = getIntent().getStringExtra(Intent.EXTRA_TITLE);
        SaveFragment.show(getFragmentManager(), mimeType, title);
    }

    if (mState.action == ACTION_GET_CONTENT) {
        final Intent moreApps = new Intent(getIntent());
        moreApps.setComponent(null);
        moreApps.setPackage(null);
        RootsFragment.show(getFragmentManager(), moreApps);
    } else if (mState.action == ACTION_OPEN || mState.action == ACTION_CREATE) {
        RootsFragment.show(getFragmentManager(), null);
    }

    if (!mState.restored) {
        if (mState.action == ACTION_MANAGE) {
            final Uri rootUri = getIntent().getData();
            new RestoreRootTask(rootUri).executeOnExecutor(getCurrentExecutor());
        } else {
            new RestoreStackTask().execute();
        }
    } else {
        onCurrentDirectoryChanged(ANIM_NONE);
    }
}

From source file:com.cssweb.android.view.KlineMini.java

public void touchesEnded(MotionEvent event) {
    isTouched = false;/*w w w.  j  a  va 2s.  com*/
    if (isTouchMoved == false) {
        isTouchMoved = false;
        isTrackStatus = !isTrackStatus;
        if (isTrackStatus) {//??
            int mouseX = (int) event.getX();
            int mouseY = (int) event.getY();
            if (mouseX <= klineX || mouseY <= axisLabelHeight
                    || mouseY >= height - axisLabelHeight - zbmenuHeight)
                return;
            double sep = (shapeWidth + spaceWidth);
            visualPos = (int) ((mouseX - klineX) / sep + 1);
            if (visualPos >= this.actualDataLen - actualPos) {
                visualPos = this.actualDataLen - actualPos;
                isTrackNumber = actualDataLen - 1;
            } else {
                isTrackNumber = actualPos + visualPos - 1;
            }
            trackLineV = (int) (visualPos * (spaceWidth + shapeWidth) - shapeWidth / 2);
            Log.i("========isTouched222==========",
                    trackLineV + ">>>>>" + visualKLineCount + ">>>>>" + isTrackNumber);
            Log.i("========isTouchMoved==========", actualDataLen + ">>>>" + actualPos + ">>>>>>" + visualPos);
        } else {
            isSingleMoved = true;
        }
    } else {

    }
    this.invalidate();
    //Log.i("========isTouched==========", isTouched+">>>>>>>>>>");
    //Log.i("========isTouchMoved==========", isTouchMoved+">>>>>>>>>>");
    //Log.i("========isTrackStatus==========", isTrackStatus+">>>>>>>>>>");
}

From source file:no.ntnu.idi.socialhitchhiking.map.MapActivityCreateOrEditRoute.java

/** onSingleTapUp(MotionEvent e)
 * updates the {@link AutoCompleteTextView} fields on map movement
 *//*from  w w w  .j  ava  2s .  c  o m*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
    GeoPoint gp = mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY());
    MapLocation mapLocation = (MapLocation) GeoHelper.getLocation(gp);

    addPoint(mapLocation);
    fillFieldsOnClick();
    return false;
}

From source file:com.spoiledmilk.ibikecph.map.MapActivity.java

protected boolean onImgSwiperTouch(View view, MotionEvent event) {
    if (leftContainer.getVisibility() != View.VISIBLE) {
        leftContainer.setVisibility(View.VISIBLE);
    }//from w  ww  .j  a v  a2 s .  c  o m
    if (mapDisabledView.getVisibility() != View.VISIBLE) {
        mapDisabledView.setVisibility(View.VISIBLE);
    }
    switch (event.getAction()) {
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        view.setPressed(false);
        if (moveCount <= 3)
            translate(slidden ? -maxSlide : maxSlide, true);
        else
            translate(0, true);
        break;
    case MotionEvent.ACTION_DOWN:
        moveCount = 0;
        view.setPressed(true);
        touchX = event.getX();
        break;
    case MotionEvent.ACTION_MOVE:
        if (moveCount++ < 3)
            break;
        newTouchX = event.getX();
        delta = newTouchX - touchX;
        translate(delta, false);
        touchX = newTouchX;
        break;
    }

    if (slidden && mapDisabledView.getVisibility() != View.GONE) {
        mapDisabledView.setVisibility(View.GONE);
    }

    return true;
}

From source file:cn.org.eshow.framwork.view.slidingmenu.CustomViewAbove.java

/**
 * Checks if is in ignored view.//  w w w.  ja  v a2 s.c o m
 *
 * @param ev the ev
 * @return true, if is in ignored view
 */
private boolean isInIgnoredView(MotionEvent ev) {
    Rect rect = new Rect();
    for (View v : mIgnoredViews) {
        v.getHitRect(rect);
        if (rect.contains((int) ev.getX(), (int) ev.getY()))
            return true;
    }
    return false;
}