android.view.MotionEvent#getAction ( )源码实例Demo

下面列出了android.view.MotionEvent#getAction ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            is_moving = false;
            down_x = event.getRawX();
            down_y = event.getRawY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (!is_moving && (Math.abs(event.getRawX() - down_x) > touch_slop || Math.abs(event.getRawY() - down_y) > touch_slop)) {
                is_moving = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (!is_moving) {
                performClick();
            }
            break;
    }
    return true;
}
 
源代码2 项目: XCL-Charts   文件: RangeBarChart01View.java
@Override
public boolean onTouchEvent(MotionEvent event) {
	// TODO Auto-generated method stub		
	super.onTouchEvent(event);		
	if(event.getAction() == MotionEvent.ACTION_UP) 
	{		
		triggerClick(event.getX(),event.getY());
	}
	return true;
}
 
源代码3 项目: oneHookLibraryAndroid   文件: ScaleView.java
@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();

    switch (action) {
        case (MotionEvent.ACTION_DOWN):
            mStartX = event.getX();
            mPreviousX = mStartX;
            return true;
        case (MotionEvent.ACTION_MOVE):
            final float yOffset = event.getX() - mPreviousX;
            mPreviousX = event.getX();

            /* modify raw translation based on panning direction, YES, natural panning
               introduced by apple. I dont get it, it doesn't feel natural to me.
             */
            mRawTranslationX += mIsNaturalPanning ? yOffset : -yOffset;
            enforceLimit();
            invalidateCurrentScale();
            invalidate();
            return true;
        case (MotionEvent.ACTION_UP):
        case (MotionEvent.ACTION_CANCEL):
        case (MotionEvent.ACTION_OUTSIDE):
            mStartX = -1;
            snap();
            invalidateCurrentScale();
            return true;
        default:
            return super.onTouchEvent(event);
    }
}
 
源代码4 项目: TurboLauncher   文件: DragLayer.java
@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean handled = false;
    int action = ev.getAction();

    int x = (int) ev.getX();
    int y = (int) ev.getY();

    if (action == MotionEvent.ACTION_DOWN) {
        if (handleTouchDown(ev, false)) {
            return true;
        }
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        if (mTouchCompleteListener != null) {
            mTouchCompleteListener.onTouchComplete();
        }
        mTouchCompleteListener = null;
    }

    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);
}
 
源代码5 项目: DragPointView   文件: DragViewHelper.java
@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction() & MotionEvent.ACTION_MASK;
    if (action == MotionEvent.ACTION_DOWN) {
        ViewParent parent = v.getParent();
        if (parent == null) {
            return false;
        }
        parent.requestDisallowInterceptTouchEvent(true);
        addViewToWindow();
    }
    return windowView.onTouchEvent(event);
}
 
源代码6 项目: AndroidFaceRecognizer   文件: MainPageActivity.java
@Override
public boolean onTouch(View v, MotionEvent event) {
	int action = event.getAction();

	switch (action) {
	case MotionEvent.ACTION_DOWN:				
		
		break;
	case MotionEvent.ACTION_UP:
		break;
	default:
		break;
	}
	return false;
}
 
源代码7 项目: LB-Launcher   文件: DragLayer.java
@Override
public boolean onInterceptHoverEvent(MotionEvent ev) {
    if (mLauncher == null || mLauncher.getWorkspace() == null) {
        return false;
    }
    Folder currentFolder = mLauncher.getWorkspace().getOpenFolder();
    if (currentFolder == null) {
        return false;
    } else {
            AccessibilityManager accessibilityManager = (AccessibilityManager)
                    getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
        if (accessibilityManager.isTouchExplorationEnabled()) {
            final int action = ev.getAction();
            boolean isOverFolder;
            switch (action) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    isOverFolder = isEventOverFolder(currentFolder, ev);
                    if (!isOverFolder) {
                        sendTapOutsideFolderAccessibilityEvent(currentFolder.isEditingName());
                        mHoverPointClosesFolder = true;
                        return true;
                    }
                    mHoverPointClosesFolder = false;
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    isOverFolder = isEventOverFolder(currentFolder, ev);
                    if (!isOverFolder && !mHoverPointClosesFolder) {
                        sendTapOutsideFolderAccessibilityEvent(currentFolder.isEditingName());
                        mHoverPointClosesFolder = true;
                        return true;
                    } else if (!isOverFolder) {
                        return true;
                    }
                    mHoverPointClosesFolder = false;
            }
        }
    }
    return false;
}
 
