// @Override public boolean dispatchTouchEvent (MotionEvent event) { public boolean gotTouchEvent(MotionEvent event) { super.onTouchEvent(event); int evcount = event.getPointerCount(); if (evcount == 0) return true; int[] arr = new int[event.getPointerCount() * 3]; for (int i = 0; i < event.getPointerCount(); i++) { arr[i * 3 + 0] = (int) event.getPointerId(i); arr[i * 3 + 1] = (int) event.getX(i); arr[i * 3 + 2] = (int) event.getY(i); } // System.out.printf("gaction: %d\n",event.getAction()); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { GodotLib.touch(0, 0, evcount, arr); // System.out.printf("action down at: %f,%f\n", event.getX(),event.getY()); } break; case MotionEvent.ACTION_MOVE: { GodotLib.touch(1, 0, evcount, arr); // for(int i=0;i<event.getPointerCount();i++) { // System.out.printf("%d - moved to: %f,%f\n",i, event.getX(i),event.getY(i)); // } } break; case MotionEvent.ACTION_POINTER_UP: { int pointer_idx = event.getActionIndex(); GodotLib.touch(4, pointer_idx, evcount, arr); // System.out.printf("%d - s.up at: %f,%f\n",pointer_idx, // event.getX(pointer_idx),event.getY(pointer_idx)); } break; case MotionEvent.ACTION_POINTER_DOWN: { int pointer_idx = event.getActionIndex(); GodotLib.touch(3, pointer_idx, evcount, arr); // System.out.printf("%d - s.down at: %f,%f\n",pointer_idx, // event.getX(pointer_idx),event.getY(pointer_idx)); } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: { GodotLib.touch(2, 0, evcount, arr); // for(int i=0;i<event.getPointerCount();i++) { // System.out.printf("%d - up! %f,%f\n",i, event.getX(i),event.getY(i)); // } } break; } return true; }
@Override public boolean handleMotionEvent(MotionEvent event) { if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) { int actionPointerIndex = event.getActionIndex(); int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_MOVE: SDLJoystick joystick = getJoystick(event.getDeviceId()); if (joystick != null) { for (int i = 0; i < joystick.axes.size(); i++) { InputDevice.MotionRange range = joystick.axes.get(i); /* Normalize the value to -1...1 */ float value = (event.getAxisValue(range.getAxis(), actionPointerIndex) - range.getMin()) / range.getRange() * 2.0f - 1.0f; SDLActivity.onNativeJoy(joystick.device_id, i, value); } } break; default: break; } } return true; }
@Override public boolean onTouchEvent(MotionEvent event) { if (controlling) { velocityTracker.addMovement(event); velocityTracker.computeCurrentVelocity(1); boolean down = event.getActionMasked() == MotionEvent.ACTION_DOWN || event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN; boolean up = event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP; Finger[] fingers = new Finger[event.getPointerCount()]; for (int i = 0; i < event.getPointerCount(); i++) { fingers[i] = new Finger( event.getPointerId(i), event.getX(i), event.getY(i), velocityTracker.getXVelocity(i), velocityTracker.getYVelocity(i), !(event.getActionIndex() == i && up)); } FingerMessage<?> message = up ? new UpMessage(fingers) : down ? new DownMessage(fingers) : new MoveMessage(fingers); communicator.sendMessage(message); } return super.onTouchEvent(event); }
public boolean onTouch(View v, MotionEvent event) { GiderosApplication app = GiderosApplication.getInstance(); if (app == null) return false; int size = event.getPointerCount(); for (int i = 0; i < size; i++) { id[i] = event.getPointerId(i); x[i] = (int) event.getX(i); y[i] = (int) event.getY(i); } int actionMasked = event.getActionMasked(); boolean isPointer = (actionMasked == MotionEvent.ACTION_POINTER_DOWN || actionMasked == MotionEvent.ACTION_POINTER_UP); int actionIndex = isPointer ? event.getActionIndex() : 0; if (actionMasked == MotionEvent.ACTION_DOWN || actionMasked == MotionEvent.ACTION_POINTER_DOWN) { app.onTouchesBegin(size, id, x, y, actionIndex); } else if (actionMasked == MotionEvent.ACTION_MOVE) { app.onTouchesMove(size, id, x, y); } else if (actionMasked == MotionEvent.ACTION_UP || actionMasked == MotionEvent.ACTION_POINTER_UP) { app.onTouchesEnd(size, id, x, y, actionIndex); } else if (actionMasked == MotionEvent.ACTION_CANCEL) { app.onTouchesCancel(size, id, x, y); } return true; }
private void handleUp(MotionEvent event) { if (DEBUG && mDragging) Log.v(TAG, "** Handle RELEASE"); int actionIndex = event.getActionIndex(); if (event.getPointerId(actionIndex) == mPointerId) { switchToState(STATE_FINISH, event.getX(actionIndex), event.getY(actionIndex)); } }
/** * Extracts the touch point data from a MotionEvent, converts each point into a marshallable * object and passes the set of points to the JNI layer to be transmitted to the remote host. * * @param event The event to send to the remote host for injection. NOTE: This object must be * updated to represent the remote machine's coordinate system before calling this function. */ public void sendTouchEvent(MotionEvent event) { int action = event.getActionMasked(); TouchEventData.EventType touchEventType = TouchEventData.EventType.fromMaskedAction(action); List<TouchEventData> touchEventList = new ArrayList<TouchEventData>(); if (action == MotionEvent.ACTION_MOVE) { // In order to process all of the events associated with an ACTION_MOVE event, we need // to walk the list of historical events in order and add each event to our list, then // retrieve the current move event data. int pointerCount = event.getPointerCount(); int historySize = event.getHistorySize(); for (int h = 0; h < historySize; ++h) { for (int p = 0; p < pointerCount; ++p) { touchEventList.add( new TouchEventData( event.getPointerId(p), event.getHistoricalX(p, h), event.getHistoricalY(p, h), event.getHistoricalSize(p, h), event.getHistoricalSize(p, h), event.getHistoricalOrientation(p, h), event.getHistoricalPressure(p, h))); } } for (int p = 0; p < pointerCount; p++) { touchEventList.add( new TouchEventData( event.getPointerId(p), event.getX(p), event.getY(p), event.getSize(p), event.getSize(p), event.getOrientation(p), event.getPressure(p))); } } else { // For all other events, we only want to grab the current/active pointer. The event // contains a list of every active pointer but passing all of of these to the host can // cause confusion on the remote OS side and result in broken touch gestures. int activePointerIndex = event.getActionIndex(); touchEventList.add( new TouchEventData( event.getPointerId(activePointerIndex), event.getX(activePointerIndex), event.getY(activePointerIndex), event.getSize(activePointerIndex), event.getSize(activePointerIndex), event.getOrientation(activePointerIndex), event.getPressure(activePointerIndex))); } if (!touchEventList.isEmpty()) { mInjector.sendTouchEvent(touchEventType, touchEventList.toArray(new TouchEventData[0])); } }
public boolean onTouchEvent(final MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { final int index = event.getActionIndex(); isInTouchSequence = true; mainPointerId = event.getPointerId(event.getActionIndex()); nativeTouchDown(event.getX(index), event.getY(index)); break; } case MotionEvent.ACTION_MOVE: { if (!isInTouchSequence) { return false; } final int index = event.findPointerIndex(mainPointerId); if (index != -1) { nativeTouchMove(event.getX(index), event.getY(index)); } break; } case MotionEvent.ACTION_UP: // Fall through case MotionEvent.ACTION_CANCEL: { final int index = event.findPointerIndex(mainPointerId); nativeTouchUp(event.getX(index), event.getY(index)); isInTouchSequence = false; mainPointerId = -1; break; } case MotionEvent.ACTION_POINTER_DOWN: { return false; } case MotionEvent.ACTION_POINTER_UP: { return false; } default: } return true; }
@Override public boolean onTouch(View v, MotionEvent event) { // react on touch events // get pointer index from the event object int pointerIndex = event.getActionIndex(); // get pointer ID int pointerId = event.getPointerId(pointerIndex); // get masked (not specific to a pointer) action int maskedAction = event.getActionMasked(); MainActivity.debug("WidgetView: maskedAction = " + maskedAction); switch (maskedAction) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: { motionDown = true; downX = event.getX(); downY = event.getY(); break; } case MotionEvent.ACTION_MOVE: { if (Math.abs(downX - event.getX()) + Math.abs(downY - event.getY()) > 20) { motionMove = true; } break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: { if (!motionMove && clickable && MainActivity.isSafe()) { Intent intent = new Intent(this.getContext(), WidgetActivity.class); selectedDrawable = this.getDrawable(); this.getContext().startActivity(intent); } motionDown = false; motionMove = false; break; } case MotionEvent.ACTION_CANCEL: { break; } } invalidate(); return true; }
// returns the WebTouchEvent::Type for the MotionEvent and -1 for failure public static int createTouchPoints(MotionEvent event, TouchPoint[] pts) { int type; int defaultState; switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: type = TOUCH_EVENT_TYPE_START; defaultState = TOUCH_POINT_STATE_PRESSED; break; case MotionEvent.ACTION_MOVE: type = TOUCH_EVENT_TYPE_MOVE; defaultState = TOUCH_POINT_STATE_MOVED; break; case MotionEvent.ACTION_UP: type = TOUCH_EVENT_TYPE_END; defaultState = TOUCH_POINT_STATE_RELEASED; break; case MotionEvent.ACTION_CANCEL: type = TOUCH_EVENT_TYPE_CANCEL; defaultState = TOUCH_POINT_STATE_CANCELLED; break; case MotionEvent.ACTION_POINTER_DOWN: // fall through. case MotionEvent.ACTION_POINTER_UP: type = TOUCH_EVENT_TYPE_MOVE; defaultState = TOUCH_POINT_STATE_STATIONARY; break; default: Log.e("Chromium", "Unknown motion event action: " + event.getActionMasked()); return CONVERSION_ERROR; } for (int i = 0; i < pts.length; ++i) { int state = defaultState; if (defaultState == TOUCH_POINT_STATE_STATIONARY && event.getActionIndex() == i) { // An additional pointer has started or ended. Map this pointer state as // required, and all other pointers as "stationary". state = event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN ? TOUCH_POINT_STATE_PRESSED : TOUCH_POINT_STATE_RELEASED; } pts[i] = new TouchPoint( state, event.getPointerId(i), event.getX(i), event.getY(i), event.getSize(i), event.getPressure(i)); } return type; }
private void handleDown(MotionEvent event) { int actionIndex = event.getActionIndex(); float eventX = event.getX(actionIndex); float eventY = event.getY(actionIndex); switchToState(STATE_START, eventX, eventY); if (!trySwitchToFirstTouchState(eventX, eventY)) { mDragging = false; } else { mPointerId = event.getPointerId(actionIndex); updateGlowPosition(eventX, eventY); } }
private void onPointerUp(MotionEvent ev) { final int pointerIndex = ev.getActionIndex(); final int pointerId = ev.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastMotionX = ev.getX(newPointerIndex); mActivePointerId = ev.getPointerId(newPointerIndex); if (mVelocityTracker != null) { mVelocityTracker.clear(); } } }
@Override public boolean onTouchEvent(MotionEvent event) { int pointer = event.getPointerCount(); int index = event.getActionIndex(); int id = event.getPointerId(index); Log.e("Touch", "pointer = " + pointer + " index = " + index + " id = " + id); switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.e("Touch", "ACTION_DOWN"); touch_start(event.getX(), event.getY()); break; case MotionEvent.ACTION_POINTER_DOWN: Log.e("Touch", "ACTION_POINTER_DOWN"); if (id == 0) { touch_start(event.getX(0), event.getY(0)); } else { touch2_start(event.getX(1), event.getY(1)); } break; case MotionEvent.ACTION_MOVE: Log.e("Touch", "ACTION_MOVE"); if (pointer == 2) { Log.e( "Touch", "x1 = " + event.getX(0) + " y1 = " + event.getY(0) + " x2 = " + event.getX(1) + " y2 = " + event.getY(1)); touch_move(event.getX(0), event.getY(0)); touch2_move(event.getX(1), event.getY(1)); } else { touch_move(event.getX(), event.getY()); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: if (id == 0) { touch_up(); } else { touch2_up(); } break; default: Log.e("Touch", "UNKNOWN"); break; } this.invalidate(); return true; }
/* #Public Methods */ public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: ptrID1 = event.getPointerId(event.getActionIndex()); break; case MotionEvent.ACTION_POINTER_DOWN: ptrID2 = event.getPointerId(event.getActionIndex()); sX = event.getX(event.findPointerIndex(ptrID1)); sY = event.getY(event.findPointerIndex(ptrID1)); fX = event.getX(event.findPointerIndex(ptrID2)); fY = event.getY(event.findPointerIndex(ptrID2)); break; case MotionEvent.ACTION_MOVE: if (ptrID1 != INVALID_POINTER_ID && ptrID2 != INVALID_POINTER_ID) { float nfX, nfY, nsX, nsY; nsX = event.getX(event.findPointerIndex(ptrID1)); nsY = event.getY(event.findPointerIndex(ptrID1)); nfX = event.getX(event.findPointerIndex(ptrID2)); nfY = event.getY(event.findPointerIndex(ptrID2)); mAngle = angleBetweenLines(fX, fY, sX, sY, nfX, nfY, nsX, nsY); if (mListener != null) { mListener.OnRotation(this); } } break; case MotionEvent.ACTION_UP: ptrID1 = INVALID_POINTER_ID; break; case MotionEvent.ACTION_POINTER_UP: ptrID2 = INVALID_POINTER_ID; break; case MotionEvent.ACTION_CANCEL: ptrID1 = INVALID_POINTER_ID; ptrID2 = INVALID_POINTER_ID; break; } return true; }
public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: sX = event.getX(); sY = event.getY(); ptrID1 = event.getPointerId(0); mAngle = 0; firstTouch = true; break; case MotionEvent.ACTION_POINTER_DOWN: fX = event.getX(); fY = event.getY(); focalX = getMidpoint(fX, sX); focalY = getMidpoint(fY, sY); ptrID2 = event.getPointerId(event.getActionIndex()); mAngle = 0; firstTouch = true; break; case MotionEvent.ACTION_MOVE: if (ptrID1 != INVALID_POINTER_ID && ptrID2 != INVALID_POINTER_ID) { float nfX, nfY, nsX, nsY; nsX = event.getX(event.findPointerIndex(ptrID1)); nsY = event.getY(event.findPointerIndex(ptrID1)); nfX = event.getX(event.findPointerIndex(ptrID2)); nfY = event.getY(event.findPointerIndex(ptrID2)); if (firstTouch) { mAngle = 0; firstTouch = false; } else { mAngle = angleBetweenLines(fX, fY, sX, sY, nfX, nfY, nsX, nsY); } if (mListener != null) { mListener.OnRotation(this); } fX = nfX; fY = nfY; sX = nsX; sY = nsY; } break; case MotionEvent.ACTION_UP: ptrID1 = INVALID_POINTER_ID; break; case MotionEvent.ACTION_POINTER_UP: ptrID2 = INVALID_POINTER_ID; break; } return true; }
/** 处理我们拖动ListView item的逻辑 */ @Override public boolean onTouchEvent(MotionEvent ev) { if (isSlide && slidePosition != AdapterView.INVALID_POSITION) { System.out.println("touch-->" + "开始"); requestDisallowInterceptTouchEvent(true); addVelocityTracker(ev); final int action = ev.getAction(); int x = (int) ev.getX(); switch (action) { case MotionEvent.ACTION_DOWN: System.out.println("touch-->" + "down"); break; case MotionEvent.ACTION_MOVE: System.out.println("touch-->" + "move"); MotionEvent cancelEvent = MotionEvent.obtain(ev); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (ev.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); onTouchEvent(cancelEvent); int deltaX = downX - x; // 手指拖动itemView滚动, deltaX大于0向左滚动,小于0向右滚 itemView.scrollTo(deltaX, 0); // 根据手指滑动的距离,调整透明度 itemView.setAlpha(1f - Math.abs((float) deltaX / screenWidth)); return true; // 拖动的时候ListView不滚动 case MotionEvent.ACTION_UP: System.out.println("touch-->" + "up"); // 手指离开的时候就不响应左右滚动 isSlide = false; int velocityX = getScrollVelocity(); if (velocityX > SNAP_VELOCITY) { scrollRight(); } else if (velocityX < -SNAP_VELOCITY) { scrollLeft(); } else { scrollByDistanceX(); } recycleVelocityTracker(); break; } } // 否则直接交给ListView来处理onTouchEvent事件 return super.onTouchEvent(ev); }
@Override public boolean onTouch(View view, MotionEvent event) { final int count = event.getPointerCount(); if (count == 0) return false; int action = event.getActionMasked(); int pointerIndex = event.getActionIndex(); switch (action) { case MotionEvent.ACTION_POINTER_UP: action = NATIVE_ACTION_UP; break; case MotionEvent.ACTION_UP: action = NATIVE_ACTION_UP; pointerIndex = 0; break; case MotionEvent.ACTION_POINTER_DOWN: action = NATIVE_ACTION_DOWN; break; case MotionEvent.ACTION_DOWN: action = NATIVE_ACTION_DOWN; pointerIndex = 0; break; case MotionEvent.ACTION_MOVE: action = NATIVE_ACTION_MOVE; pointerIndex = INVALID_POINTER_MASK; break; case MotionEvent.ACTION_CANCEL: action = NATIVE_ACTION_CANCEL; break; } switch (count) { case 1: nativeOnTouch( action, event.getPointerId(0), event.getX(), event.getY(), INVALID_TOUCH_ID, 0, 0, 0); return true; default: nativeOnTouch( action, event.getPointerId(0), event.getX(0), event.getY(0), event.getPointerId(1), event.getX(1), event.getY(1), pointerIndex); return true; } }
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = tempX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX(); int deltaX = tempX - moveX; tempX = moveX; if (Math.abs(moveX - downX) > mTouchSlop && Math.abs((int) event.getRawY() - downY) < mTouchSlop) { isSilding = true; if (isTouchOnAbsListView()) { MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); v.onTouchEvent(cancelEvent); } } if (moveX - downX >= 0 && isSilding) { mParentView.scrollBy(deltaX, 0); if (isTouchOnScrollView() || isTouchOnAbsListView()) { return true; } } break; case MotionEvent.ACTION_UP: isSilding = false; if (mParentView.getScrollX() <= -viewWidth / 2) { isFinish = true; scrollRight(); } else { scrollOrigin(); isFinish = false; } break; } if (isTouchOnScrollView() || isTouchOnAbsListView()) { return v.onTouchEvent(event); } return true; }
@Override public boolean onInterceptTouchEvent(MotionEvent event) { if (mTopCard == null) { return false; } if (mGestureDetector.onTouchEvent(event)) { return true; } final int pointerIndex; final float x, y; final float dx, dy; switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: mTopCard.getHitRect(childRect); CardModel cardModel = (CardModel) getAdapter().getItem(getChildCount() - 1); if (cardModel.getOnClickListener() != null) { cardModel.getOnClickListener().OnClickListener(); } pointerIndex = event.getActionIndex(); x = event.getX(pointerIndex); y = event.getY(pointerIndex); if (!childRect.contains((int) x, (int) y)) { return false; } mLastTouchX = x; mLastTouchY = y; mActivePointerId = event.getPointerId(pointerIndex); break; case MotionEvent.ACTION_MOVE: pointerIndex = event.findPointerIndex(mActivePointerId); x = event.getX(pointerIndex); y = event.getY(pointerIndex); if (Math.abs(x - mLastTouchX) > mTouchSlop || Math.abs(y - mLastTouchY) > mTouchSlop) { float[] points = new float[] {x - mTopCard.getLeft(), y - mTopCard.getTop()}; mTopCard.getMatrix().invert(mMatrix); mMatrix.mapPoints(points); mTopCard.setPivotX(points[0]); mTopCard.setPivotY(points[1]); return true; } } return false; }
@Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getActionIndex() != 0) { return super.onTouchEvent(ev); } final int action = ev.getAction(); final int x = (int) ev.getX(0); final int y = (int) ev.getY(0); if (action == MotionEvent.ACTION_DOWN && x > getWidth() - 80) { // drag on the right part of the item only mDragMode = true; } if (!mDragMode) { return super.onTouchEvent(ev); } switch (action) { case MotionEvent.ACTION_DOWN: mStartPosition = pointToPosition(x, y); if (mStartPosition != INVALID_POSITION) { int mItemPosition = mStartPosition - getFirstVisiblePosition(); mDragPointOffset = y - getChildAt(mItemPosition).getTop(); mDragPointOffset -= ((int) ev.getRawY()) - y; startDrag(mItemPosition, y); drag(0, y); // replace 0 with x if desired } break; case MotionEvent.ACTION_MOVE: drag(0, y); // replace 0 with x if desired break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: default: mDragMode = false; int endPosition = pointToPosition(x, y); stopDrag(mStartPosition - getFirstVisiblePosition()); if (mDragNDropListener != null && mStartPosition != INVALID_POSITION && endPosition != INVALID_POSITION) mDragNDropListener.onDrop(mStartPosition, endPosition); break; } return ev.getPointerCount() <= 1 || super.onTouchEvent(ev); }
@Override public boolean onTouch(View v, MotionEvent ev) { requestDisallowInterceptTouchEvent(true); addVelocityTracker(ev); final int action = ev.getAction(); int y = (int) ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: MotionEvent cancelEvent = MotionEvent.obtain(ev); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (ev.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); onTouchEvent(cancelEvent); int deltaY = downY - y; downY = y; Log.e("move_distance", String.valueOf(deltaY)); // 手指拖动this滚动, deltaX大于0向左滚动,小于0向右滚 this.scrollBy(0, deltaY); return true; // 拖动的时候ListView不滚动 case MotionEvent.ACTION_UP: // int velocityY = getScrollVelocity(); // if (velocityY > SNAP_VELOCITY) { // scrollDOWN(); // } else if (velocityY < -SNAP_VELOCITY) { // scrollUP(); // } else { // scrollByDistanceX(); // } // // recycleVelocityTracker(); // 手指离开的时候就不响应左右滚动 scrollByDistanceX(); isSlide = false; break; } // 否则直接交给ListView来处理onTouchEvent事件 return super.onTouchEvent(ev); }
// Touch events @Override public boolean onTouch(View v, MotionEvent event) { /* Ref: http://developer.android.com/training/gestures/multi.html */ final int touchDevId = event.getDeviceId(); final int pointerCount = event.getPointerCount(); int action = event.getActionMasked(); int pointerFingerId; int i = -1; float x, y, p; switch (action) { case MotionEvent.ACTION_MOVE: for (i = 0; i < pointerCount; i++) { pointerFingerId = event.getPointerId(i); x = event.getX(i) / mWidth; y = event.getY(i) / mHeight; p = event.getPressure(i); SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_DOWN: // Primary pointer up/down, the index is always zero i = 0; case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_POINTER_DOWN: // Non primary pointer up/down if (i == -1) { i = event.getActionIndex(); } pointerFingerId = event.getPointerId(i); x = event.getX(i) / mWidth; y = event.getY(i) / mHeight; p = event.getPressure(i); SDLActivity.onNativeTouch(touchDevId, pointerFingerId, action, x, y, p); break; default: break; } return true; }
private boolean isEventOverChild(MotionEvent ev, List<View> children) { final int actionIndex = ev.getActionIndex(); final float x = ev.getX(actionIndex) + getScrollX(); final float y = ev.getY(actionIndex) + getScrollY(); for (View child : children) { if (!canViewReceivePointerEvents(child)) { continue; } child.getHitRect(sHitFrame); // child can receive the motion event. if (sHitFrame.contains((int) x, (int) y)) { return true; } } return false; }
@Override public void onPointerEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: mSwipeFireable = true; mDebugFireable = true; mDownPointers = 0; captureDown(event, 0); break; case MotionEvent.ACTION_POINTER_DOWN: captureDown(event, event.getActionIndex()); if (mDebugFireable) { mDebugFireable = event.getPointerCount() < 5; if (!mDebugFireable) { if (DEBUG) Slog.d(TAG, "Firing debug"); mCallbacks.onDebug(); } } break; case MotionEvent.ACTION_MOVE: if (mSwipeFireable) { final int swipe = detectSwipe(event); mSwipeFireable = swipe == SWIPE_NONE; if (swipe == SWIPE_FROM_TOP) { if (DEBUG) Slog.d(TAG, "Firing onSwipeFromTop"); mCallbacks.onSwipeFromTop(); } else if (swipe == SWIPE_FROM_BOTTOM) { if (DEBUG) Slog.d(TAG, "Firing onSwipeFromBottom"); mCallbacks.onSwipeFromBottom(); } else if (swipe == SWIPE_FROM_RIGHT) { if (DEBUG) Slog.d(TAG, "Firing onSwipeFromRight"); mCallbacks.onSwipeFromRight(); } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mSwipeFireable = false; mDebugFireable = false; break; default: if (DEBUG) Slog.d(TAG, "Ignoring " + event); } }
@Override public boolean onMotionEvent(MotionEvent event, float offset) { if (position == null) { return false; } // Get the pointer ID int activePointerIndex = event.getActionIndex(); int activePointerId = event.getPointerId(activePointerIndex); float x, y; switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: x = event.getX(activePointerIndex); y = event.getY(activePointerIndex); // if the current touch is inside the left click button if (rectWithOffset(position, offset).contains((int) x, (int) y)) { pressedId = activePointerId; return true; } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: if (activePointerId == pressedId) { pressedId = -1; if (PresentationController.getInstance(context).isStarted()) { PresentationController.getInstance(context).stop(); } else { // show dialog to select time TimePickerDialog dialog = new TimePickerDialog(context, timeSetListener, 0, 10, true); dialog.setTitle("Presentation duration in hours and minutes"); dialog.show(); } return true; } break; } return false; }
// @Override public boolean onTouchEvent__disabled(MotionEvent event) { // react on touch events // get pointer index from the event object int pointerIndex = event.getActionIndex(); // get pointer ID int pointerId = event.getPointerId(pointerIndex); // get masked (not specific to a pointer) action int maskedAction = event.getActionMasked(); switch (maskedAction) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: { if (clickable && MainActivity.isSafe()) { Intent intent = new Intent(this.getContext(), WidgetActivity.class); selectedDrawable = this.getDrawable(); this.getContext().startActivity(intent); } break; } case MotionEvent.ACTION_MOVE: { break; } /*case MotionEvent.ACTION_MOVE: { // a pointer was moved break; }*/ case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: { break; } } invalidate(); return true; }
@Override public boolean onTouchEvent(MotionEvent event) { // TODO(abarth): This version check might not be effective in some // versions of Android that statically compile code and will be upset // at the lack of |requestUnbufferedDispatch|. Instead, we should factor // version-dependent code into separate classes for each supported // version and dispatch dynamically. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { requestUnbufferedDispatch(event); } ArrayList<Pointer> pointers = new ArrayList<Pointer>(); // TODO(abarth): Rather than unpacking these events here, we should // probably send them in one packet to the engine. int maskedAction = event.getActionMasked(); // ACTION_UP, ACTION_POINTER_UP, ACTION_DOWN, and ACTION_POINTER_DOWN // only apply to a single pointer, other events apply to all pointers. if (maskedAction == MotionEvent.ACTION_UP || maskedAction == MotionEvent.ACTION_POINTER_UP || maskedAction == MotionEvent.ACTION_DOWN || maskedAction == MotionEvent.ACTION_POINTER_DOWN) { addPointerForIndex(event, event.getActionIndex(), pointers); } else { // ACTION_MOVE may not actually mean all pointers have moved // but it's the responsibility of a later part of the system to // ignore 0-deltas if desired. for (int p = 0; p < event.getPointerCount(); p++) { addPointerForIndex(event, p, pointers); } } PointerPacket packet = new PointerPacket(); packet.pointers = pointers.toArray(new Pointer[0]); mSkyEngine.onPointerPacket(packet); return true; }
@Override public boolean onMotionEvent(MotionEvent event, float offset) { if (leftClickSize == null) { return false; } // Get the pointer ID int activePointerIndex = event.getActionIndex(); int activePointerId = event.getPointerId(activePointerIndex); float x, y; switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: x = event.getX(activePointerIndex); y = event.getY(activePointerIndex); // if the current touch is inside the left click button if (rectWithOffset(leftClickSize, offset).contains(x, y)) { isLeftClickPressed = true; leftClickTriggeredById = activePointerId; onLeftClickDown(); return true; } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: // if it's the touch that triggered the left/right click if (activePointerId == leftClickTriggeredById) { isLeftClickPressed = false; leftClickTriggeredById = -1; onLeftClickUp(); return true; } break; } return false; }
private boolean move(MotionEvent motionEvent) { if (mSwipeView == null || mVelocityTracker == null || mPaused) { return false; } 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; mSwipeView.setPressed(false); MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mSwipeView.onTouchEvent(cancelEvent); } // Prevent swipes to disallowed directions if ((deltaX < 0 && mAllowedSwipeDirection == SwipeDirection.RIGHT) || (deltaX > 0 && mAllowedSwipeDirection == SwipeDirection.LEFT)) { resetMotion(); return false; } if (mSwiping) { mCallbacks.onTouchDown(); mTranslationX = deltaX; mSwipeView.setTranslationX(deltaX - mSwipingSlop); mSwipeView.setAlpha(Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } return false; }
public static void velocityTrackerPointerUpCleanUpIfNecessary( MotionEvent ev, VelocityTracker tracker) { // Check the dot product of current velocities. // If the pointer that left was opposing another velocity vector, clear. tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); final int upIndex = ev.getActionIndex(); final int id1 = ev.getPointerId(upIndex); final float x1 = tracker.getXVelocity(id1); final float y1 = tracker.getYVelocity(id1); for (int i = 0, count = ev.getPointerCount(); i < count; i++) { if (i == upIndex) continue; final int id2 = ev.getPointerId(i); final float x = x1 * tracker.getXVelocity(id2); final float y = y1 * tracker.getYVelocity(id2); final float dot = x + y; if (dot < 0) { tracker.clear(); break; } } }
private boolean interceptTouchEvent(MotionEvent ev) { /* * This method JUST determines whether we want to intercept the motion. If we * return true, onMotionEvent will be called and we do the actual scrolling there. */ /* * Shortcut the most recurring case: the user is in the dragging state and he is * moving his finger. We want to intercept this motion. */ final int action = ev.getAction(); if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) { return true; } switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_MOVE: { /* * mIsBeingDragged == false, otherwise the shortcut would have caught it. * Check whether the user has moved far enough from his original down touch. */ /* * Locally do absolute value. mLastMotionX is set to the x value of the down * event. */ final int activePointerId = mActivePointerId; if (activePointerId == INVALID_POINTER) { // If we don't have a valid id, the touch down wasn't on content. break; } final int pointerIndex = ev.findPointerIndex(activePointerId); if (pointerIndex == -1) { Log.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent"); break; } final int x = (int) ev.getX(pointerIndex); final int xDiff = (int) Math.abs(x - mLastMotionX); if (xDiff > mTouchSlop) { setHorizontalScrollBarEnabled(true); mIsBeingDragged = true; mLastMotionX = x; initVelocityTrackerIfNotExists(); mVelocityTracker.addMovement(ev); if (getParent() != null) getParent().requestDisallowInterceptTouchEvent(true); } break; } case MotionEvent.ACTION_DOWN: { final int x = (int) ev.getX(); if (!inChild((int) x, (int) ev.getY())) { mIsBeingDragged = false; recycleVelocityTracker(); break; } /* * Remember location of down touch. ACTION_DOWN always refers to pointer index * 0. */ mLastMotionX = x; mActivePointerId = ev.getPointerId(0); initOrResetVelocityTracker(); mVelocityTracker.addMovement(ev); /* * If being flinged and user touches the screen, initiate drag; otherwise * don't. mScroller.isFinished should be false when being flinged. */ mIsBeingDragged = false; break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: /* Release the drag */ mIsBeingDragged = false; mActivePointerId = INVALID_POINTER; if (mScroller.springBack(getScrollX(), getScrollY(), 0, getScrollRange(), 0, 0)) { postInvalidateOnAnimation(); } break; case MotionEvent.ACTION_POINTER_DOWN: { final int index = ev.getActionIndex(); mLastMotionX = (int) ev.getX(index); mActivePointerId = ev.getPointerId(index); break; } case MotionEvent.ACTION_POINTER_UP: onSecondaryPointerUp(ev); mLastMotionX = (int) ev.getX(ev.findPointerIndex(mActivePointerId)); break; } /* * The only time we want to intercept motion events is if we are in the drag mode. */ return mIsBeingDragged; }