List of usage examples for android.view MotionEvent getX
public final float getX(int pointerIndex)
From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java
private void handleCancel(MotionEvent event) { if (DEBUG && mDragging) Log.v(TAG, "** Handle CANCEL"); // We should drop the active target here but it interferes with // moving off the screen in the direction of the navigation bar. At some point we may // want to revisit how we handle this. For now we'll allow a canceled event to // activate the current target. // mActiveTarget = -1; // Drop the active target if canceled. int actionIndex = event.findPointerIndex(mPointerId); actionIndex = actionIndex == -1 ? 0 : actionIndex; switchToState(STATE_FINISH, event.getX(actionIndex), event.getY(actionIndex)); }
From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java
private void onSecondaryPointerUp(MotionEvent ev) { final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = ev.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { // This was our active pointer going up. Choose a new // active pointer and adjust accordingly. // TODO: Make this decision more intelligent. final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastMotionX = ev.getX(newPointerIndex); mLastMotionX2 = ev.getX(newPointerIndex); mLastMotionY = ev.getY(newPointerIndex); mActivePointerId = ev.getPointerId(newPointerIndex); if (mVelocityTracker != null) { mVelocityTracker.clear();/* w ww. ja v a 2 s. com*/ } } }
From source file:cnedu.ustcjd.widget.MultiSlider.java
private int getValue(MotionEvent event, int pointerIndex, Thumb thumb) { final int width = getWidth(); final int available = getAvailable(); int optThumbOffset = getThumbOptOffset(thumb); int x = (int) event.getX(pointerIndex); float scale;// ww w.j a v a 2 s .c o m float progress = mScaleMin; if (isLayoutRtl()) { if (x > width - getPaddingRight()) { scale = 0.0f; } else if (x < getPaddingLeft()) { scale = 1.0f; } else { scale = (float) (available - x + getPaddingLeft() + optThumbOffset) / (float) available; progress = mScaleMin; } } else { if (x < getPaddingLeft()) { scale = 0.0f; } else if (x > width - getPaddingRight()) { scale = 1.0f; } else { scale = (float) (x - getPaddingLeft() - optThumbOffset) / (float) available; progress = mScaleMin; } } progress += scale * getScaleSize(); return Math.round(progress); }
From source file:com.android.launcher2.PagedView.java
protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { /*/* w w w . j av a 2s .co m*/ * Locally do absolute value. mLastMotionX is set to the y value * of the down event. */ final int pointerIndex = ev.findPointerIndex(mActivePointerId); if (pointerIndex == -1) { return; } final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); final int xDiff = (int) Math.abs(x - mLastMotionX); final int yDiff = (int) Math.abs(y - mLastMotionY); final int touchSlop = Math.round(touchSlopScale * mTouchSlop); boolean xPaged = xDiff > mPagingTouchSlop; boolean xMoved = xDiff > touchSlop; boolean yMoved = yDiff > touchSlop; if (xMoved || xPaged || yMoved) { if (mUsePagingTouchSlop ? xPaged : xMoved) { // Scroll if the user moved far enough along the X axis mTouchState = TOUCH_STATE_SCROLLING; mTotalMotionX += Math.abs(mLastMotionX - x); mLastMotionX = x; mLastMotionXRemainder = 0; mTouchX = getScrollX(); mSmoothingTime = System.nanoTime() / NANOTIME_DIV; pageBeginMoving(); } // Either way, cancel any pending longpress cancelCurrentPageLongPress(); } }
From source file:com.example.SmartBoard.DrawingView.java
public boolean onTouchDragEvent(MotionEvent event) { boolean handled = false; JSONObject mObjectTouched;// w w w .ja v a 2s . co m int X; int Y; int pointerId; int actionIndex = event.getActionIndex(); //get coordinates and make object transparent switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: dragFinished = false; //it's the first pointer, so clear all existing pointers data mObjectPointers.clear(); X = (int) event.getX(actionIndex); Y = (int) event.getY(actionIndex); dragX = X; dragY = Y; String objectType = ""; //check if we've touched inside some object mObjectTouched = getTouchedObject(X, Y); if (mObjectTouched == null) { return true; } else { objectType = getObjectType(mObjectTouched); } if (objectType.compareTo("Circle") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); } catch (JSONException e) { e.printStackTrace(); } } else if (objectType.compareTo("Rectangle") == 0) { Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens")); tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height()); try { mObjectTouched.put("dimens", tempRect.flattenToString()); } catch (JSONException e) { } } else if (objectType.compareTo("Line") == 0) { if (lengthOfLine(X, Y, mObjectTouched.optInt("startx"), mObjectTouched.optInt("starty")) < lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy"))) { try { mObjectTouched.put("startx", X); mObjectTouched.put("starty", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy"))); } catch (JSONException e) { e.printStackTrace(); } dragLineStart = true; } else { try { mObjectTouched.put("stopx", X); mObjectTouched.put("stopy", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"), mObjectTouched.optInt("starty"))); } catch (JSONException e) { e.printStackTrace(); } dragLineStart = false; } } else if (objectType.compareTo("Text") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region")); tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y); mObjectTouched.put("region", tempRect.flattenToString()); } catch (JSONException e) { e.printStackTrace(); } } mObjectPointers.put(event.getPointerId(0), mObjectTouched); if (mObjectTouched != null) { mqtt.publishObject(mObjectTouched); } invalidate(); handled = true; break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_MOVE: dragFinished = false; final int pointerCount = event.getPointerCount(); for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) { //some pointer has moved, search it by pointer id pointerId = event.getPointerId(actionIndex); X = (int) event.getX(actionIndex); Y = (int) event.getY(actionIndex); dragX = X; dragY = Y; mObjectTouched = mObjectPointers.get(pointerId); if (mObjectTouched == null) continue; // if null no object was touched so skip if (mObjectTouched.optString("type").compareTo("Circle") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); } catch (JSONException e) { } } else if (mObjectTouched.optString("type").compareTo("Rectangle") == 0) { Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens")); tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height()); try { mObjectTouched.put("dimens", tempRect.flattenToString()); } catch (JSONException e) { } } else if (mObjectTouched.optString("type").compareTo("Text") == 0) { try { mObjectTouched.put("x", X); mObjectTouched.put("y", Y); Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region")); tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y); mObjectTouched.put("region", tempRect.flattenToString()); } catch (JSONException e) { e.printStackTrace(); } } else if (mObjectTouched.optString("type").compareTo("Line") == 0) { if (dragLineStart) { try { mObjectTouched.put("startx", X); mObjectTouched.put("starty", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy"))); //mObjectTouched.put("stopx", tempStopX); // mObjectTouched.put("stopy", tempStopY); } catch (JSONException e) { e.printStackTrace(); } } else { try { mObjectTouched.put("stopx", X); mObjectTouched.put("stopy", Y); mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"), mObjectTouched.optInt("starty"))); //mObjectTouched.put("stopx", tempStopX); // mObjectTouched.put("stopy", tempStopY); } catch (JSONException e) { e.printStackTrace(); } } } if (mObjectTouched != null) { mqtt.publishObject(mObjectTouched); } } invalidate(); handled = true; break; case MotionEvent.ACTION_UP: dragFinished = true; mObjectPointers.clear(); invalidate(); handled = true; break; case MotionEvent.ACTION_CANCEL: handled = true; break; default: // do nothing break; } return super.onTouchEvent(event) || handled; }
From source file:cn.ismartv.tvrecyclerview.widget.helper.ItemTouchHelper.java
private void updateDxDy(MotionEvent ev, int directionFlags, int pointerIndex) { final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); // Calculate the distance moved mDx = x - mInitialTouchX;//w ww. ja v a 2 s . co m mDy = y - mInitialTouchY; if ((directionFlags & LEFT) == 0) { mDx = Math.max(0, mDx); } if ((directionFlags & RIGHT) == 0) { mDx = Math.min(0, mDx); } if ((directionFlags & UP) == 0) { mDy = Math.max(0, mDy); } if ((directionFlags & DOWN) == 0) { mDy = Math.min(0, mDy); } }
From source file:com.aviary.android.feather.sdk.widget.AviaryWorkspace.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); if (!isEnabled()) { return false; // We don't want the events. Let them fall through to the all // apps view. }/*from w w w .ja v a 2 s . c o m*/ if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { return true; } acquireVelocityTrackerAndAddMovement(ev); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_MOVE: { /* * Locally do absolute value. mLastMotionX is set to the y value of the * down event. */ final int pointerIndex = ev.findPointerIndex(mActivePointerId); if (pointerIndex < 0) { // invalid pointer return true; } final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); final int xDiff = (int) Math.abs(x - mLastMotionX); final int yDiff = (int) Math.abs(y - mLastMotionY); final int touchSlop = mTouchSlop; boolean xMoved = xDiff > touchSlop; boolean yMoved = yDiff > touchSlop; mLastMotionX2 = x; if (xMoved || yMoved) { if (xMoved) { // Scroll if the user moved far enough along the X axis mTouchState = TOUCH_STATE_SCROLLING; mLastMotionX = x; mTouchX = getScrollX(); mSmoothingTime = System.nanoTime() / NANOTIME_DIV; enableChildrenCache(mCurrentScreen - 1, mCurrentScreen + 1); } } break; } case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); // Remember location of down touch mLastMotionX = x; mLastMotionX2 = x; mLastMotionY = y; mActivePointerId = ev.getPointerId(0); mAllowLongPress = true; mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: // Release the drag clearChildrenCache(); mTouchState = TOUCH_STATE_REST; mActivePointerId = INVALID_POINTER; mAllowLongPress = false; releaseVelocityTracker(); break; case MotionEvent.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; } /* * The only time we want to intercept motion events is if we are in the drag mode. */ return mTouchState != TOUCH_STATE_REST; }
From source file:com.android.launcher3.ItemTouchHelper.java
void updateDxDy(MotionEvent ev, int directionFlags, int pointerIndex) { final float x = ev.getX(pointerIndex); final float y = ev.getY(pointerIndex); // Calculate the distance moved mDx = x - mInitialTouchX;/*from w w w. j a v a 2s . c o m*/ mDy = y - mInitialTouchY; if ((directionFlags & LEFT) == 0) { mDx = Math.max(0, mDx); } if ((directionFlags & RIGHT) == 0) { mDx = Math.min(0, mDx); } if ((directionFlags & UP) == 0) { mDy = Math.max(0, mDy); } if ((directionFlags & DOWN) == 0) { mDy = Math.min(0, mDy); } }
From source file:cn.ismartv.tvrecyclerview.widget.helper.ItemTouchHelper.java
private ViewHolder findSwipedView(MotionEvent motionEvent) { final RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); if (mActivePointerId == ACTIVE_POINTER_ID_NONE) { return null; }/* www . j ava2s .c o m*/ final int pointerIndex = motionEvent.findPointerIndex(mActivePointerId); final float dx = motionEvent.getX(pointerIndex) - mInitialTouchX; final float dy = motionEvent.getY(pointerIndex) - mInitialTouchY; final float absDx = Math.abs(dx); final float absDy = Math.abs(dy); if (absDx < mSlop && absDy < mSlop) { return null; } if (absDx > absDy && lm.canScrollHorizontally()) { return null; } else if (absDy > absDx && lm.canScrollVertically()) { return null; } View child = findChildView(motionEvent); if (child == null) { return null; } return mRecyclerView.getChildViewHolder(child); }
From source file:com.android.launcher3.ItemTouchHelper.java
private android.support.v7.widget.RecyclerView.ViewHolder findSwipedView(MotionEvent motionEvent) { final android.support.v7.widget.RecyclerView.LayoutManager lm = mRecyclerView.getLayoutManager(); if (mActivePointerId == ACTIVE_POINTER_ID_NONE) { return null; }/*from ww w . j a v a 2 s.com*/ final int pointerIndex = motionEvent.findPointerIndex(mActivePointerId); final float dx = motionEvent.getX(pointerIndex) - mInitialTouchX; final float dy = motionEvent.getY(pointerIndex) - mInitialTouchY; final float absDx = Math.abs(dx); final float absDy = Math.abs(dy); if (absDx < mSlop && absDy < mSlop) { return null; } if (absDx > absDy && lm.canScrollHorizontally()) { return null; } else if (absDy > absDx && lm.canScrollVertically()) { return null; } View child = findChildView(motionEvent); if (child == null) { return null; } return mRecyclerView.getChildViewHolder(child); }