List of usage examples for android.support.v4.view MotionEventCompat getActionIndex
public static int getActionIndex(MotionEvent event)
From source file:danonino.danonino_the_game.Core.Labels.PowerUp.java
public int onTouch(MotionEvent event) { int index = MotionEventCompat.getActionIndex(event); int x;// ww w . ja va2s .co m int y; if (event.getPointerCount() > 1) { // The coordinates of the current screen contact, relative to // the responding View or Activity. x = (int) MotionEventCompat.getX(event, index); y = (int) MotionEventCompat.getY(event, index); } else { // Single touch event x = (int) MotionEventCompat.getX(event, index); y = (int) MotionEventCompat.getY(event, index); } if (this.usePowerUpBtn.btn_rect.contains(x, y) || ((event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_POINTER_UP) && usePowerUpBtnPressed)) { this.usePowerUpBtnPressed = !this.usePowerUpBtnPressed; this.usePowerUpBtn.onTouch(event); return 1; } return 0; }
From source file:com.fishstix.dosboxfree.ButtonLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); final int pointerIndex = MotionEventCompat.getActionIndex(ev);//((action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT); final int pId = ev.getPointerId(pointerIndex) + 1; KeyEvent evt = null;/* ww w . j a v a 2 s. c o m*/ Message msg = Message.obtain(); msg.what = DBMain.HANDLER_SEND_KEYCODE; switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: { Log.i("DosBoxTurbo", "button onDown()"); int x = (int) ev.getX(pointerIndex);//(int) mWrap.getX(ev,pointerIndex); int width = getWidth(); //int y = (int) mWrap.getY(ev,pointerIndex); float val = ((float) x / (float) width) * 4f; if (val < 1.0) { // first button if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_A) > 0) return false; evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_A); virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_A); msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_A; mDBLauncher.bButtonA.setBackgroundColor(0x80FF0000); } else if (val < 2.0) { if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_B) > 0) return false; evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_B); virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_B); msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_B; mDBLauncher.bButtonB.setBackgroundColor(0x80FF0000); } else if (val < 3.0) { if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_C) > 0) return false; evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_C); virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_C); msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_C; mDBLauncher.bButtonC.setBackgroundColor(0x80FF0000); } else { if (virtualbutton_hm.indexOfValue(HardCodeWrapper.KEYCODE_VIRTUAL_D) > 0) return false; evt = new KeyEvent(action, HardCodeWrapper.KEYCODE_VIRTUAL_D); virtualbutton_hm.put(pId, HardCodeWrapper.KEYCODE_VIRTUAL_D); msg.arg2 = HardCodeWrapper.KEYCODE_VIRTUAL_D; mDBLauncher.bButtonD.setBackgroundColor(0x80FF0000); } msg.obj = evt; msg.arg1 = 0; mDBLauncher.mSurfaceView.virtButton[pointerIndex] = true; mDBLauncher.mSurfaceView.mFilterLongClick = true; // prevent long click listener from getting in the way mDBLauncher.mHandler.sendMessage(msg); return true; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: { Log.i("DosBoxTurbo", "button onUp()"); msg.arg2 = virtualbutton_hm.get(pId); switch (msg.arg2) { case HardCodeWrapper.KEYCODE_VIRTUAL_A: mDBLauncher.bButtonA.setBackgroundColor(0x80FFFF00); break; case HardCodeWrapper.KEYCODE_VIRTUAL_B: mDBLauncher.bButtonB.setBackgroundColor(0x80FFFF00); break; case HardCodeWrapper.KEYCODE_VIRTUAL_C: mDBLauncher.bButtonC.setBackgroundColor(0x80FFFF00); break; case HardCodeWrapper.KEYCODE_VIRTUAL_D: mDBLauncher.bButtonD.setBackgroundColor(0x80FFFF00); break; } virtualbutton_hm.delete(pId); if (msg.arg2 == 0) return false; evt = new KeyEvent(action, msg.arg2); msg.obj = evt; msg.arg1 = 1; mDBLauncher.mSurfaceView.virtButton[pointerIndex] = false; mDBLauncher.mSurfaceView.mFilterLongClick = false; mDBLauncher.mHandler.sendMessage(msg); return true; } } return false; }
From source file:com.duy.pascal.ui.view.BaseRecycleView.java
@Override public boolean onInterceptTouchEvent(MotionEvent e) { final int action = MotionEventCompat.getActionMasked(e); final int actionIndex = MotionEventCompat.getActionIndex(e); switch (action) { case MotionEvent.ACTION_DOWN: mScrollPointerId = MotionEventCompat.getPointerId(e, 0); mInitialTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = (int) (e.getY() + 0.5f); return super.onInterceptTouchEvent(e); case MotionEventCompat.ACTION_POINTER_DOWN: mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); mInitialTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); mInitialTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); return super.onInterceptTouchEvent(e); case MotionEvent.ACTION_MOVE: { final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); if (index < 0) { return false; }//www . j a v a 2s . c o m final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); if (getScrollState() != SCROLL_STATE_DRAGGING) { final int dx = x - mInitialTouchX; final int dy = y - mInitialTouchY; final boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally(); final boolean canScrollVertically = getLayoutManager().canScrollVertically(); boolean startScroll = false; if (canScrollHorizontally && Math.abs(dx) > mTouchSlop && (Math.abs(dx) >= Math.abs(dy) || canScrollVertically)) { startScroll = true; } if (canScrollVertically && Math.abs(dy) > mTouchSlop && (Math.abs(dy) >= Math.abs(dx) || canScrollHorizontally)) { startScroll = true; } return startScroll && super.onInterceptTouchEvent(e); } return super.onInterceptTouchEvent(e); } default: return super.onInterceptTouchEvent(e); } }
From source file:com.github.ppamorim.SlapBar.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!isEnabled()) { return false; }/*w w w .ja v a 2s . c om*/ switch (MotionEventCompat.getActionMasked(ev)) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: dragHelper.cancel(); return false; case MotionEvent.ACTION_DOWN: int index = MotionEventCompat.getActionIndex(ev); activePointerId = MotionEventCompat.getPointerId(ev, index); if (activePointerId == INVALID_POINTER) { return false; } default: return dragHelper.shouldInterceptTouchEvent(ev); } }
From source file:com.greenkee.pokeADot.GameActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // tell system to use the layout defined in our XML file setContentView(com.greenkee.pokeADot.R.layout.activity_game_screen); mView = (GameView) findViewById(com.greenkee.pokeADot.R.id.gameScreen); mThread = mView.getThread();// w ww . ja va 2 s .c o m mView.setActivity(this); ((GameView) mView).setTextViews((TextView) findViewById(com.greenkee.pokeADot.R.id.status_display), (TextView) findViewById(com.greenkee.pokeADot.R.id.score_display), (TextView) findViewById(com.greenkee.pokeADot.R.id.combo_display)); if (savedInstanceState == null) { System.out.println("STATE SET"); mThread.doStart(); } else { super.onRestoreInstanceState(savedInstanceState); mThread.restoreState(savedInstanceState); } mView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int action = MotionEventCompat.getActionMasked(event); final int pointerIndex; final float x; final float y; switch (action) { case (MotionEvent.ACTION_DOWN): { // System.out.println("ACTION_DOWN"); if (((GameView.GameThread) mThread).getCurrentState() == GameView.GameThread.STATE_RUNNING) { int index = MotionEventCompat.getActionIndex(event); p1ID = MotionEventCompat.getPointerId(event, index); if (p1ID != -1) { int p1Index = MotionEventCompat.findPointerIndex(event, p1ID); ((GameView.GameThread) mThread).checkTouch(MotionEventCompat.getX(event, p1Index), MotionEventCompat.getY(event, p1Index)); //DO SOMETHING HERE } } else if (!(mView.dialogOpen) && (!(((GameView.GameThread) mThread).gameOver)) && (((GameView.GameThread) mThread) .getCurrentState() == GameView.GameThread.STATE_READY) || (((GameView.GameThread) mThread) .getCurrentState() == GameView.GameThread.STATE_LOSE)) { ((GameView.GameThread) mThread).startGame(); // System.out.println("START GAME"); } else if (((GameView.GameThread) mThread) .getCurrentState() == GameView.GameThread.STATE_PAUSE) { mThread.unpause(); } return true; } case (MotionEvent.ACTION_MOVE): { // System.out.println("ACTION_MOVE"); if (((GameView.GameThread) mThread).getCurrentState() == GameView.GameThread.STATE_RUNNING) { if (p1ID != -1) { int p1Index = MotionEventCompat.findPointerIndex(event, p1ID); //DO SOMETHING HERE } } return true; } case (MotionEvent.ACTION_UP): { // System.out.println("ACTION_UP"); reset(); return true; } case (MotionEvent.ACTION_CANCEL): { // System.out.println("ACTION_CANCEL"); reset(); return true; } case (MotionEvent.ACTION_OUTSIDE): { return true; } } return false; } private void reset() { numTouches = 0; p1ID = -1; p2ID = -1; } } ); }
From source file:cn.goodjobs.common.view.photodraweeview.ScaleDragDetector.java
private void onTouchActivePointer(int action, MotionEvent ev) { switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = ev.getPointerId(0); break;// w w w.j a v a2 s . c o m case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: mActivePointerId = INVALID_POINTER_ID; break; case MotionEvent.ACTION_POINTER_UP: final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { final int newPointerIndex = (pointerIndex == 0) ? 1 : 0; mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex); mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex); } break; } mActivePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0); }
From source file:com.github.ppamorim.library.DraggerView.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!isEnabled()) { return false; }/* w ww. j a v a 2s. com*/ final int action = MotionEventCompat.getActionMasked(ev); switch (action) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: dragHelper.cancel(); return false; case MotionEvent.ACTION_DOWN: int index = MotionEventCompat.getActionIndex(ev); activePointerId = MotionEventCompat.getPointerId(ev, index); if (activePointerId == INVALID_POINTER) { return false; } default: return dragHelper.shouldInterceptTouchEvent(ev); } }
From source file:com.github.ppamorim.cult.CultView.java
/** * Override method to intercept only touch events over the drag view and to cancel the drag when * the action associated to the MotionEvent is equals to ACTION_CANCEL or ACTION_UP. * * @param ev captured./*from w w w .j ava 2 s .c o m*/ * @return true if the view is going to process the touch event or false if not. */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!isEnabled()) { return false; } switch (MotionEventCompat.getActionMasked(ev)) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: dragHelper.cancel(); return false; case MotionEvent.ACTION_DOWN: int index = MotionEventCompat.getActionIndex(ev); activePointerId = MotionEventCompat.getPointerId(ev, index); if (activePointerId == INVALID_POINTER) { return false; } default: dragHelper.processTouchEvent(ev); return dragHelper.shouldInterceptTouchEvent(ev); } }
From source file:com.github.ppamorim.dragger.DraggerView.java
/** * Detect the type of motion event (like touch) * at the DraggerView, this can be a simple * detector of the touch, not the listener ifself. * * @param ev Event of MotionEvent// ww w.j a va2 s . co m * @return View is touched */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!isEnabled() || !canSlide()) { return false; } final int action = MotionEventCompat.getActionMasked(ev); switch (action) { case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: dragHelper.cancel(); return false; case MotionEvent.ACTION_DOWN: int index = MotionEventCompat.getActionIndex(ev); activePointerId = MotionEventCompat.getPointerId(ev, index); if (activePointerId == INVALID_POINTER) { return false; } default: return dragHelper.shouldInterceptTouchEvent(ev); } }
From source file:com.android.ex.photo.PhotoViewPager.java
/** * {@inheritDoc}/*from w w w . j a v a 2 s.c om*/ * <p> * We intercept touch event intercepts so we can prevent switching views when the * current view is internally scrollable. */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final InterceptType intercept = (mListener != null) ? mListener.onTouchIntercept(mActivatedX, mActivatedY) : InterceptType.NONE; final boolean ignoreScrollLeft = (intercept == InterceptType.BOTH || intercept == InterceptType.LEFT); final boolean ignoreScrollRight = (intercept == InterceptType.BOTH || intercept == InterceptType.RIGHT); // Only check ability to page if we can't scroll in one / both directions final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mActivePointerId = INVALID_POINTER; } switch (action) { case MotionEvent.ACTION_MOVE: { if (ignoreScrollLeft || ignoreScrollRight) { 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 = MotionEventCompat.findPointerIndex(ev, activePointerId); final float x = MotionEventCompat.getX(ev, pointerIndex); if (ignoreScrollLeft && ignoreScrollRight) { mLastMotionX = x; return false; } else if (ignoreScrollLeft && (x > mLastMotionX)) { mLastMotionX = x; return false; } else if (ignoreScrollRight && (x < mLastMotionX)) { mLastMotionX = x; return false; } } break; } case MotionEvent.ACTION_DOWN: { mLastMotionX = ev.getX(); // Use the raw x/y as the children can be located anywhere and there isn't a // single offset that would be meaningful mActivatedX = ev.getRawX(); mActivatedY = ev.getRawY(); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEventCompat.ACTION_POINTER_UP: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); if (pointerId == mActivePointerId) { // Our active pointer going up; select a new active pointer final int newPointerIndex = pointerIndex == 0 ? 1 : 0; mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex); mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex); } break; } } return super.onInterceptTouchEvent(ev); }