源代码8 项目: opengl   文件: MyGLSurfaceView.java
@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:

            float dx = x - mPreviousX;
            float dy = y - mPreviousY;

            // reverse direction of rotation above the mid-line
            if (y > getHeight() / 2) {
                dx = dx * -1 ;
            }

            // reverse direction of rotation to left of the mid-line
            if (x < getWidth() / 2) {
                dy = dy * -1 ;
            }

            mRenderer.setAngle(
                    mRenderer.getAngle() +
                    ((dx + dy) * TOUCH_SCALE_FACTOR));  // = 180.0f / 320
            requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}
 
@Override
public boolean onTouchEvent(MotionEvent event) {
    scaleDetector.onTouchEvent(event);

    if (zooming) {
        invalidate();
        zooming = false;
        return true;
    }

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            dragX = event.getX();
            dragY = event.getY();

            getParent().requestDisallowInterceptTouchEvent(true);
            dragged = false;
            return true;

        case MotionEvent.ACTION_UP:
            getParent().requestDisallowInterceptTouchEvent(false);
            return true;

        case MotionEvent.ACTION_MOVE:
            float newX = event.getX();
            float newY = event.getY();

            scrollScreen(dragX - newX, dragY - newY);

            dragX = newX;
            dragY = newY;
            dragged = true;
            return true;
        default:
            return false;
    }
}
 
源代码10 项目: CoolViewPager   文件: CoolViewPager.java
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (timer != null) {
        final int action = ev.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            stopTimer();
        }
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            checkAndStartTimer();
        }
    }
    return super.dispatchTouchEvent(ev);
}
 
源代码11 项目: EazeGraph   文件: VerticalBarChart.java
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        performClick();
        return true;
    } else {
        return false;
    }
}
 
源代码12 项目: AssistantBySDK   文件: WheelView.java
@Override
public boolean onTouchEvent(MotionEvent event) {
	if (!isEnabled() || getViewAdapter() == null) {
		return true;
	}
	
	switch (event.getAction()) {
	    case MotionEvent.ACTION_MOVE:
	        if (getParent() != null) {
	            getParent().requestDisallowInterceptTouchEvent(true);
	        }
	        break;
	        
	    case MotionEvent.ACTION_UP:
	        if (!isScrollingPerformed) {
	            int distance = (int) event.getY() - getHeight() / 2;
	            if (distance > 0) {
	                distance += getItemHeight() / 2;
	            } else {
                       distance -= getItemHeight() / 2;
	            }
	            int items = distance / getItemHeight();
	            if (items != 0 && isValidItemIndex(currentItem + items)) {
                    notifyClickListenersAboutClick(currentItem + items);
	            }
	        }
	        break;
	}
	return scroller.onTouchEvent(event);
}
 
源代码13 项目: CardLayoutManager   文件: CardItemView.java
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (!isEventInPath(event)) { return false;}
    }

    return super.dispatchTouchEvent(event);
}
 
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // If the scrollable view is handling touch, never intercept
    if (mIsScrollableViewHandlingTouch || !isTouchEnabled()) {
        mDragHelper.abort();
        return false;
    }

    final int action = ev.getAction();
    final float x = ev.getX();
    final float y = ev.getY();
    final float adx = Math.abs(x - mInitialMotionX);
    final float ady = Math.abs(y - mInitialMotionY);
    final int dragSlop = mDragHelper.getTouchSlop();

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mIsUnableToDrag = false;
            mInitialMotionX = x;
            mInitialMotionY = y;
            if (!isViewUnder(mDragView, (int) x, (int) y)) {
                mDragHelper.cancel();
                mIsUnableToDrag = true;
                return false;
            }

            break;
        }

        case MotionEvent.ACTION_MOVE: {
            if (ady > dragSlop && adx > ady) {
                mDragHelper.cancel();
                mIsUnableToDrag = true;
                return false;
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            // If the dragView is still dragging when we get here, we need to call processTouchEvent
            // so that the view is settled
            // Added to make scrollable views work (tokudu)
            if (mDragHelper.isDragging()) {
                mDragHelper.processTouchEvent(ev);
                return true;
            }
            // Check if this was a click on the faded part of the screen, and fire off the listener if there is one.
            if (ady <= dragSlop
                    && adx <= dragSlop
                    && mSlideOffset > 0 && !isViewUnder(mSlideableView, (int) mInitialMotionX, (int) mInitialMotionY) && mFadeOnClickListener != null) {
                playSoundEffect(android.view.SoundEffectConstants.CLICK);
                mFadeOnClickListener.onClick(this);
                return true;
            }
            break;
    }
    return mDragHelper.shouldInterceptTouchEvent(ev);
}
 
