Java Code Examples for android.view.MotionEvent#getX()

The following examples show how to use android.view.MotionEvent#getX() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public boolean onTouchEvent(MotionEvent event) {
	int x = (int) event.getX() - getRelativeLeft((View)clickMark.getParent()) - (clickMark.getWidth() / 2);
	int y = (int) event.getY() - getRelativeTop((View)clickMark.getParent()) - (clickMark.getHeight() / 2);

	switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			clickMark.setX(x);
			clickMark.setY(y);
			break;

		case MotionEvent.ACTION_MOVE:
			clickMark.setX(x);
			clickMark.setY(y);
			break;

		case MotionEvent.ACTION_UP:
			break;
	}

	return true;
}
 
Example 2
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    boolean b = viewDragHelper.shouldInterceptTouchEvent(ev);
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        viewDragHelper.processTouchEvent(ev);
    }
    float dx = 0, dy = 0;
    if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
        x = ev.getX();
        y = ev.getY();
    } else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
        dx = Math.abs(ev.getX() - x);
        dy = Math.abs(ev.getY() - y);
        x = ev.getX();
        y = ev.getY();
    } else if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
        x = 0;
        y = 0;
    }
    boolean scroll = dx > 4 || dy > 4;
    //Log.i("tag", String.format("onScroll: %s %s %s", scroll, dx, dy));
    return b && isNeedSwipe && scroll;
}
 
Example 3
public GestureDirection.UserGesture getGesture(MotionEvent motionEvent) {
    final float x = motionEvent.getX();
    final float y = motionEvent.getY();
    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mDirectionPoint = new GestureDirection(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            mDirectionPoint.updateEndPoint(x, y);
            break;
        case MotionEvent.ACTION_UP:
            if (mDirectionPoint != null) {
                return mDirectionPoint.getDirection();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            mDirectionPoint = null;
            break;
    }
    return GestureDirection.UserGesture.SWIPE_UNKNOWN;
}
 
Example 4
public boolean onConfigureTouch(MotionEvent event)
{
  int pointerIndex = event.getActionIndex();
  int fingerPositionX = (int) event.getX(pointerIndex);
  int fingerPositionY = (int) event.getY(pointerIndex);
  switch (event.getAction())
  {
    case MotionEvent.ACTION_DOWN:
      mPreviousTouchX = fingerPositionX;
      mPreviousTouchY = fingerPositionY;
      break;
    case MotionEvent.ACTION_MOVE:
      mControlPositionX += fingerPositionX - mPreviousTouchX;
      mControlPositionY += fingerPositionY - mPreviousTouchY;
      setBounds(mControlPositionX, mControlPositionY, getWidth() + mControlPositionX,
              getHeight() + mControlPositionY);
      mPreviousTouchX = fingerPositionX;
      mPreviousTouchY = fingerPositionY;
      break;

  }
  return true;
}
 
Example 5
Source Project: GithubApp   File: AppIntroViewPager.java    License: Apache License 2.0 6 votes vote down vote up
private boolean checkPagingState(MotionEvent event) {
    if (!pagingEnabled) {
        return true;
    }

    if (!nextPagingEnabled) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            currentTouchDownX = event.getX();
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (detectSwipeToRight(event)) {
                return true;
            }
        }
    }

    return false;
}
 
Example 6
Source Project: easydeviceinfo   File: EasyDisplayMod.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get display xy coordinates int [ ].
 *
 * @param event
 *     the event
 *
 * @return the int [ ]
 */
public final int[] getDisplayXYCoordinates(final MotionEvent event) {
  int[] coordinates = new int[2];
  coordinates[0] = 0;
  coordinates[1] = 0;
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    coordinates[0] = (int) event.getX();     // X Coordinates
    coordinates[1] = (int) event.getY();     // Y Coordinates
  }
  return coordinates;
}
 
