Java Code Examples for android.view.MotionEvent#getActionIndex()
The following examples show how to use
android.view.MotionEvent#getActionIndex() .
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 check out the related API usage on the sidebar.
Example 1
Source File: PlatformActivity.java From FirefoxReality with Mozilla Public License 2.0 | 6 votes |
@Override public boolean onGenericMotionEvent(MotionEvent aEvent) { if (aEvent.getActionIndex() != 0) { Log.e(LOGTAG,"aEvent.getActionIndex()=" + aEvent.getActionIndex()); return false; } if (aEvent.getAction() != MotionEvent.ACTION_HOVER_MOVE) { return false; } final float xx = aEvent.getX(0); final float yy = aEvent.getY(0); queueRunnable(() -> touchEvent(false, xx, yy)); return true; }
Example 2
Source File: ZoomableRecyclerView.java From Hentoid with Apache License 2.0 | 6 votes |
@Override public boolean onTouchEvent(MotionEvent ev) { int action = ev.getActionMasked(); int actionIndex = ev.getActionIndex(); switch (action) { case MotionEvent.ACTION_DOWN: motionActionDownLocal(ev); break; case MotionEvent.ACTION_POINTER_DOWN: motionActionPointerDown(ev, actionIndex); break; case MotionEvent.ACTION_MOVE: return motionActionMoveLocal(ev); case MotionEvent.ACTION_UP: motionActionUpLocal(ev); break; case MotionEvent.ACTION_CANCEL: motionActionCancel(); break; default: // Nothing to process as default } return super.onTouchEvent(ev); }
Example 3
Source File: GameSurfaceView.java From FruitNinja with Apache License 2.0 | 5 votes |
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: createNewPath(event.getX(), event.getY(), event.getPointerId(0)); break; case MotionEvent.ACTION_POINTER_DOWN: int newPointerIndex = event.getActionIndex(); createNewPath(event.getX(newPointerIndex), event.getY(newPointerIndex), event.getPointerId(newPointerIndex)); break; case MotionEvent.ACTION_MOVE: for (int i = 0; i < paths.size(); i++) { int pointerIndex = event.findPointerIndex(paths.indexOfKey(i)); if (pointerIndex >= 0) { paths.valueAt(i).lineTo(event.getX(pointerIndex), event.getY(pointerIndex)); paths.valueAt(i).updateTimeDrawn(System.currentTimeMillis()); } } break; } gameThread.updateDrawnPath(paths); return true; }
Example 4
Source File: PZSImageView.java From CameraV with GNU General Public License v3.0 | 5 votes |
@Override public boolean onTouchEvent(MotionEvent event) { if (mHandleTouch) { int action = parseMotionEvent(event); switch(action){ case PZS_ACTION_INIT: initGestureAction(event.getX(), event.getY()); break; case PZS_ACTION_SCALE: handleScale(event); break; case PZS_ACTION_TRANSLATE: handleTranslate(event); break; case PZS_ACTION_TRANSLATE_TO_SCALE: initGestureAction(event.getX(), event.getY()); break; case PZS_ACTION_SCALE_TO_TRANSLATE: int activeIndex = (event.getActionIndex() == 0 ? 1 : 0); initGestureAction(event.getX(activeIndex), event.getY(activeIndex)); break; case PZS_ACTION_FIT_CENTER: fitCenter(); initGestureAction(event.getX(), event.getY()); break; case PZS_ACTION_CANCEL: break; } //check current position of bitmap. validateMatrix(); updateMatrix(); return true; } else return false; }
Example 5
Source File: MultiPointerGestureDetector.java From ZoomableDraweeView-sample with Apache License 2.0 | 5 votes |
/** * Gets the index of the i-th pressed pointer. * Normally, the index will be equal to i, except in the case when the pointer is released. * @return index of the specified pointer or -1 if not found (i.e. not enough pointers are down) */ private int getPressedPointerIndex(MotionEvent event, int i) { final int count = event.getPointerCount(); final int action = event.getActionMasked(); final int index = event.getActionIndex(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { if (i >= index) { i++; } } return (i < count) ? i : -1; }
Example 6
Source File: ScalingImageView.java From ShaderEditor with MIT License | 5 votes |
private void transform(MotionEvent event) { transformMatrix.set(initialMatrix); int pointerCount = event.getPointerCount(); if (pointerCount == 1) { int i = event.getActionIndex(); PointF point = initialPoint.get(event.getPointerId(i)); if (point != null) { transformMatrix.postTranslate( event.getX(i) - point.x, event.getY(i) - point.y); } } else if (pointerCount > 1) { transformTapeline.set(event, 0, 1); float scale = fitScale( initialMatrix, drawableRect, transformTapeline.length / initialTapeline.length); transformMatrix.postScale( scale, scale, initialTapeline.pivotX, initialTapeline.pivotY); transformMatrix.postTranslate( transformTapeline.pivotX - initialTapeline.pivotX, transformTapeline.pivotY - initialTapeline.pivotY); } if (fitTranslate(transformMatrix, drawableRect, bounds)) { initTransform(event, -1); } super.setImageMatrix(transformMatrix); }
Example 7
Source File: SuggestionStripView.java From openboard with GNU General Public License v3.0 | 5 votes |
@Override public boolean onInterceptTouchEvent(final MotionEvent me) { if (mStripVisibilityGroup.isShowingImportantNoticeStrip()) { return false; } // Detecting sliding up finger to show {@link MoreSuggestionsView}. if (!mMoreSuggestionsView.isShowingInParent()) { mLastX = (int)me.getX(); mLastY = (int)me.getY(); return mMoreSuggestionsSlidingDetector.onTouchEvent(me); } if (mMoreSuggestionsView.isInModalMode()) { return false; } final int action = me.getAction(); final int index = me.getActionIndex(); final int x = (int)me.getX(index); final int y = (int)me.getY(index); if (Math.abs(x - mOriginX) >= mMoreSuggestionsModalTolerance || mOriginY - y >= mMoreSuggestionsModalTolerance) { // Decided to be in the sliding suggestion mode only when the touch point has been moved // upward. Further {@link MotionEvent}s will be delivered to // {@link #onTouchEvent(MotionEvent)}. mNeedsToTransformTouchEventToHoverEvent = AccessibilityUtils.Companion.getInstance().isTouchExplorationEnabled(); mIsDispatchingHoverEventToMoreSuggestions = false; return true; } if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { // Decided to be in the modal input mode. mMoreSuggestionsView.setModalMode(); } return false; }
Example 8
Source File: MultiPointerGestureDetector.java From FrescoUtils with Apache License 2.0 | 5 votes |
private int getPressedPointerIndex(MotionEvent event, int i) { final int count = event.getPointerCount(); final int action = event.getActionMasked(); final int index = event.getActionIndex(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { if (i >= index) { i++; } } return (i < count) ? i : -1; }
Example 9
Source File: SuggestionStripView.java From Indic-Keyboard with Apache License 2.0 | 4 votes |
@Override public boolean onTouchEvent(final MotionEvent me) { if (!mMoreSuggestionsView.isShowingInParent()) { // Ignore any touch event while more suggestions panel hasn't been shown. // Detecting sliding up is done at {@link #onInterceptTouchEvent}. return true; } // In the sliding input mode. {@link MotionEvent} should be forwarded to // {@link MoreSuggestionsView}. final int index = me.getActionIndex(); final int x = mMoreSuggestionsView.translateX((int)me.getX(index)); final int y = mMoreSuggestionsView.translateY((int)me.getY(index)); me.setLocation(x, y); if (!mNeedsToTransformTouchEventToHoverEvent) { mMoreSuggestionsView.onTouchEvent(me); return true; } // In sliding suggestion mode with accessibility mode on, a touch event should be // transformed to a hover event. final int width = mMoreSuggestionsView.getWidth(); final int height = mMoreSuggestionsView.getHeight(); final boolean onMoreSuggestions = (x >= 0 && x < width && y >= 0 && y < height); if (!onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) { // Just drop this touch event because dispatching hover event isn't started yet and // the touch event isn't on {@link MoreSuggestionsView}. return true; } final int hoverAction; if (onMoreSuggestions && !mIsDispatchingHoverEventToMoreSuggestions) { // Transform this touch event to a hover enter event and start dispatching a hover // event to {@link MoreSuggestionsView}. mIsDispatchingHoverEventToMoreSuggestions = true; hoverAction = MotionEvent.ACTION_HOVER_ENTER; } else if (me.getActionMasked() == MotionEvent.ACTION_UP) { // Transform this touch event to a hover exit event and stop dispatching a hover event // after this. mIsDispatchingHoverEventToMoreSuggestions = false; mNeedsToTransformTouchEventToHoverEvent = false; hoverAction = MotionEvent.ACTION_HOVER_EXIT; } else { // Transform this touch event to a hover move event. hoverAction = MotionEvent.ACTION_HOVER_MOVE; } me.setAction(hoverAction); mMoreSuggestionsView.onHoverEvent(me); return true; }
Example 10
Source File: CardContainer.java From giraff-android with GNU Affero General Public License v3.0 | 4 votes |
@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(0); if (cardModel.getOnClickListener() != null) { cardModel.getOnClickListener().onCardClick(); } 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; }
Example 11
Source File: PlayerInputComponent.java From Learning-Java-by-Building-Android-Games-Second-Edition with MIT License | 4 votes |
@Override public void handleInput(MotionEvent event, GameState gameState, ArrayList<Rect> buttons) { // In each MotionEvent object, every active pointer is present. Therefore looping through them all for an event on only one of them is obviously wrong. // The getActionIndex returns the index in the array of the pointer that performed/trigged the action/method call. So using getX(i) and getY(i) only gets a true result // on the button that was actually pressed/removed!!!!!!!!!!!!!!!!!!!!!!!!!!!!! int i = event.getActionIndex(); int x = (int) event.getX(i); int y = (int) event.getY(i); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_UP: if (buttons.get(HUD.UP).contains(x,y) || buttons.get(HUD.DOWN).contains(x,y)) { // Player has released either up or down mTransform.stopVertical(); } break; case MotionEvent.ACTION_DOWN: if (buttons.get(HUD.UP).contains(x,y)) { // Player has pressed up mTransform.headUp(); } else if (buttons.get(HUD.DOWN).contains(x,y)) { // Player has pressed down mTransform.headDown(); } else if (buttons.get(HUD.FLIP).contains(x,y)) { // Player has released the flip button mTransform.flip(); } else if (buttons.get(HUD.SHOOT).contains(x,y)) { mPLS.spawnPlayerLaser(mTransform); } break; case MotionEvent.ACTION_POINTER_UP: if (buttons.get(HUD.UP).contains(x, y) || buttons.get(HUD.DOWN).contains(x, y)) { // Player has released either up or down mTransform.stopVertical(); } break; case MotionEvent.ACTION_POINTER_DOWN: if (buttons.get(HUD.UP).contains(x, y)) { // Player has pressed up mTransform.headUp(); } else if (buttons.get(HUD.DOWN).contains(x, y)) { // Player has pressed down mTransform.headDown(); } else if (buttons.get(HUD.FLIP).contains(x, y)) { // Player has released the flip button mTransform.flip(); } else if (buttons.get(HUD.SHOOT).contains(x, y)) { mPLS.spawnPlayerLaser(mTransform); } break; } }
Example 12
Source File: TouchView1.java From Android_UE with Apache License 2.0 | 4 votes |
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: trankId = event.getPointerId(event.getActionIndex()); downX = event.getX(); downY = event.getY(); break; case MotionEvent.ACTION_POINTER_DOWN: trankId = event.getPointerId(event.getActionIndex()); int downIndex = event.getActionIndex(); downX = event.getX(downIndex); downY = event.getY(downIndex); break; case MotionEvent.ACTION_MOVE: int moveIndex = event.findPointerIndex(trankId); if (moveIndex >= 0) { mTranX = event.getX(moveIndex) - downX + mTranX; mTranY = event.getY(moveIndex) - downY + mTranY; handlerPort(); invalidate(); downX = event.getX(moveIndex); downY = event.getY(moveIndex); } break; case MotionEvent.ACTION_POINTER_UP: // 抬起的是当前的手指 if (event.getPointerId(event.getActionIndex()) == trankId && event.getPointerCount() > 1) { int newIndex; if (event.getActionIndex() == event.getPointerCount() - 1 && event.getPointerCount() > 2) { newIndex = event.getActionIndex() - 2; } else { newIndex = event.getActionIndex() - 1; } if (newIndex >= 0) { trankId = event.getPointerId(newIndex); downX = event.getX(newIndex); downY = event.getY(newIndex); } } break; } return true; }
Example 13
Source File: StackView.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
private void onSecondaryPointerUp(MotionEvent ev) { final int activePointerIndex = ev.getActionIndex(); final int pointerId = ev.getPointerId(activePointerIndex); if (pointerId == mActivePointerId) { int activeViewIndex = (mSwipeGestureType == GESTURE_SLIDE_DOWN) ? 0 : 1; View v = getViewAtRelativeIndex(activeViewIndex); if (v == null) return; // Our primary pointer has gone up -- let's see if we can find // another pointer on the view. If so, then we should replace // our primary pointer with this new pointer and adjust things // so that the view doesn't jump for (int index = 0; index < ev.getPointerCount(); index++) { if (index != activePointerIndex) { float x = ev.getX(index); float y = ev.getY(index); mTouchRect.set(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); if (mTouchRect.contains(Math.round(x), Math.round(y))) { float oldX = ev.getX(activePointerIndex); float oldY = ev.getY(activePointerIndex); // adjust our frame of reference to avoid a jump mInitialY += (y - oldY); mInitialX += (x - oldX); mActivePointerId = ev.getPointerId(index); if (mVelocityTracker != null) { mVelocityTracker.clear(); } // ok, we're good, we found a new pointer which is touching the active view return; } } } // if we made it this far, it means we didn't find a satisfactory new pointer :(, // so end the gesture handlePointerUp(ev); } }
Example 14
Source File: PullToZoomListView.java From UltimateAndroid with Apache License 2.0 | 4 votes |
public boolean onTouchEvent(MotionEvent paramMotionEvent) { Log.d("mmm", "" + (0xFF & paramMotionEvent.getAction())); switch (0xFF & paramMotionEvent.getAction()) { case 4: case 0: if (!this.mScalingRunnalable.mIsFinished) { this.mScalingRunnalable.abortAnimation(); } this.mLastMotionY = paramMotionEvent.getY(); this.mActivePointerId = paramMotionEvent.getPointerId(0); this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight); this.mLastScale = (this.mHeaderContainer.getBottom() / this.mHeaderHeight); break; case 2: Log.d("mmm", "mActivePointerId" + mActivePointerId); int j = paramMotionEvent.findPointerIndex(this.mActivePointerId); if (j == -1) { Log.e("PullToZoomListView", "Invalid pointerId=" + this.mActivePointerId + " in onTouchEvent"); } else { if (this.mLastMotionY == -1.0F) this.mLastMotionY = paramMotionEvent.getY(j); if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight) { ViewGroup.LayoutParams localLayoutParams = this.mHeaderContainer .getLayoutParams(); float f = ((paramMotionEvent.getY(j) - this.mLastMotionY + this.mHeaderContainer .getBottom()) / this.mHeaderHeight - this.mLastScale) / 2.0F + this.mLastScale; if ((this.mLastScale <= 1.0D) && (f < this.mLastScale)) { localLayoutParams.height = this.mHeaderHeight; this.mHeaderContainer .setLayoutParams(localLayoutParams); return super.onTouchEvent(paramMotionEvent); } this.mLastScale = Math.min(Math.max(f, 1.0F), this.mMaxScale); localLayoutParams.height = ((int) (this.mHeaderHeight * this.mLastScale)); if (localLayoutParams.height < this.mScreenHeight) this.mHeaderContainer .setLayoutParams(localLayoutParams); this.mLastMotionY = paramMotionEvent.getY(j); return true; } this.mLastMotionY = paramMotionEvent.getY(j); } break; case 1: reset(); endScraling(); break; case 3: int i = paramMotionEvent.getActionIndex(); this.mLastMotionY = paramMotionEvent.getY(i); this.mActivePointerId = paramMotionEvent.getPointerId(i); break; case 5: onSecondaryPointerUp(paramMotionEvent); this.mLastMotionY = paramMotionEvent.getY(paramMotionEvent .findPointerIndex(this.mActivePointerId)); break; case 6: } return super.onTouchEvent(paramMotionEvent); }
Example 15
Source File: PlayerInputComponent.java From Learning-Java-by-Building-Android-Games-Second-Edition with MIT License | 4 votes |
public void handleInput(MotionEvent event, GameState gameState, ArrayList<Rect> buttons) { int i = event.getActionIndex(); int x = (int) event.getX(i); int y = (int) event.getY(i); if(!gameState.getPaused()) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_UP: if (buttons.get(HUD.LEFT).contains(x, y) || buttons.get(HUD.RIGHT) .contains(x, y)) { // Player has released either left or right mPlayersTransform.stopHorizontal(); } break; case MotionEvent.ACTION_DOWN: if (buttons.get(HUD.LEFT).contains(x, y)) { // Player has pressed left mPlayersTransform.headLeft(); } else if (buttons.get(HUD.RIGHT).contains(x, y)) { // Player has pressed right mPlayersTransform.headRight(); } else if (buttons.get(HUD.JUMP).contains(x, y)) { // Player has released the jump button mPlayersPlayerTransform.triggerJump(); } break; case MotionEvent.ACTION_POINTER_UP: if (buttons.get(HUD.LEFT).contains(x, y) || buttons.get(HUD.RIGHT).contains(x, y)) { // Player has released either up or down mPlayersTransform.stopHorizontal(); } break; case MotionEvent.ACTION_POINTER_DOWN: if (buttons.get(HUD.LEFT).contains(x, y)) { // Player has pressed left mPlayersTransform.headLeft(); } else if (buttons.get(HUD.RIGHT).contains(x, y)) { // Player has pressed right mPlayersTransform.headRight(); } else if (buttons.get(HUD.JUMP).contains(x, y)) { // Player has released the jump button mPlayersPlayerTransform.triggerJump(); } break; } } }
Example 16
Source File: RotaryControlView.java From pin with GNU General Public License v3.0 | 4 votes |
@Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); Log.d("RotaryControlView", "Touch Event with pointer index: " + event.getActionIndex()); if(event.getActionIndex() != 0) return true;//We only care about pointer 1 int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); int paddingRight = getPaddingRight(); int paddingBottom = getPaddingBottom(); int right = getWidth() - paddingRight; int bottom = getHeight() - paddingBottom; int contentWidth = right - paddingLeft; int contentHeight = bottom - paddingTop; float cx = paddingLeft + (0.5f * contentWidth); float cy = paddingTop + (0.5f * contentHeight); float x = event.getX(); float y = event.getY(); double unit = ((double) mMaxValue - mMinValue) / Math.abs(mEndAngle - mStartAngle); float a, c; double angle; a = Math.abs(y - cy); c = Math.abs(x - cx); if(c != 0) angle = Math.toDegrees(Math.atan(a / c)); else angle = 0; if(x < cx) angle = 180 - angle; if(y < cy) angle = 360 - angle; Log.d("RotaryControlView", "onTouchMove, alpha: " + angle); switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: Log.d("RotaryControlView", "onTouchDown"); mTouch.set(x, y); mLastValue = mValue; if(mValListener != null) mValListener.onUserChangeBegin(mValue); break; case MotionEvent.ACTION_MOVE: double diffangle; if(Math.abs(angle - mAngleLastTouch) > 180) diffangle = (360 - angle) - mAngleLastTouch; else diffangle = angle - mAngleLastTouch; double range = mEndAngle - mStartAngle; while(range <= 0) range += 360; mLastValue += (int) Math.round(diffangle * (mMaxValue - mMinValue) / range); mLastValue = Math.min(mLastValue, mMaxValue); mLastValue = Math.max(mLastValue, mMinValue); Log.d("RotaryControlView", "onTouchMove, diffangle: " + diffangle); int val = (int) mLastValue; this.mValue = val - (val % mStepValue); if(mValListener != null) mValListener.onUserChange(mValue); invalidate(); break; case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_UP: if(mValListener != null) mValListener.onUserChangeEnd(mValue); Log.d("RotaryControlView", "onTouchUp"); } mAngleLastTouch = angle; return true; }
Example 17
Source File: Canvas.java From J2ME-Loader with Apache License 2.0 | 4 votes |
@Override @SuppressLint("ClickableViewAccessibility") public boolean onTouchEvent(MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: if (overlay != null) { overlay.show(); } case MotionEvent.ACTION_POINTER_DOWN: int index = event.getActionIndex(); int id = event.getPointerId(index); if ((overlay == null || !overlay.pointerPressed(id, event.getX(index), event.getY(index))) && touchInput && id == 0) { Display.postEvent(CanvasEvent.getInstance(Canvas.this, CanvasEvent.POINTER_PRESSED, id, convertPointerX(event.getX()), convertPointerY(event.getY()))); } break; case MotionEvent.ACTION_MOVE: int pointerCount = event.getPointerCount(); int historySize = event.getHistorySize(); for (int h = 0; h < historySize; h++) { for (int p = 0; p < pointerCount; p++) { id = event.getPointerId(p); if ((overlay == null || !overlay.pointerDragged(id, event.getHistoricalX(p, h), event.getHistoricalY(p, h))) && touchInput && id == 0) { Display.postEvent(CanvasEvent.getInstance(Canvas.this, CanvasEvent.POINTER_DRAGGED, id, convertPointerX(event.getHistoricalX(p, h)), convertPointerY(event.getHistoricalY(p, h)))); } } } for (int p = 0; p < pointerCount; p++) { id = event.getPointerId(p); if ((overlay == null || !overlay.pointerDragged(id, event.getX(p), event.getY(p))) && touchInput && id == 0) { Display.postEvent(CanvasEvent.getInstance(Canvas.this, CanvasEvent.POINTER_DRAGGED, id, convertPointerX(event.getX(p)), convertPointerY(event.getY(p)))); } } break; case MotionEvent.ACTION_UP: if (overlay != null) { overlay.hide(); } case MotionEvent.ACTION_POINTER_UP: index = event.getActionIndex(); id = event.getPointerId(index); if ((overlay == null || !overlay.pointerReleased(id, event.getX(index), event.getY(index))) && touchInput && id == 0) { Display.postEvent(CanvasEvent.getInstance(Canvas.this, CanvasEvent.POINTER_RELEASED, id, convertPointerX(event.getX()), convertPointerY(event.getY()))); } break; default: return super.onTouchEvent(event); } return true; }
Example 18
Source File: Touchscreen.java From YetAnotherPixelDungeon with GNU General Public License v3.0 | 4 votes |
public static void processTouchEvents( ArrayList<MotionEvent> events ) { int size = events.size(); for (int i=0; i < size; i++) { MotionEvent e = events.get( i ); Touch touch; switch (e.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: touched = true; touch = new Touch( e, 0 ); pointers.put( e.getPointerId( 0 ), touch ); event.dispatch( touch ); break; case MotionEvent.ACTION_POINTER_DOWN: int index = e.getActionIndex(); touch = new Touch( e, index ); pointers.put( e.getPointerId( index ), touch ); event.dispatch( touch ); break; case MotionEvent.ACTION_MOVE: int count = e.getPointerCount(); for (int j=0; j < count; j++) { pointers.get( e.getPointerId( j ) ).update( e, j ); } event.dispatch( null ); break; case MotionEvent.ACTION_POINTER_UP: event.dispatch( pointers.remove( e.getPointerId( e.getActionIndex() ) ).up() ); break; case MotionEvent.ACTION_UP: touched = false; event.dispatch( pointers.remove( e.getPointerId( 0 ) ).up() ); break; } e.recycle(); } }
Example 19
Source File: HeaderBehavior.java From material-components-android with Apache License 2.0 | 4 votes |
@Override public boolean onTouchEvent( @NonNull CoordinatorLayout parent, @NonNull V child, @NonNull MotionEvent ev) { boolean consumeUp = false; switch (ev.getActionMasked()) { case MotionEvent.ACTION_MOVE: final int activePointerIndex = ev.findPointerIndex(activePointerId); if (activePointerIndex == -1) { return false; } final int y = (int) ev.getY(activePointerIndex); int dy = lastMotionY - y; lastMotionY = y; // We're being dragged so scroll the ABL scroll(parent, child, dy, getMaxDragOffset(child), 0); break; case MotionEvent.ACTION_POINTER_UP: int newIndex = ev.getActionIndex() == 0 ? 1 : 0; activePointerId = ev.getPointerId(newIndex); lastMotionY = (int) (ev.getY(newIndex) + 0.5f); break; case MotionEvent.ACTION_UP: if (velocityTracker != null) { consumeUp = true; velocityTracker.addMovement(ev); velocityTracker.computeCurrentVelocity(1000); float yvel = velocityTracker.getYVelocity(activePointerId); fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel); } // $FALLTHROUGH case MotionEvent.ACTION_CANCEL: isBeingDragged = false; activePointerId = INVALID_POINTER; if (velocityTracker != null) { velocityTracker.recycle(); velocityTracker = null; } break; } if (velocityTracker != null) { velocityTracker.addMovement(ev); } return isBeingDragged || consumeUp; }
Example 20
Source File: PlayerInputComponent.java From Learning-Java-by-Building-Android-Games-Second-Edition with MIT License | 4 votes |
@Override public void handleInput(MotionEvent event, GameState gameState, ArrayList<Rect> buttons) { // In each MotionEvent object, every active pointer is present. Therefore looping through them all for an event on only one of them is obviously wrong. // The getActionIndex returns the index in the array of the pointer that performed/trigged the action/method call. So using getX(i) and getY(i) only gets a true result // on the button that was actually pressed/removed!!!!!!!!!!!!!!!!!!!!!!!!!!!!! int i = event.getActionIndex(); int x = (int) event.getX(i); int y = (int) event.getY(i); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_UP: if (buttons.get(HUD.UP).contains(x,y) || buttons.get(HUD.DOWN).contains(x,y)) { // Player has released either up or down mTransform.stopVertical(); } break; case MotionEvent.ACTION_DOWN: if (buttons.get(HUD.UP).contains(x,y)) { // Player has pressed up mTransform.headUp(); } else if (buttons.get(HUD.DOWN).contains(x,y)) { // Player has pressed down mTransform.headDown(); } else if (buttons.get(HUD.FLIP).contains(x,y)) { // Player has released the flip button mTransform.flip(); } else if (buttons.get(HUD.SHOOT).contains(x,y)) { mPLS.spawnPlayerLaser(mTransform); } break; case MotionEvent.ACTION_POINTER_UP: if (buttons.get(HUD.UP).contains(x, y) || buttons.get(HUD.DOWN).contains(x, y)) { // Player has released either up or down mTransform.stopVertical(); } break; case MotionEvent.ACTION_POINTER_DOWN: if (buttons.get(HUD.UP).contains(x, y)) { // Player has pressed up mTransform.headUp(); } else if (buttons.get(HUD.DOWN).contains(x, y)) { // Player has pressed down mTransform.headDown(); } else if (buttons.get(HUD.FLIP).contains(x, y)) { // Player has released the flip button mTransform.flip(); } else if (buttons.get(HUD.SHOOT).contains(x, y)) { mPLS.spawnPlayerLaser(mTransform); } break; } }