@Override public boolean dispatchTouchEvent(MotionEvent event) { int x = (int) event.getRawX(); int y = (int) event.getRawY(); int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { View clickedView = getTouchTarget(this, x, y); if (clickedView != null && clickedView.isClickable() && clickedView.isEnabled()) { this.clickedView = clickedView; isClickedParent = false; initParamForRipple(event, clickedView, false); postInvalidateDelayed(INVALIDATE_DURATION); } else { this.clickedView = this; isClickedParent = true; RippleLayout.this.setClickable(true); initParamForRipple(event, clickedView, true); postInvalidateDelayed(INVALIDATE_DURATION); } } else if (action == MotionEvent.ACTION_UP) { isPressed = false; RippleLayout.this.setClickable(false); postInvalidateDelayed(INVALIDATE_DURATION); return true; } else if (action == MotionEvent.ACTION_CANCEL) { isPressed = false; postInvalidateDelayed(INVALIDATE_DURATION); } return super.dispatchTouchEvent(event); }
public boolean onTouchEvent(MotionEvent ev) { int action = ev.getActionMasked(); ViewParent parent = getParent(); // LinearLayout gestureDetector.onTouchEvent(ev); switch (action) { case MotionEvent.ACTION_DOWN: this.mDragHelper.processTouchEvent(ev); parent.requestDisallowInterceptTouchEvent(true); sX = ev.getRawX(); sY = ev.getRawY(); return true; case MotionEvent.ACTION_MOVE: float distanceX = ev.getRawX() - sX; float distanceY = ev.getRawY() - sY; sX = ev.getRawX(); sY = ev.getRawY(); float angle = Math.abs(distanceY / distanceX); angle = (float) Math.toDegrees(Math.atan(angle)); if (angle > 30) { parent.requestDisallowInterceptTouchEvent(false); return false; } parent.requestDisallowInterceptTouchEvent(true); mDragHelper.processTouchEvent(ev); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: default: parent.requestDisallowInterceptTouchEvent(true); mDragHelper.processTouchEvent(ev); } return true; // 必须返回true,表示已经正确处理touch事件,才能继续后续的拖动*/ }
@Override public boolean dispatchTouchEvent(MotionEvent ev) { createVelocityTracker(ev); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: xDown = ev.getRawX(); yDown = ev.getRawY(); break; case MotionEvent.ACTION_MOVE: // 滑动事件 xMove = ev.getRawX(); yMove = ev.getRawY(); // 计算出滑动距离 int distanceX = (int) (xMove - xDown); int distanceY = (int) (yMove - yDown); int ySpeed = getScrollVelocity(); if (distanceX > XDISTANCE_MIN && (distanceY < YDISTANCE_MIN && distanceY > -YDISTANCE_MIN) && ySpeed < YSPEED_MIN) { finish(); } break; case MotionEvent.ACTION_UP: // 向上事件 recycleVelocityTracker(); break; } return super.dispatchTouchEvent(ev); }
@Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: oldX = event.getRawX(); newX = event.getRawX(); break; case MotionEvent.ACTION_MOVE: newX = event.getRawX(); if (v != layout) { v.setTranslationX((newX - oldX) * 1.075f); if (v.getX() > v.getWidth() - 150 || v.getX() < -v.getWidth() + 150) { contentFrameLayout.removeView(v); } } else { if (newX - oldX > 50) { int distance = (int) (newX - oldX); if (distance > v.getWidth() / 2) ; { finish(); overridePendingTransition(R.anim.side_in_left, R.anim.slide_out_right); } } } break; case MotionEvent.ACTION_UP: if (v != layout) { v.setTranslationX(0); } break; } return true; }
@SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (mRightListener != null) { Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT]; if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())) { mRightListener.onDrawableRightClick(this); return true; } } if (mLeftListener != null) { Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT]; if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width())) { mLeftListener.onDrawableLeftClick(this); return true; } } break; } return super.onTouchEvent(event); }
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: flingX = (int) ev.getRawX(); flingY = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: int tmp = (int) ev.getRawX(); int tmpy = (int) ev.getRawY(); if (Math.abs(tmpy - flingY) <= 100) { if (flingX - tmp > 100) { isFling = true; mHandler.sendEmptyMessage(MessageID.MESSAGE_FLING_NEXT); } else if (tmp - flingX > 100) { isFling = true; mHandler.sendEmptyMessage(MessageID.MESSAGE_FLING_PRE); } else { isFling = false; } } else { isFling = false; } break; } return super.onInterceptTouchEvent(ev); }
@Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mDownX = ev.getRawX(); mDownY = ev.getRawY(); break; case MotionEvent.ACTION_MOVE: mMoveX = ev.getRawX(); mMoveY = ev.getRawY(); int diffX = (int) (mMoveX - mDownX); int diffY = (int) (mMoveY - mDownY); if (Math.abs(diffX) > Math.abs(diffY)) { getParent().requestDisallowInterceptTouchEvent(true); } else { getParent().requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: break; default: break; } return super.onTouchEvent(ev); }
/** 事件拦截操作 */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // 处理ViewPager冲突问题 ViewPager mViewPager = getTouchViewPager(mViewPagers, ev); Log.i(TAG, "mViewPager = " + mViewPager); if (mViewPager != null && mViewPager.getCurrentItem() != 0) { return super.onInterceptTouchEvent(ev); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = tempX = (int) ev.getRawX(); downY = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) ev.getRawX(); // 满足此条件屏蔽SildingFinishLayout里面子类的touch事件 if (moveX - downX > mTouchSlop && Math.abs((int) ev.getRawY() - downY) < mTouchSlop) { return true; } break; } return super.onInterceptTouchEvent(ev); }
public boolean onTouchEvent(MotionEvent paramMotionEvent) { boolean bool1 = true; switch (paramMotionEvent.getAction()) { default: case 0: case 2: case 1: } while (true) { return bool1; boolean bool2 = requestFocus(); MarkerListener localMarkerListener1 = this.mListener; float f1 = paramMotionEvent.getRawX(); float f2 = paramMotionEvent.getY(); bool1 = localMarkerListener1.markerTouchStart(this, f1, f2); continue; MarkerListener localMarkerListener2 = this.mListener; float f3 = paramMotionEvent.getRawX(); float f4 = paramMotionEvent.getY(); bool1 = localMarkerListener2.markerTouchMove(this, f3, f4); continue; MarkerListener localMarkerListener3 = this.mListener; float f5 = paramMotionEvent.getY(); bool1 = localMarkerListener3.markerTouchEnd(this, f5); } }
/** * 事件分发, 请求父控件及祖宗控件是否拦截事件 1. 右划, 而且是第一个页面, 需要父控件(mainactivity)拦截,菜单就出来了 2. 左划, 而且是最后一个页面, * 需要父控件(viewpager2)拦截,会滑动2的下一个页面 3. 上下滑动, 需要父控件拦截 */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: getParent().requestDisallowInterceptTouchEvent(true); // 不要拦截, // 这样是为了保证ACTION_MOVE调用 startX = (int) ev.getRawX(); startY = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: int endX = (int) ev.getRawX(); int endY = (int) ev.getRawY(); if (Math.abs(endX - startX) > Math.abs(endY - startY)) { // 左右滑动 if (endX > startX) { // 右划 if (getCurrentItem() == 0) { // 第一个页面, 需要父控件拦截 getParent().requestDisallowInterceptTouchEvent(false); } } else { // 左划 if (getCurrentItem() == getAdapter().getCount() - 1) { // 最后一个页面, // 需要拦截 getParent().requestDisallowInterceptTouchEvent(false); } } } else { // 上下滑动 getParent().requestDisallowInterceptTouchEvent(false); } break; default: break; } return super.dispatchTouchEvent(ev); }
@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { mDownX = ev.getRawX(); mNeedConsumeTouch = true; // 默认情况下,scrollView内部的滚动优先,默认情况下由该ScrollView去消费touch事件 if (getScrollX() <= 0) { // 允许向右拖动 mAllowDrag = true; } else { // 不允许向右拖动 mAllowDrag = false; } } else if (ev.getAction() == MotionEvent.ACTION_MOVE) { if (!mNeedConsumeTouch) { // 在最顶端且向上拉了,则这个touch事件交给父类去处理 getParent().requestDisallowInterceptTouchEvent(false); return false; } else if (mAllowDrag) { // needConsumeTouch尚未被定性,此处给其定性 // 允许拖动到底部的下一页,而且又向上拖动了,就将touch事件交给父view if (ev.getRawX() - mDownX > 2) { // flag设置,由父类去消费 mNeedConsumeTouch = false; getParent().requestDisallowInterceptTouchEvent(false); return false; } } } // 通知父view是否要处理touch事件 getParent().requestDisallowInterceptTouchEvent(mNeedConsumeTouch); return super.dispatchTouchEvent(ev); }
@Override public boolean onFling(MotionEvent start, MotionEvent finish, float velocityX, float velocityY) { float gapX = start.getRawX() - finish.getRawX(); float gapY = start.getRawY() - finish.getRawY(); float distanceX = Math.abs(gapX); float distanceY = Math.abs(gapY); if (distanceY > distanceX) { // up downs if (gapY > 0) { // up System.out.println("Swipe up"); } else { // down System.out.println("Swipe down"); BaseActivity activity = (BaseActivity) getActivity(); activity.showToolbar(0); activity.hideToolbar(2); } } else { // left right if (gapX > 0) { // left System.out.println("Swipe left"); } else { // rights System.out.println("Swipe right"); } } return false; }
/** 事件拦截操作 */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // addVelocityTracker(ev); downX = tempX = (int) ev.getRawX(); downY = (int) ev.getRawY(); tempTime = System.currentTimeMillis(); break; case MotionEvent.ACTION_MOVE: int moveX = (int) ev.getRawX(); // 满足此条件屏蔽SildingLayout里面子类的touch事件 if ((Math.abs(moveX - downX) > mTouchSlop && Math.abs((int) ev.getRawY() - downY) < mTouchSlop)) { return true; } break; case MotionEvent.ACTION_UP: // recycleVelocityTracker(); break; } return super.onInterceptTouchEvent(ev); }
@SuppressWarnings("unused") @Override public boolean onTouch(View v, MotionEvent event) { Utils.closeKeybord(moEditPassword, Login.this); Utils.closeKeybord(moEditUsername, Login.this); if (canLogin() && !mbIsSlidingBack) { if (miSliderMaxX == 0) { miSliderMinX = moViewSlideLine.getLeft() - moImgSlider.getWidth() / 2; miSliderMaxX = moViewSlideLine.getRight() - moImgSlider.getWidth() / 2; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: miLastX = (int) event.getRawX(); case MotionEvent.ACTION_MOVE: int liX = (int) event.getRawX(); if (liX > miSliderMaxX) liX = miSliderMaxX; else if (liX < miSliderMinX) liX = miSliderMinX; if (liX != miLastX) { moImgSlider.layout( liX, moImgSlider.getTop(), liX + moImgSlider.getWidth(), moImgSlider.getTop() + moImgSlider.getHeight()); miLastX = liX; if (miLastX == miSliderMaxX) { // startRotateImg(); startLogin(); // 创建了一个新线程来发送网络请求,如此页面便不会卡顿 new Thread( new Runnable() { @Override public void run() { Message loMsg; // 如果登录成功 if (loginPro()) // loginPro { loMsg = new Message(); loMsg.what = LOGIN_SUCCESS; moHandler.sendMessage(loMsg); } else { loMsg = new Message(); loMsg.what = LOGIN_FAILED; moHandler.sendMessage(loMsg); } } }) .start(); } } break; case MotionEvent.ACTION_UP: if ((int) event.getRawX() < miSliderMaxX) slideBack(); break; } } return false; }
@Override public boolean onTouchEvent(MotionEvent event) { if (velocityTracker == null) { // If we do not have velocity tracker velocityTracker = VelocityTracker.obtain(); // then get one } velocityTracker.addMovement(event); // add this movement to it positiveScroll = true; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { if (!flinger.isFinished()) { // If scrolling, then stop now flinger.forceFinished(); } currentX = (int) event.getRawX(); currentY = (int) event.getRawY(); break; } case MotionEvent.ACTION_MOVE: { final int x2 = (int) event.getRawX(); final int y2 = (int) event.getRawY(); final int diffX = currentX - x2; final int diffY = currentY - y2; currentX = x2; currentY = y2; scrollBy(diffX, diffY); break; } case MotionEvent.ACTION_UP: { final VelocityTracker velocityTracker = this.velocityTracker; velocityTracker.computeCurrentVelocity(1000, maximumVelocity); int velocityX = (int) velocityTracker.getXVelocity(); int velocityY = (int) velocityTracker.getYVelocity(); if (Math.abs(velocityX) > minimumVelocity || Math.abs(velocityY) > minimumVelocity) { flinger.start( getActualScrollX(), getActualScrollY(), velocityX, velocityY, getMaxScrollX(), getMaxScrollY()); } else { if (this.velocityTracker != null) { // If the velocity less than threshold this.velocityTracker.recycle(); // recycle the tracker this.velocityTracker = null; } } break; } } return true; }
private int getTabNum(MotionEvent ev) { if (isOverView(ev.getRawX(), ev.getRawY(), mDirectoryButton)) { return 0; } else if (isOverView(ev.getRawX(), ev.getRawY(), mRequestButton)) { return 1; } else if (isOverView(ev.getRawX(), ev.getRawY(), mSendButton)) { return 2; } else if (isOverView(ev.getRawX(), ev.getRawY(), mWalletButton)) { return 3; } else if (isOverView(ev.getRawX(), ev.getRawY(), mSettingButton)) { return 4; } else return -1; }
/** * 设置焦点和测光区域 * * @param event */ public void focusOnTouch(MotionEvent event) { try { if (mCamera == null) { return; } Camera.Parameters parameters = mCamera.getParameters(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { int[] location = new int[2]; mSurfaceView.getLocationOnScreen(location); Rect focusRect = calculateTapArea( view_focus.getWidth(), view_focus.getHeight(), 1f, event.getRawX(), event.getRawY(), location[0], location[0] + mSurfaceView.getWidth(), location[1], location[1] + mSurfaceView.getHeight()); Rect meteringRect = calculateTapArea( view_focus.getWidth(), view_focus.getHeight(), 1.5f, event.getRawX(), event.getRawY(), location[0], location[0] + mSurfaceView.getWidth(), location[1], location[1] + mSurfaceView.getHeight()); if (parameters.getMaxNumFocusAreas() > 0) { List<Camera.Area> focusAreas = new ArrayList<Camera.Area>(); focusAreas.add(new Camera.Area(focusRect, 1000)); parameters.setFocusAreas(focusAreas); } if (parameters.getMaxNumMeteringAreas() > 0) { List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>(); meteringAreas.add(new Camera.Area(meteringRect, 1000)); parameters.setMeteringAreas(meteringAreas); } } parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); mCamera.setParameters(parameters); mCamera.autoFocus(this); } catch (Exception e) { e.printStackTrace(); } }
@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; }
/** * Determines if given points are inside view. * * @param event the event * @param view - view object to compare * @return true if the points are within view bounds, false otherwise * @author RayBa * @date 07.04.2013 */ private boolean isPointInsideView(MotionEvent event, View view) { int location[] = new int[2]; view.getLocationOnScreen(location); int viewX = location[0]; int viewY = location[1]; // point is inside view bounds if ((event.getRawX() > (viewX - checkboxTouchPadding) && event.getRawX() < (viewX + view.getWidth() + checkboxTouchPadding))) { // event.setLocation(viewX, viewY); return true; } else { return false; } }
/** * MotionEvent has no getRawX(int) method; simulate it pending future API approval. * * @param event * @param pointerIndex * @return */ protected static float getRawX(MotionEvent event, int pointerIndex) { float offset = event.getX() - event.getRawX(); if (pointerIndex < event.getPointerCount()) { return event.getX(pointerIndex) + offset; } return 0f; }
/** * 自定义移动距离 * * @param event * @return */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX(); int deltaX = tempX - moveX; tempX = moveX; System.out.println("downX:" + downX); // downX为移动的最初点 if (moveX - downX > mTouchSlop && Math.abs((int) event.getRawY() - downY) < mTouchSlop) { isSilding = true; } // 慢慢滑动看不到效果滑动?? 设置项目的样式为半透明的样式,再设置布局的背景 if (moveX - downX >= 0 && isSilding) { mContentView.scrollBy(deltaX, 0); } break; case MotionEvent.ACTION_UP: isSilding = false; System.out.println(mContentView.getScrollX()); if (mContentView.getScrollX() <= -viewWidth / 20) { isFinish = true; scrollRight(); } else { scrollOrigin(); isFinish = false; } break; } return true; }
/** 滑动中,设置 view 的位置和透明度 */ private boolean actionMove(MotionEvent motionEvent) { if (mVelocityTracker == null || mPaused) { return false; } // 更新速度 mVelocityTracker.addMovement(motionEvent); // x y 轴的增量 float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; // 不是滑动中并且有横向移动且横向移动大于纵向移动 if (!mSwiping && Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { // 设置为滑动中 mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); } if (mSwiping) { mDownView.setTranslationX(deltaX - mSwipingSlop); mDownView.setAlpha( Math.max(0f, Math.min(mAlpha, mAlpha * (1f - Math.abs(deltaX) / mViewWidth)))); return true; } return false; }
@Override public boolean onTouchEvent(MotionEvent event) { // 获取相对屏幕的坐标,即以屏幕左上角为原点 x = event.getRawX(); y = event.getRawY() - 25; // 25是系统状态栏的高度 Log.i("currP", "currX" + x + "====currY" + y); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 获取相对View的坐标,即以此View左上角为原点 mTouchStartX = event.getX(); mTouchStartY = event.getY(); Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY); break; case MotionEvent.ACTION_MOVE: updateViewPosition(); break; case MotionEvent.ACTION_UP: updateViewPosition(); mTouchStartX = mTouchStartY = 0; break; } return true; }
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: return true; case MotionEvent.ACTION_UP: int x = getRight() - getCompoundDrawables()[2].getBounds().width(); if (event.getRawX() < x) { return false; } ViewUtil.showConfirmDialog( R.string.delete_group_title, R.string.delete_group_text, getContext(), true, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mListener.onDeleteRepetition(mID); } }); return true; default: return false; } }
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX(); int deltaX = tempX - moveX; tempX = moveX; if (moveX - downX > mTouchSlop && Math.abs((int) event.getRawY() - downY) < mTouchSlop) { isSilding = true; } if (moveX - downX >= 0 && isSilding) { mContentView.scrollBy(deltaX, 0); } break; case MotionEvent.ACTION_UP: isSilding = false; if (mContentView.getScrollX() <= -viewWidth / 2) { isFinish = true; scrollRight(); } else { scrollOrigin(); isFinish = false; } break; } return true; }
@Override public boolean onTouchEvent(MotionEvent ev) { // Log.d("DragLayer","onTouchEvent"); boolean handled = false; int action = ev.getAction(); int x = (int) ev.getX(); int y = (int) ev.getY(); if (ev.getAction() == MotionEvent.ACTION_DOWN) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { if (handleTouchDown(ev, false)) { return true; } } } if (mCurrentResizeFrame != null) { handled = true; switch (action) { case MotionEvent.ACTION_MOVE: mCurrentResizeFrame.visualizeResizeForDelta(x - mXDown, y - mYDown); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: mCurrentResizeFrame.visualizeResizeForDelta(x - mXDown, y - mYDown); mCurrentResizeFrame.onTouchUp(); mCurrentResizeFrame = null; } } if (handled) return true; return mDragController.onTouchEvent(ev.getRawX(), ev.getRawY(), ev); }
/** * if the view (v) wants to handle this event. * * @param v * @param event * @return */ private boolean childNeedHandleTouchEvent(View v, MotionEvent event) { if (v == null) return false; int[] loc = new int[2]; v.getLocationOnScreen(loc); int left = loc[0], top = loc[1]; if (event.getRawX() > left && event.getRawX() < left + v.getWidth() && event.getRawY() > top && event.getRawY() < top + v.getHeight()) { return v.onTouchEvent(event); } return false; }
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: return true; case MotionEvent.ACTION_UP: if (event.getRawX() >= (v.getRight() - ((TextView) v).getCompoundDrawables()[2].getBounds().width())) { // 响应右侧图标点击事件 Intent intent = new Intent(context, TerminalSubmitActivity.class); intent.putExtra("operate", "modify"); intent.putExtra("terminalid", ((String) v.getTag()).split(",")[0]); ((Activity) context).startActivityForResult(intent, 0); } else { // 响应自己的点击事件 if (!v.performClick()) { // 响应ListView点击事件 int position = Integer.parseInt(((String) v.getTag()).split(",")[1]) + 1; listView.performItemClick( listView.getChildAt(position - listView.getFirstVisiblePosition()), position, listView.getItemIdAtPosition(position)); } } } return false; }
/** * Check if we have a sloppy gesture. Sloppy gestures can happen if the edge of the user's hand is * touching the screen, for example. * * @param event * @return */ protected boolean isSloppyGesture(MotionEvent event) { // As orientation can change, query the metrics in touch down DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); mRightSlopEdge = metrics.widthPixels - mEdgeSlop; mBottomSlopEdge = metrics.heightPixels - mEdgeSlop; final float edgeSlop = mEdgeSlop; final float rightSlop = mRightSlopEdge; final float bottomSlop = mBottomSlopEdge; final float x0 = event.getRawX(); final float y0 = event.getRawY(); final float x1 = getRawX(event, 1); final float y1 = getRawY(event, 1); boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop; boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop; if (p0sloppy && p1sloppy) { return true; } else if (p0sloppy) { return true; } else if (p1sloppy) { return true; } return false; }
// handles touch event @Override public boolean onTouchEvent(MotionEvent event) { if (!isFlinging) { int orgLeft = ring.getPaddingLeft(); int orgTop = ring.getPaddingTop(); int left = (int) (event.getRawX() - ringWidth / 2 - 10); int top = (int) (event.getRawY() - ringHeight / 2 - 20); // check touch position int dist = (left - orgLeft) * (left - orgLeft) + (top - orgTop) * (top - orgTop); if (DBG) Log.i(TAG, "distance : " + dist); if (dist < ringWidth * ringHeight) { // move the ring moveRing(left, top); // and check flinging // gestureDetector.onTouchEvent(event); // released? if (event.getAction() == MotionEvent.ACTION_UP) { // check place, if ((int) (top + ringWidth / 2) < 3 * mDisplayHeight / 4) { // start moving to center animation startAnimateToCenter(150); } } } } return false; }