Example 7
@Override
public void onLongPress(MotionEvent e) {
    if (!mShouldReactToLongPress) {
        return;
    }
    View child = findChildView(e);
    if (child != null) {
        ViewHolder vh = mRecyclerView.getChildViewHolder(child);
        if (vh != null) {
            if (!mCallback.hasDragFlag(mRecyclerView, vh)) {
                return;
            }
            int pointerId = e.getPointerId(0);
            // Long press is deferred.
            // Check w/ active pointer id to avoid selecting after motion
            // event is canceled.
            if (pointerId == mActivePointerId) {
                final int index = e.findPointerIndex(mActivePointerId);
                final float x = e.getX(index);
                final float y = e.getY(index);
                mInitialTouchX = x;
                mInitialTouchY = y;
                mDx = mDy = 0f;
                if (DEBUG) {
                    Log.d(TAG,
                            "onlong press: x:" + mInitialTouchX + ",y:" + mInitialTouchY);
                }
                if (mCallback.isLongPressDragEnabled()) {
                    select(vh, ACTION_STATE_DRAG);
                }
            }
        }
    }
}
 
Example 8
Source Project: Common   File: ToggleButton.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!(mFactor == 0 || mFactor == 1)) {
        return false;
    }
    float eX = event.getX();
    float eY = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mDX = eX;
            mDY = eY;
            mIsClickValid = true;
        case MotionEvent.ACTION_MOVE:
            if (mIsClickValid && (Math.abs(eX - mDX) > mTouchSlop || Math.abs(eY - mDY) > mTouchSlop)) {
                mIsClickValid = false;
            }
            return mIsClickValid;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (!mIsClickValid || eX < 0 || eX > mWidth || eY < 0 || eY > mHeight) {
                return false;
            }
            toggle();
            return true;
    }
    return super.onTouchEvent(event);
}
 
Example 9
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
	// ...
	double x = event.getX(0) - event.getX(1);
	double y = event.getY(0) - event.getY(1);
	return (float)Math.sqrt(x * x + y * y);

}
 
Example 10
@Override
public boolean onTouchEvent(MotionEvent event) {
    float y = event.getY();
    float x = event.getX();
    final int action = event.getActionMasked();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            gotSlot = isInSelectedSlot(x, y);
            downX = x;
            downY = y;
            break;

        case MotionEvent.ACTION_MOVE:
            if (gotSlot) {
                if (x >= slotPositions[0] && x <= slotPositions[rangeCount - 1]) {
                    currentSlidingX = x;
                    currentSlidingY = y;
                    invalidate();
                }
            }
            break;

        case MotionEvent.ACTION_UP:
            if (gotSlot) {
                gotSlot = false;
                currentSlidingX = x;
                currentSlidingY = y;
                updateCurrentIndex();
            }
            break;
    }
    return true;
}
 
Example 11
/**
 * @param e1 The first down {@link MotionEvent} that started the scrolling.
 * @param e2 The move {@link MotionEvent} that triggered the current scroll.
 * @return Whether the distance is greater than the touch slop threshold.
 */
private boolean isDistanceGreaterThanTouchSlop(MotionEvent e1, MotionEvent e2) {
    float deltaX = e2.getX() - e1.getX();
    float deltaY = e2.getY() - e1.getY();
    // Check if the distance between the events |e1| and |e2| is greater than the touch slop.
    return isDistanceGreaterThanTouchSlop(deltaX, deltaY);
}
 