源代码15 项目: SprintNBA   文件: StickyNavLayout.java
@Override
public boolean onTouchEvent(MotionEvent event) {
    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(event);
    int action = event.getAction();
    float y = event.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            if (!mScroller.isFinished())
                mScroller.abortAnimation();
            mLastY = y;
            return true;
        case MotionEvent.ACTION_MOVE:
            float dy = y - mLastY;

            if (!mDragging && Math.abs(dy) > mTouchSlop) {
                mDragging = true;
            }
            if (mDragging) {
                scrollBy(0, (int) -dy);
                // 如果topView隐藏,且上滑动时,则改变当前事件为ACTION_DOWN
                if (getScrollY() == mTopViewHeight && dy < 0) {
                    event.setAction(MotionEvent.ACTION_DOWN);
                    dispatchTouchEvent(event);
                    isInControl = false;
                    isSticky = true;
                } else {
                    isSticky = false;
                }
            }
            mLastY = y;
            break;
        case MotionEvent.ACTION_CANCEL:
            mDragging = false;
            recycleVelocityTracker();
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            break;
        case MotionEvent.ACTION_UP:
            mDragging = false;
            mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityY = (int) mVelocityTracker.getYVelocity();
            if (Math.abs(velocityY) > mMinimumVelocity) {
                fling(-velocityY);
            }
            recycleVelocityTracker();
            break;
    }

    return super.onTouchEvent(event);
}
 
源代码16 项目: ChatKeyboard   文件: ChatKeyboardLayout.java
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(mContext, Manifest.permission
            .WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (mOnChatKeyBoardListener != null) {
            mOnChatKeyBoardListener.onRecordingAction(RecordingAction
                    .PERMISSION_NOT_GRANTED);
        }
        return true;
    }
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        startY = motionEvent.getRawY();
        btnRecording.setText(getResources().getString(R.string.recording_end));
        btnRecording.setBackgroundResource(R.drawable.recording_p);
        if (mOnChatKeyBoardListener != null) {
            mOnChatKeyBoardListener.onRecordingAction(RecordingAction.START);
        }
    } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        btnRecording.setText(getResources().getString(R.string.recording_start));
        btnRecording.setBackgroundResource(R.drawable.recording_n);
        if (mOnChatKeyBoardListener != null && !isCanceled) {
            mOnChatKeyBoardListener.onRecordingAction(RecordingAction.COMPLETE);
        } else if (mOnChatKeyBoardListener != null) {
            mOnChatKeyBoardListener.onRecordingAction(RecordingAction.CANCELED);
        }
    } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
        endY = motionEvent.getRawY();
        if (startY - endY > Utils.dip2px(mContext, 50)) {
            btnRecording.setText(getResources().getString(R.string.recording_cancel));
            isCanceled = true;
            if (mOnChatKeyBoardListener != null) {
                mOnChatKeyBoardListener.onRecordingAction(RecordingAction.READY_CANCEL);
            }
        } else {
            if (mOnChatKeyBoardListener != null) {
                mOnChatKeyBoardListener.onRecordingAction(RecordingAction.RESTORE);
            }
            btnRecording.setText(getResources().getString(R.string.recording_end));
            isCanceled = false;
        }
    }
    return false;
}
 
