List of usage examples for android.view MotionEvent getRawX
public final float getRawX()
From source file:org.connectbot.ConsoleFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.frg_console, container, false); this.inflater = inflater; flip = (ViewFlipper) v.findViewById(R.id.console_flip); empty = (TextView) v.findViewById(android.R.id.empty); stringPromptGroup = (RelativeLayout) v.findViewById(R.id.console_password_group); stringPromptInstructions = (TextView) v.findViewById(R.id.console_password_instructions); stringPrompt = (EditText) v.findViewById(R.id.console_password); stringPrompt.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_UP) return false; if (keyCode != KeyEvent.KEYCODE_ENTER) return false; // pass collected password down to current terminal String value = stringPrompt.getText().toString(); PromptHelper helper = getCurrentPromptHelper(); if (helper == null) return false; helper.setResponse(value);//from ww w . j av a2 s.com // finally clear password for next user stringPrompt.setText(""); updatePromptVisible(); return true; } }); booleanPromptGroup = (RelativeLayout) v.findViewById(R.id.console_boolean_group); booleanPrompt = (TextView) v.findViewById(R.id.console_prompt); booleanYes = (Button) v.findViewById(R.id.console_prompt_yes); booleanYes.setOnClickListener(new OnClickListener() { public void onClick(View v) { PromptHelper helper = getCurrentPromptHelper(); if (helper == null) return; helper.setResponse(Boolean.TRUE); updatePromptVisible(); } }); booleanNo = (Button) v.findViewById(R.id.console_prompt_no); booleanNo.setOnClickListener(new OnClickListener() { public void onClick(View v) { PromptHelper helper = getCurrentPromptHelper(); if (helper == null) return; helper.setResponse(Boolean.FALSE); updatePromptVisible(); } }); // preload animations for terminal switching slide_left_in = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_in); slide_left_out = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_out); slide_right_in = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_in); slide_right_out = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_out); fade_out_delayed = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out_delayed); fade_stay_hidden = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_stay_hidden); // Preload animation for keyboard button keyboard_fade_in = AnimationUtils.loadAnimation(getActivity(), R.anim.keyboard_fade_in); keyboard_fade_out = AnimationUtils.loadAnimation(getActivity(), R.anim.keyboard_fade_out); inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); final RelativeLayout keyboardGroup = (RelativeLayout) v.findViewById(R.id.keyboard_group); mKeyboardButton = (ImageView) v.findViewById(R.id.button_keyboard); mKeyboardButton.setOnClickListener(new OnClickListener() { public void onClick(View view) { View flip = findCurrentView(R.id.console_flip); if (flip == null) return; inputManager.showSoftInput(flip, InputMethodManager.SHOW_FORCED); keyboardGroup.setVisibility(View.GONE); } }); final ImageView ctrlButton = (ImageView) v.findViewById(R.id.button_ctrl); ctrlButton.setOnClickListener(new OnClickListener() { public void onClick(View view) { View flip = findCurrentView(R.id.console_flip); if (flip == null) return; TerminalView terminal = (TerminalView) flip; TerminalKeyListener handler = terminal.bridge.getKeyHandler(); handler.metaPress(TerminalKeyListener.META_CTRL_ON); keyboardGroup.setVisibility(View.GONE); } }); final ImageView escButton = (ImageView) v.findViewById(R.id.button_esc); escButton.setOnClickListener(new OnClickListener() { public void onClick(View view) { View flip = findCurrentView(R.id.console_flip); if (flip == null) return; TerminalView terminal = (TerminalView) flip; TerminalKeyListener handler = terminal.bridge.getKeyHandler(); handler.sendEscape(); keyboardGroup.setVisibility(View.GONE); } }); // detect fling gestures to switch between terminals final GestureDetector detect = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { private float totalY = 0; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { final float distx = e2.getRawX() - e1.getRawX(); final float disty = e2.getRawY() - e1.getRawY(); final int goalwidth = flip.getWidth() / 2; // need to slide across half of display to trigger console change // make sure user kept a steady hand horizontally if (Math.abs(disty) < (flip.getHeight() / 4)) { if (distx > goalwidth) { shiftCurrentTerminal(SHIFT_RIGHT); return true; } if (distx < -goalwidth) { shiftCurrentTerminal(SHIFT_LEFT); return true; } } return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // if copying, then ignore if (copySource != null && copySource.isSelectingForCopy()) return false; if (e1 == null || e2 == null) return false; // if releasing then reset total scroll if (e2.getAction() == MotionEvent.ACTION_UP) { totalY = 0; } // activate consider if within x tolerance if (Math.abs(e1.getX() - e2.getX()) < ViewConfiguration.getTouchSlop() * 4) { View flip = findCurrentView(R.id.console_flip); if (flip == null) return false; TerminalView terminal = (TerminalView) flip; // estimate how many rows we have scrolled through // accumulate distance that doesn't trigger immediate scroll totalY += distanceY; final int moved = (int) (totalY / terminal.bridge.charHeight); // consume as scrollback only if towards right half of screen if (e2.getX() > flip.getWidth() / 2) { if (moved != 0) { int base = terminal.bridge.buffer.getWindowBase(); terminal.bridge.buffer.setWindowBase(base + moved); totalY = 0; return true; } } else { // otherwise consume as pgup/pgdown for every 5 lines if (moved > 5) { ((vt320) terminal.bridge.buffer).keyPressed(vt320.KEY_PAGE_DOWN, ' ', 0); terminal.bridge.tryKeyVibrate(); totalY = 0; return true; } else if (moved < -5) { ((vt320) terminal.bridge.buffer).keyPressed(vt320.KEY_PAGE_UP, ' ', 0); terminal.bridge.tryKeyVibrate(); totalY = 0; return true; } } } return false; } }); flip.setLongClickable(true); flip.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // when copying, highlight the area if (copySource != null && copySource.isSelectingForCopy()) { int row = (int) Math.floor(event.getY() / copySource.charHeight); int col = (int) Math.floor(event.getX() / copySource.charWidth); SelectionArea area = copySource.getSelectionArea(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // recording starting area if (area.isSelectingOrigin()) { area.setRow(row); area.setColumn(col); lastTouchRow = row; lastTouchCol = col; copySource.redraw(); } return true; case MotionEvent.ACTION_MOVE: /* ignore when user hasn't moved since last time so * we can fine-tune with directional pad */ if (row == lastTouchRow && col == lastTouchCol) return true; // if the user moves, start the selection for other corner area.finishSelectingOrigin(); // update selected area area.setRow(row); area.setColumn(col); lastTouchRow = row; lastTouchCol = col; copySource.redraw(); return true; case MotionEvent.ACTION_UP: /* If they didn't move their finger, maybe they meant to * select the rest of the text with the directional pad. */ if (area.getLeft() == area.getRight() && area.getTop() == area.getBottom()) { return true; } // copy selected area to clipboard String copiedText = area.copyFrom(copySource.buffer); clipboard.setText(copiedText); Toast.makeText(getActivity(), getString(R.string.console_copy_done, copiedText.length()), Toast.LENGTH_LONG).show(); // fall through to clear state case MotionEvent.ACTION_CANCEL: // make sure we clear any highlighted area area.reset(); copySource.setSelectingForCopy(false); copySource.redraw(); return true; } } Configuration config = getResources().getConfiguration(); if (event.getAction() == MotionEvent.ACTION_DOWN) { lastX = event.getX(); lastY = event.getY(); } else if (event.getAction() == MotionEvent.ACTION_UP && keyboardGroup.getVisibility() == View.GONE && event.getEventTime() - event.getDownTime() < CLICK_TIME && Math.abs(event.getX() - lastX) < MAX_CLICK_DISTANCE && Math.abs(event.getY() - lastY) < MAX_CLICK_DISTANCE) { keyboardGroup.startAnimation(keyboard_fade_in); keyboardGroup.setVisibility(View.VISIBLE); mUIHandler.postDelayed(new Runnable() { public void run() { if (keyboardGroup.getVisibility() == View.GONE) return; keyboardGroup.startAnimation(keyboard_fade_out); keyboardGroup.setVisibility(View.GONE); } }, KEYBOARD_DISPLAY_TIME); } // pass any touch events back to detector return detect.onTouchEvent(event); } }); return v; }
From source file:com.wunderlist.slidinglayer.SlidingLayer.java
private float getViewX(MotionEvent event) { return event.getRawX(); }
From source file:com.grottworkshop.gwsswipelayout.SwipeLayout.java
/** * if the view (v) wants to handle this event. * @param v the v// w ww . j a va 2 s. c o m * @param event the event * @return v touch event or false */ 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]; return event.getRawX() > left && event.getRawX() < left + v.getWidth() && event.getRawY() > top && event.getRawY() < top + v.getHeight() && v.onTouchEvent(event); }
From source file:com.androzic.MapFragment.java
@SuppressLint("ClickableViewAccessibility") @Override/*www . j a va 2s . c o m*/ public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: zoom100X = (int) event.getRawX(); zoom100Y = (int) event.getRawY(); zoomHandler.postDelayed(new Runnable() { @Override public void run() { zoom100X = 0; zoom100Y = 0; } }, 2000); break; case MotionEvent.ACTION_UP: int dx = Math.abs((int) event.getRawX() - zoom100X); int dy = (int) event.getRawY() - zoom100Y; int h = v.getHeight(); int w = v.getWidth() >> 1; if (dy > h * 0.8 && dy < h * 2 && dx < w) { wait(new Waitable() { @Override public void waitFor() { synchronized (map) { if (application.setZoom(1.)) { map.updateMapInfo(); map.updateMapCenter(); } } } }); zoom100X = 0; zoom100Y = 0; } break; } return false; }
From source file:net.osmand.plus.views.controls.SwipeDismissListViewTouchListener.java
@Override public boolean onTouch(View view, MotionEvent ev) { if (mViewWidth < 2) { mViewWidth = mListView.getWidth(); }/* www .j a v a 2 s . c o m*/ switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mSwipePaused) { return false; } // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mListView.getChildCount(); int[] listViewCoords = new int[2]; mListView.getLocationOnScreen(listViewCoords); int x = (int) ev.getRawX() - listViewCoords[0]; int y = (int) ev.getRawY() - listViewCoords[1]; View child; for (int i = mListView.getHeaderViewsCount(); i < childCount; i++) { child = mListView.getChildAt(i); if (child != null) { child.getHitRect(rect); if (rect.contains(x, y)) { // if a specific swiping layout has been giving, use this to swipe. if (mSwipingLayout > 0) { View swipingView = child.findViewById(mSwipingLayout); if (swipingView != null) { mSwipeDownView = swipingView; mSwipeDownChild = child; break; } } // If no swiping layout has been found, swipe the whole child mSwipeDownView = mSwipeDownChild = child; break; } } } if (mSwipeDownView != null) { // test if the item should be swiped int position = mListView.getPositionForView(mSwipeDownView) - mListView.getHeaderViewsCount(); if (mCallbacks == null || mCallbacks.canDismiss(position)) { mDownX = ev.getRawX(); mDownY = ev.getRawY(); mDownPosition = position; mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(ev); } else { // set back to null to revert swiping mSwipeDownView = mSwipeDownChild = null; } } return false; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } if (mSwipeDownView != null && mSwiping) { // cancel ViewCompat.animate(mSwipeDownView).translationX(0).alpha(1).setDuration(mAnimationTime) .setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDownY = 0; mSwipeDownView = mSwipeDownChild = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = ev.getRawX() - mDownX; mVelocityTracker.addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX && mSwiping && isSwipeDirectionValid(mVelocityTracker.getXVelocity()) && deltaX >= mViewWidth * 0.2f) { dismiss = true; dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss) { // dismiss slideOutView(mSwipeDownView, mSwipeDownChild, mDownPosition, dismissRight); } else if (mSwiping) { // Swipe back to regular position ViewCompat.animate(mSwipeDownView).translationX(0).alpha(1).setDuration(mAnimationTime) .setListener(null); } mVelocityTracker = null; mDownX = 0; mDownY = 0; mSwipeDownView = null; mSwipeDownChild = null; mDownPosition = AbsListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mSwipePaused) { break; } mVelocityTracker.addMovement(ev); float deltaX = ev.getRawX() - mDownX; float deltaY = ev.getRawY() - mDownY; // Only start swipe in correct direction if (isSwipeDirectionValid(deltaX)) { ViewParent parent = mListView.getParent(); if (parent != null) { // If we swipe don't allow parent to intercept touch (e.g. like NavigationDrawer does) // otherwise swipe would not be working. parent.requestDisallowInterceptTouchEvent(true); } if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mListView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(ev); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (ev.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mListView.onTouchEvent(cancelEvent); } } else { // If we swiped into wrong direction, act like this was the new // touch down point mDownX = ev.getRawX(); deltaX = 0; } if (mSwiping) { ViewCompat.setTranslationX(mSwipeDownView, deltaX); ViewCompat.setAlpha(mSwipeDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:de.timroes.swipetodismiss.SwipeDismissList.java
@Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mListView.getWidth(); }/* www . ja v a2 s . c o m*/ switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (mPaused || !mEnabled) { return false; } resetSwipeState(); // TODO: ensure this is a finger, and set a flag // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mListView.getChildCount(); int[] listViewCoords = new int[2]; mListView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mListView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { if (mSwipeLayout != 0) { View swipeView = child.findViewById(mSwipeLayout); if (swipeView != null) { mDownView = swipeView; break; } } mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); mDownPosition = mListView.getPositionForView(mDownView); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } view.onTouchEvent(motionEvent); return true; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX && mSwiping && isDirectionValid(mVelocityTracker.getXVelocity()) && deltaX >= mViewWidth * 0.2f) { dismiss = true; dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; mPaused = true; animate(mDownView).translationX(dismissRight ? mViewWidth : -mViewWidth).alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView).translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); } resetSwipeState(); break; } case MotionEvent.ACTION_MOVE: { if (mUndoPopup.isShowing()) { // Send a delayed message to hide popup mHandler.sendMessageDelayed(mHandler.obtainMessage(mDelayedMsgId), mAutoHideDelay); } if (mVelocityTracker == null || mPaused || !mEnabled) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; // Only start swipe in correct direction if (isDirectionValid(deltaX)) { if (Math.abs(deltaX) > mSlop && Math.abs(deltaX) > Math.abs(deltaY)) { mSwiping = true; mListView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mListView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } } else { // If we swiped into wrong direction, act like this was the new // touch down point mDownX = motionEvent.getRawX(); deltaX = 0; } if (mSwiping) { setTranslationX(mDownView, deltaX); setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:com.example.appdetail_optimization.HorizontalListView.java
@Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: scrollMode = SCROLL_MODE_IDLE;/* ww w . j a v a2 s . co m*/ downEventX = ev.getRawX(); downEventY = ev.getRawY(); isFullScroll = false; int lastVisiblePos = getLastVisiblePosition(); if (lastVisiblePos >= mAdapter.getCount() - 1) { View view = getChildAt(getChildCount() - 1); if (Math.abs(view.getRight() - getWidth()) < 10) { isFullScroll = true; } } getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: if (scrollMode == SCROLL_MODE_HORIZONTAL) { // view } else if (scrollMode == SCROLL_MODE_VERTICAL) { // viewtouch return false; } else if (scrollMode == SCROLL_MODE_IDLE) { float distanceX = Math.abs(ev.getRawX() - downEventX); float distanceY = Math.abs(ev.getRawY() - downEventY); if (distanceX > distanceY && distanceX > 5) { if (isFullScroll) { if (ev.getRawX() < downEventX) { Log.i("LeiTest", "viewpager"); scrollMode = SCROLL_MODE_VERTICAL; getParent().requestDisallowInterceptTouchEvent(false); return super.dispatchTouchEvent(ev); } } scrollMode = SCROLL_MODE_HORIZONTAL; getParent().requestDisallowInterceptTouchEvent(true); } else if (distanceY > distanceX && distanceY > 5) { scrollMode = SCROLL_MODE_VERTICAL; getParent().requestDisallowInterceptTouchEvent(false); return false; } } break; case MotionEvent.ACTION_UP: scrollMode = SCROLL_MODE_IDLE; getParent().requestDisallowInterceptTouchEvent(false); break; case MotionEvent.ACTION_CANCEL: scrollMode = SCROLL_MODE_IDLE; getParent().requestDisallowInterceptTouchEvent(false); break; default: break; } return super.dispatchTouchEvent(ev); }
From source file:com.shuxiangbaima.task.view.HorizontalListView.java
@Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: scrollMode = SCROLL_MODE_IDLE;//from ww w . j a va2s . c o m downEventX = ev.getRawX(); downEventY = ev.getRawY(); isFullScroll = false; int lastVisiblePos = getLastVisiblePosition(); if (lastVisiblePos >= mAdapter.getCount() - 1) { View view = getChildAt(getChildCount() - 1); if (Math.abs(view.getRight() - getWidth()) < 10) { isFullScroll = true; } } getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: if (scrollMode == SCROLL_MODE_HORIZONTAL) { // view } else if (scrollMode == SCROLL_MODE_VERTICAL) { // viewtouch return false; } else if (scrollMode == SCROLL_MODE_IDLE) { float distanceX = Math.abs(ev.getRawX() - downEventX); float distanceY = Math.abs(ev.getRawY() - downEventY); if (distanceX > distanceY && distanceX > 5) { if (isFullScroll) { if (ev.getRawX() < downEventX) { Log.i("LeiTest", "viewpager"); scrollMode = SCROLL_MODE_VERTICAL; getParent().requestDisallowInterceptTouchEvent(false); return super.dispatchTouchEvent(ev); } } scrollMode = SCROLL_MODE_HORIZONTAL; getParent().requestDisallowInterceptTouchEvent(true); } else if (distanceY > distanceX && distanceY > 5) { scrollMode = SCROLL_MODE_VERTICAL; getParent().requestDisallowInterceptTouchEvent(false); return false; } } break; case MotionEvent.ACTION_UP: scrollMode = SCROLL_MODE_IDLE; getParent().requestDisallowInterceptTouchEvent(false); break; case MotionEvent.ACTION_CANCEL: scrollMode = SCROLL_MODE_IDLE; getParent().requestDisallowInterceptTouchEvent(false); break; default: break; } return super.dispatchTouchEvent(ev); }
From source file:com.fancyfamily.primarylibrary.commentlibrary.widget.HorizontalListView.java
@Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: scrollMode = SCROLL_MODE_IDLE;/* w w w .j a v a 2 s . c o m*/ downEventX = ev.getRawX(); downEventY = ev.getRawY(); isFullScroll = false; int lastVisiblePos = getLastVisiblePosition(); if (lastVisiblePos >= mAdapter.getCount() - 1) { View view = getChildAt(getChildCount() - 1); if (view != null && Math.abs(view.getRight() - getWidth()) < 10) { isFullScroll = true; } } getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: if (scrollMode == SCROLL_MODE_HORIZONTAL) { // view } else if (scrollMode == SCROLL_MODE_VERTICAL) { // viewtouch return false; } else if (scrollMode == SCROLL_MODE_IDLE) { float distanceX = Math.abs(ev.getRawX() - downEventX); float distanceY = Math.abs(ev.getRawY() - downEventY); if (distanceX > distanceY && distanceX > 5) { if (isFullScroll) { if (ev.getRawX() < downEventX) { Log.i("LeiTest", "viewpager"); scrollMode = SCROLL_MODE_VERTICAL; getParent().requestDisallowInterceptTouchEvent(false); return super.dispatchTouchEvent(ev); } } scrollMode = SCROLL_MODE_HORIZONTAL; getParent().requestDisallowInterceptTouchEvent(true); } else if (distanceY > distanceX && distanceY > 5) { scrollMode = SCROLL_MODE_VERTICAL; getParent().requestDisallowInterceptTouchEvent(false); return false; } } break; case MotionEvent.ACTION_UP: scrollMode = SCROLL_MODE_IDLE; getParent().requestDisallowInterceptTouchEvent(false); break; case MotionEvent.ACTION_CANCEL: scrollMode = SCROLL_MODE_IDLE; getParent().requestDisallowInterceptTouchEvent(false); break; default: break; } return super.dispatchTouchEvent(ev); }
From source file:com.htc.dotdesign.ToolBoxService.java
private void initDragButton() { //Initialize drag button if (mDragButton != null) { mCurrFuncIcon = (ImageView) mDragButton.findViewById(R.id.curr_func_icon); if (mDragBtnColorIcon != null) { mDragBtnColorIcon.setColor(mCurrBrushColor); mCurrFuncIcon.setBackground(mDragBtnColorIcon); }/*from ww w. java2 s. c o m*/ mDragButton.setOnTouchListener(new OnTouchListener() { //private int initialX; private int initialY; private float initialTouchX; private float initialTouchY; private boolean mIsMoving = false; private boolean mIsTryMoving = false; @Override public boolean onTouch(View v, MotionEvent event) { Log.d(DotDesignConstants.LOG_TAG, LOG_PREFIX + "onTouch"); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(DotDesignConstants.LOG_TAG, LOG_PREFIX + "onTouch, ACTION_DOWN"); //initialX = mDragButtonParams.x; initialY = mDragButtonParams.y; initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); break; case MotionEvent.ACTION_UP: Log.d(DotDesignConstants.LOG_TAG, LOG_PREFIX + "onTouch, ACTION_UP"); if (!mIsMoving && !mIsTryMoving) { if (!mbIsAnimationPlaying) { if (mIsToolBarOpen) { if (mIsToolBarExtend) { setToolPanelVisibility(false); } else { mToolBar.clearAnimation(); if (mCurrLayoutMode == LayoutMode.LEFT_SIDE) { playAnimation(mToolBar, mLeftOut); } else if (mCurrLayoutMode == LayoutMode.RIGHT_SIDE) { playAnimation(mToolBar, mRightOut); } } } else { if (mCurrLayoutMode == LayoutMode.LEFT_SIDE) { mToolBarParams.gravity = Gravity.START; } else if (mCurrLayoutMode == LayoutMode.RIGHT_SIDE) { mToolBarParams.gravity = Gravity.END; } mToolBarParams.gravity = Gravity.TOP | mToolBarParams.gravity; mToolBarParams.x = 0; mToolBarParams.y = mDragButtonParams.y; mWindowManager.updateViewLayout(mToolBarParent, mToolBarParams); mToolBarParent.setVisibility(View.VISIBLE); mToolBar.clearAnimation(); mToolBar.setVisibility(View.VISIBLE); if (mCurrLayoutMode == LayoutMode.LEFT_SIDE) { playAnimation(mToolBar, mLeftIn); } else if (mCurrLayoutMode == LayoutMode.RIGHT_SIDE) { playAnimation(mToolBar, mRightIn); } } } } else { mIsTryMoving = false; mIsMoving = false; /*if (mDragButtonParams.x <= mScreenWidth / 2) { mDragButtonParams.x = 0; mWindowManager.updateViewLayout(mDragButton, mDragButtonParams); mCurrLayoutMode = LayoutMode.LEFT_SIDE; } else { mDragButtonParams.x = mScreenWidth - mDragButtonWidth; mWindowManager.updateViewLayout(mDragButton, mDragButtonParams); mCurrLayoutMode = LayoutMode.RIGHT_SIDE; }*/ } // mDragButton.setBackgroundResource(R.drawable.floating2); break; case MotionEvent.ACTION_MOVE: Log.d(DotDesignConstants.LOG_TAG, LOG_PREFIX + "onTouch, ACTION_MOVE, mToolBox.getHeight()" + mToolBar.getHeight()); if (Math.abs(event.getRawX() - initialTouchX) > 10 || Math.abs(event.getRawY() - initialTouchY) > 10) { mIsTryMoving = true; } if (/*!mIsToolBarExtend && */mIsTryMoving) { mIsMoving = true; // paramsF.x = initialX + (int) (event.getRawX() - initialTouchX); mDragButtonParams.y = initialY + (int) (event.getRawY() - initialTouchY); if (mDragButtonParams.y < 0) { mDragButtonParams.y = 0; } else if (mDragButtonParams.y > mScreenHeight - mDragButtonHeight) { mDragButtonParams.y = mScreenHeight - mDragButtonHeight; } mWindowManager.updateViewLayout(mDragButton, mDragButtonParams); // mDragButton.setBackgroundResource(R.drawable.ic_launcher); if (mIsToolBarOpen) { mToolBarParams.y = mDragButtonParams.y; mWindowManager.updateViewLayout(mToolBarParent, mToolBarParams); } if (mIsToolBarExtend) { setToolPanelVisibility(false); } } break; } return true; } }); } }