Example 12
Source Project: UltimateAndroid   File: ResideMenu.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    float currentActivityScaleX = ViewHelper.getScaleX(viewActivity);
    if (currentActivityScaleX == 1.0f)
        setScaleDirectionByRawX(ev.getRawX());

    switch (ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            lastActionDownX = ev.getX();
            lastActionDownY = ev.getY();
            isInIgnoredView = isInIgnoredView(ev) && !isOpened();
            pressedState    = PRESSED_DOWN;
            break;

        case MotionEvent.ACTION_MOVE:
            if (isInIgnoredView || isInDisableDirection(scaleDirection))
                break;

            if(pressedState != PRESSED_DOWN &&
                    pressedState != PRESSED_MOVE_HORIZANTAL)
                break;

            int xOffset = (int) (ev.getX() - lastActionDownX);
            int yOffset = (int) (ev.getY() - lastActionDownY);

            if(pressedState == PRESSED_DOWN) {
                if(yOffset > 25 || yOffset < -25) {
                    pressedState = PRESSED_MOVE_VERTICAL;
                    break;
                }
                if(xOffset < -50 || xOffset > 50) {
                    pressedState = PRESSED_MOVE_HORIZANTAL;
                    ev.setAction(MotionEvent.ACTION_CANCEL);
                }
            } else if(pressedState == PRESSED_MOVE_HORIZANTAL) {
                if (currentActivityScaleX < 0.95)
                    showScrollViewMenu();

                float targetScale = getTargetScale(ev.getRawX());
                ViewHelper.setScaleX(viewActivity, targetScale);
                ViewHelper.setScaleY(viewActivity, targetScale);
                ViewHelper.setScaleX(imageViewShadow, targetScale + shadowAdjustScaleX);
                ViewHelper.setScaleY(imageViewShadow, targetScale + shadowAdjustScaleY);
                ViewHelper.setAlpha(scrollViewMenu, (1 - targetScale) * 2.0f);

                lastRawX = ev.getRawX();
                return true;
            }

            break;

        case MotionEvent.ACTION_UP:

            if (isInIgnoredView) break;
            if (pressedState != PRESSED_MOVE_HORIZANTAL) break;

            pressedState = PRESSED_DONE;
            if (isOpened()){
                if (currentActivityScaleX > 0.56f)
                    closeMenu();
                else
                    openMenu(scaleDirection);
            }else{
                if (currentActivityScaleX < 0.94f){
                    openMenu(scaleDirection);
                }else{
                    closeMenu();
                }
            }

            break;

    }
    lastRawX = ev.getRawX();
    return super.dispatchTouchEvent(ev);
}
 
Example 13
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!isEventInWheel(x, y)) {
                return false;
            }
            mLastX = x;
            mLastY = y;
            mDownTime = System.currentTimeMillis();
            mTmpAngle = 0;
            mIsDraging = false;
            if (mIsFling) {
                removeCallbacks(mFlingRunnable);
                mIsFling = false;
                return true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            /**
             * 获得开始的角度
             */
            final float start = getAngle(mLastX, mLastY);
            /**
             * 获得当前的角度
             */
            final float end = getAngle(x, y);

            // 如果是一、四象限,则直接(end-start)代表角度改变值(正是上移动负是下拉)
            if (getQuadrant(x, y) == 1 || getQuadrant(x, y) == 4) {
                mStartAngle += end - start;
                mTmpAngle += end - start;
            } else
            // 二、三象限,(start - end)代表角度改变值
            {
                mStartAngle += start - end;
                mTmpAngle += start - end;
            }
            mIsDraging = true;
            // 重新布局
            requestLayout();

            mLastX = x;
            mLastY = y;

            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            // 计算,每秒移动的角度
            float anglePerSecond = mTmpAngle * 1000
                    / (System.currentTimeMillis() - mDownTime);
            // 如果达到该值认为是快速移动
            if (Math.abs(anglePerSecond) > mFlingableValue && !mIsFling) {
                mFlingRunnable.startUsingVelocity(anglePerSecond);
                return true;
            }
            mIsFling = false;
            mIsDraging = false;
            mFlingRunnable.stop(false);
            scrollIntoSlots();
            // 如果当前旋转角度超过NOCLICK_VALUE屏蔽点击
            if (Math.abs(mTmpAngle) > NOCLICK_VALUE) {
                return true;
            }

            break;
    }
    return super.dispatchTouchEvent(event);
}
 