源代码17 项目: Android-Lock9View   文件: Lock9View.java
/**
 * 在这里处理手势
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            x = event.getX(); // 这里要实时记录手指的坐标
            y = event.getY();
            NodeView currentNode = getNodeAt(x, y);
            if (currentNode != null && !currentNode.isHighLighted()) { // 碰触了新的未点亮节点
                if (nodeList.size() > 0) { // 之前有点亮的节点
                    if (autoLink) { // 开启了中间节点自动连接
                        NodeView lastNode = nodeList.get(nodeList.size() - 1);
                        NodeView middleNode = getNodeBetween(lastNode, currentNode);
                        if (middleNode != null && !middleNode.isHighLighted()) { // 存在中间节点没点亮
                            // 点亮中间节点
                            middleNode.setHighLighted(true, true);
                            nodeList.add(middleNode);
                            handleOnNodeConnectedCallback();
                        }
                    }
                }
                // 点亮当前触摸节点
                currentNode.setHighLighted(true, false);
                nodeList.add(currentNode);
                handleOnNodeConnectedCallback();
            }
            // 有点亮的节点才重绘
            if (nodeList.size() > 0) {
                invalidate();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (nodeList.size() > 0) { // 有点亮的节点
                // 手势完成
                handleOnGestureFinishedCallback();
                // 清除状态
                nodeList.clear();
                for (int n = 0; n < getChildCount(); n++) {
                    NodeView node = (NodeView) getChildAt(n);
                    node.setHighLighted(false, false);
                }
                // 通知重绘
                invalidate();
            }
            break;
    }
    return true;
}
 
源代码18 项目: iMoney   文件: UnderlinePageIndicator.java
public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            mLastMotionX = ev.getX();
            break;

        case MotionEvent.ACTION_MOVE: {
            final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            final float x = MotionEventCompat.getX(ev, activePointerIndex);
            final float deltaX = x - mLastMotionX;

            if (!mIsDragging) {
                if (Math.abs(deltaX) > mTouchSlop) {
                    mIsDragging = true;
                }
            }

            if (mIsDragging) {
                mLastMotionX = x;
                if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                    mViewPager.fakeDragBy(deltaX);
                }
            }

            break;
        }

        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            if (!mIsDragging) {
                final int count = mViewPager.getAdapter().getCount();
                final int width = getWidth();
                final float halfWidth = width / 2f;
                final float sixthWidth = width / 6f;

                if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage - 1);
                    }
                    return true;
                } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                    if (action != MotionEvent.ACTION_CANCEL) {
                        mViewPager.setCurrentItem(mCurrentPage + 1);
                    }
                    return true;
                }
            }

            mIsDragging = false;
            mActivePointerId = INVALID_POINTER;
            if (mViewPager.isFakeDragging()) mViewPager.endFakeDrag();
            break;

        case MotionEventCompat.ACTION_POINTER_DOWN: {
            final int index = MotionEventCompat.getActionIndex(ev);
            mLastMotionX = MotionEventCompat.getX(ev, index);
            mActivePointerId = MotionEventCompat.getPointerId(ev, index);
            break;
        }

        case MotionEventCompat.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);
            }
            mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
            break;
    }

    return true;
}
 
源代码19 项目: CharacterPickerView   文件: LoopView.java
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean eventConsumed = flingGestureDetector.onTouchEvent(event);
    float itemHeight = lineSpacingMultiplier * itemTextHeight;

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startTime = System.currentTimeMillis();
            cancelFuture();
            previousY = event.getRawY();
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            break;

        case MotionEvent.ACTION_MOVE:
            float dy = previousY - event.getRawY();
            previousY = event.getRawY();

            totalScrollY = (int) (totalScrollY + dy);

            if (!isLoop) {
                float top = -initPosition * itemHeight;
                float bottom = (items.size() - 1 - initPosition) * itemHeight;

                if (totalScrollY < top) {
                    totalScrollY = (int) top;
                } else if (totalScrollY > bottom) {
                    totalScrollY = (int) bottom;
                }
            }
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
        default:
            if (!eventConsumed) {
                float y = event.getY();
                double l = Math.acos((radius - y) / radius) * radius;
                int circlePosition = (int) ((l + itemHeight / 2) / itemHeight);

                float extraOffset = (totalScrollY % itemHeight + itemHeight) % itemHeight;
                mOffset = (int) ((circlePosition - itemsVisibleCount / 2) * itemHeight - extraOffset);

                if ((System.currentTimeMillis() - startTime) > 120) {
                    // 处理拖拽事件
                    smoothScroll(ACTION.DRAG);
                } else {
                    // 处理条目点击事件
                    smoothScroll(ACTION.CLICK);
                }
            }
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
            break;
    }

    invalidate();
    return true;
}
 
源代码20 项目: XERUNG   文件: MaterialMultiAutoCompleteTextView.java
@Override
public boolean onTouchEvent(MotionEvent event) {
  if (singleLineEllipsis && getScrollX() > 0 && event.getAction() == MotionEvent.ACTION_DOWN && event.getX() < getPixel(4 * 5) && event.getY() > getHeight() - extraPaddingBottom - innerPaddingBottom && event.getY() < getHeight() - innerPaddingBottom) {
    setSelection(0);
    return false;
  }
  if (hasFocus() && showClearButton) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        if (insideClearButton(event)) {
          clearButtonTouched = true;
          clearButtonClicking = true;
          return true;
        }
      case MotionEvent.ACTION_MOVE:
        if (clearButtonClicking && !insideClearButton(event)) {
          clearButtonClicking = false;
        }
        if (clearButtonTouched) {
          return true;
        }
        break;
      case MotionEvent.ACTION_UP:
        if (clearButtonClicking) {
          if (!TextUtils.isEmpty(getText())) {
            setText(null);
          }
          clearButtonClicking = false;
        }
        if (clearButtonTouched) {
          clearButtonTouched = false;
          return true;
        }
        clearButtonTouched = false;
        break;
      case MotionEvent.ACTION_CANCEL:
        clearButtonTouched = false;
        clearButtonClicking = false;
        break;
    }
  }
  return super.onTouchEvent(event);
}