Example 14
Source Project: AnchorImageView   File: AnchorImageView.java    License: MIT License 4 votes vote down vote up
protected void disposeEvent(MotionEvent e) {
    if (anchors == null || anchors.size() == 0) {
        Toast.makeText(getContext(), "No Anchor", Toast.LENGTH_LONG).show();
    } else {
        float x = e.getX();
        float y = e.getY();
        if (anchors != null) {
            for (Integer key : anchors.keySet()) {
                Anchor anchor = anchors.get(key);
                if (new RectF(widthRatio * (float) anchor.left, heightRatio * (float) anchor.top,
                        widthRatio * (float) anchor.right, heightRatio * (float) anchor.bottom).contains(x, y)) {
                    // [email protected]@=========
                    this.anchor = anchor;

                    //clean WarnAnchor
                    if (isWarnAnchor) {
                        isWarnAnchor = false;
                        postInvalidate();
                    }
                    //on listener
                    if (onAnchorClickListener != null)
                        onAnchorClickListener.onAnchorClick(anchor, anchor.id, widthRatio, heightRatio);

                    return;
                }
            }
            // [email protected]@=========

            //current click show -> return
            /*if (rectClickAnchor != null && !rectClickAnchor.isEmpty())
                return;*/

            //mark WarnAnchor
            if (isWarnAnchor)
                return;
            isWarnAnchor = true;

            postInvalidate();
        }
    }
}
 
Example 15
Source Project: delion   File: SnackbarView.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isInBounds(MotionEvent ev, View view) {
    return ev.getX() > view.getX() && ev.getX() - view.getX() < view.getWidth()
            && ev.getY() > view.getY() && ev.getY() - view.getY() < view.getHeight();
}
 
Example 16
Source Project: android_9.0.0_r45   File: AbsSeekBar.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!mIsUserSeekable || !isEnabled()) {
        return false;
    }

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (isInScrollingContainer()) {
                mTouchDownX = event.getX();
            } else {
                startDrag(event);
            }
            break;

        case MotionEvent.ACTION_MOVE:
            if (mIsDragging) {
                trackTouchEvent(event);
            } else {
                final float x = event.getX();
                if (Math.abs(x - mTouchDownX) > mScaledTouchSlop) {
                    startDrag(event);
                }
            }
            break;

        case MotionEvent.ACTION_UP:
            if (mIsDragging) {
                trackTouchEvent(event);
                onStopTrackingTouch();
                setPressed(false);
            } else {
                // Touch up when we never crossed the touch slop threshold should
                // be interpreted as a tap-seek to that location.
                onStartTrackingTouch();
                trackTouchEvent(event);
                onStopTrackingTouch();
            }
            // ProgressBar doesn't know to repaint the thumb drawable
            // in its inactive state when the touch stops (because the
            // value has not apparently changed)
            invalidate();
            break;

        case MotionEvent.ACTION_CANCEL:
            if (mIsDragging) {
                onStopTrackingTouch();
                setPressed(false);
            }
            invalidate(); // see above explanation
            break;
    }
    return true;
}
 
Example 17
Source Project: HoloColorPicker   File: SVBar.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
	getParent().requestDisallowInterceptTouchEvent(true);

	// Convert coordinates to our internal coordinate system
	float dimen;
	if (mOrientation == ORIENTATION_HORIZONTAL) {
		dimen = event.getX();
	}
	else {
		dimen = event.getY();
	}

	switch (event.getAction()) {
	case MotionEvent.ACTION_DOWN:
	    	mIsMovingPointer = true;
		// Check whether the user pressed on the pointer
		if (dimen >= (mBarPointerHaloRadius)
				&& dimen <= (mBarPointerHaloRadius + mBarLength)) {
			mBarPointerPosition = Math.round(dimen);
			calculateColor(Math.round(dimen));
			mBarPointerPaint.setColor(mColor);
			invalidate();
		}
		break;
	case MotionEvent.ACTION_MOVE:
		if (mIsMovingPointer) {
			// Move the the pointer on the bar.
			if (dimen >= mBarPointerHaloRadius
					&& dimen <= (mBarPointerHaloRadius + mBarLength)) {
				mBarPointerPosition = Math.round(dimen);
				calculateColor(Math.round(dimen));
				mBarPointerPaint.setColor(mColor);
				if (mPicker != null) {
					mPicker.setNewCenterColor(mColor);
					mPicker.changeOpacityBarColor(mColor);
				}
				invalidate();
			} else if (dimen < mBarPointerHaloRadius) {
				mBarPointerPosition = mBarPointerHaloRadius;
				mColor = Color.WHITE;
				mBarPointerPaint.setColor(mColor);
				if (mPicker != null) {
					mPicker.setNewCenterColor(mColor);
					mPicker.changeOpacityBarColor(mColor);
				}
				invalidate();
			} else if (dimen > (mBarPointerHaloRadius + mBarLength)) {
				mBarPointerPosition = mBarPointerHaloRadius + mBarLength;
				mColor = Color.BLACK;
				mBarPointerPaint.setColor(mColor);
				if (mPicker != null) {
					mPicker.setNewCenterColor(mColor);
					mPicker.changeOpacityBarColor(mColor);
				}
				invalidate();
			}
		}
		break;
	case MotionEvent.ACTION_UP:
		mIsMovingPointer = false;
		break;
	}
	return true;
}
 
Example 18
Source Project: SwipableLayout   File: TouchImageView.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    mScaleDetector.onTouchEvent(event);
    mGestureDetector.onTouchEvent(event);
    PointF curr = new PointF(event.getX(), event.getY());

    if (state == State.NONE || state == State.DRAG || state == State.FLING) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                last.set(curr);
                if (fling != null)
                    fling.cancelFling();
                setState(State.DRAG);
                break;

            case MotionEvent.ACTION_MOVE:
                if (state == State.DRAG) {
                    float deltaX = curr.x - last.x;
                    float deltaY = curr.y - last.y;
                    float fixTransX = getFixDragTrans(deltaX, viewWidth, getImageWidth());
                    float fixTransY = getFixDragTrans(deltaY, viewHeight, getImageHeight());
                    matrix.postTranslate(fixTransX, fixTransY);
                    fixTrans();
                    last.set(curr.x, curr.y);
                }
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                setState(State.NONE);
                break;
        }
    }

    setImageMatrix(matrix);

    //
    // User-defined OnTouchListener
    //
    if (userTouchListener != null) {
        userTouchListener.onTouch(v, event);
    }

    //
    // OnTouchImageViewListener is set: TouchImageView dragged by user.
    //
    if (touchImageViewListener != null) {
        touchImageViewListener.onMove();
    }

    //
    // indicate event was handled
    //
    return true;
}
 
Example 19
Source Project: Klyph   File: ScrollableViewGroup.java    License: MIT License 4 votes vote down vote up
protected final void updatePosition(MotionEvent paramMotionEvent)
{
  this.mLastPosition[0] = paramMotionEvent.getX();
  this.mLastPosition[1] = paramMotionEvent.getY();
}
 
Example 20
Source Project: RulerView   File: MoneySelectRuleView.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getAction();
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    logD("onTouchEvent: action=%d", action);
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mIsMoving = false;
            mDownX = x;
            mDownY = y;
            if (!mScroller.isFinished()) {
                mScroller.forceFinished(true);
            }
            break;
        case MotionEvent.ACTION_MOVE:
            final int dx = x - mLastX;
            if (!mIsMoving) {
                final int dy = y - mLastY;
                if (Math.abs(x - mDownX) <= TOUCH_SLOP || Math.abs(dx) < Math.abs(dy)) {
                    break;
                }
                mIsMoving = true;
            }
            mCurrentDistance -= dx;
            computeValue();
            break;
        case MotionEvent.ACTION_UP:
            if (!mIsMoving) {
                break;
            }
            // 计算速度
            mVelocityTracker.computeCurrentVelocity(1000, MAX_FLING_VELOCITY);
            // 获取当前的水平速度
            int xVelocity = (int) mVelocityTracker.getXVelocity();
            logD("up: xVelocity=%d", xVelocity);
            if (Math.abs(xVelocity) < MIN_FLING_VELOCITY) {
                // 滑动刻度
                scrollToGradation();
            } else {
                // 惯性滑动。
                mScroller.fling((int) mCurrentDistance, 0, -xVelocity, 0, 0, mRangeDistance, 0, 0);
                invalidate();
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            if (!mScroller.isFinished()) {
                // 结束滑动,并把数值立马置为终点值
                mScroller.abortAnimation();
            }
            break;
        default: break;
    }
    mLastX = x;
    mLastY = y;
    return true;
}