Example usage for android.view MotionEvent getActionMasked

List of usage examples for android.view MotionEvent getActionMasked

Introduction

In this page you can find the example usage for android.view MotionEvent getActionMasked.

Prototype

public final int getActionMasked() 

Source Link

Document

Return the masked action being performed, without pointer index information.

Usage

From source file:com.android.inputmethod.accessibility.KeyboardAccessibilityDelegate.java

/**
 * Receives hover events when touch exploration is turned on in SDK versions ICS and higher.
 *
 * @param event The hover event./* w  w w .  j  a  v a2 s  . c  om*/
 * @return {@code true} if the event is handled.
 */
public boolean onHoverEvent(final MotionEvent event) {
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_HOVER_ENTER:
        onHoverEnter(event);
        break;
    case MotionEvent.ACTION_HOVER_MOVE:
        onHoverMove(event);
        break;
    case MotionEvent.ACTION_HOVER_EXIT:
        onHoverExit(event);
        break;
    default:
        Log.w(getClass().getSimpleName(), "Unknown hover event: " + event);
        break;
    }
    return true;
}

From source file:com.personal.taskmanager2.utilities.RecyclerViewTouchListener.java

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_CANCEL:
        Log.d(TAG, "Cancel Event");
        break;/*from   ww w  .  j  av a  2 s.co  m*/
    case MotionEvent.ACTION_DOWN:
        Log.d(TAG, "Press Event");
        break;
    case MotionEvent.ACTION_UP:
        Log.d(TAG, "Up Event");

        if (mVelocityTracker == null) {
            Log.d(TAG, "velocity tracker is null in action up");
            break;
        }

        Log.d(TAG, "Up Intercept");
        float deltaX = e.getRawX() - mDownX;
        float absDeltaX = Math.abs(deltaX);
        mVelocityTracker.addMovement(e);
        mVelocityTracker.computeCurrentVelocity(1000);
        float velocityX = mVelocityTracker.getXVelocity();
        float absVelocityX = Math.abs(velocityX);
        float absVelocityY = Math.abs(mVelocityTracker.getYVelocity());
        boolean dismiss = false;
        boolean dismissRight = false;
        if (absDeltaX > mViewWidth / 2) {
            dismiss = true;
            dismissRight = deltaX > 0;
        } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity
                && absVelocityY < absVelocityX) {
            // dismiss only if flinging in the same direction as dragging
            dismiss = (velocityX < 0) == (deltaX < 0);
            dismissRight = mVelocityTracker.getXVelocity() > 0;
        }

        if (dismiss) {
            dismiss(mChildView, mChildPosition, dismissRight);
        } else {
            mChildView.animate().alpha(1).translationX(0).setDuration(mAnimationTime).setListener(null);
        }

        mVelocityTracker.recycle();
        mVelocityTracker = null;
        mDownX = 0;
        mDeltaX = 0;
        mChildView = null;
        mChildPosition = RecyclerView.NO_POSITION;
        mSwiping = false;
        break;
    case MotionEvent.ACTION_MOVE:
        Log.d(TAG, "Move Event");
        mRecyclerView.requestDisallowInterceptTouchEvent(true);
        mRefreshLayout.requestDisallowInterceptTouchEvent(true);

        // Cancel ListView's touch (un-highlighting the item)
        MotionEvent cancelEvent = MotionEvent.obtain(e);
        cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL | (e.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
        mRecyclerView.onTouchEvent(cancelEvent);
        mRefreshLayout.onTouchEvent(cancelEvent);
        cancelEvent.recycle();

        mChildView.setTranslationX(mDeltaX);
        /*mChildView.setAlpha(Math.max(0.15f, Math.min(1f,
                                                     1f - 2f * Math.abs(mDeltaX) /
                                                          mViewWidth)));*/
        break;
    }
}

From source file:ch.jeda.platform.android.CanvasFragment.java

@Override
public boolean onTouch(final View view, final MotionEvent event) {
    int index;//from www .ja  v a2 s .c  om
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        index = event.getActionIndex();
        this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_DOWN, event.getPointerId(index),
                (int) event.getX(index), (int) event.getY(index)));
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        index = event.getActionIndex();
        this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_UP, event.getPointerId(index),
                (int) event.getX(index), (int) event.getY(index)));
        break;
    case MotionEvent.ACTION_MOVE:
        for (index = 0; index < event.getPointerCount(); ++index) {
            this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_MOVED,
                    event.getPointerId(index), (int) event.getX(index), (int) event.getY(index)));
        }

        break;
    }
    return true;
}

From source file:com.dastanapps.camera2.view.Cam2AutoFitTextureView.java

@Nullable
private Boolean touchTofocus2(MotionEvent event) {
    MotionEvent motionEvent = event;
    final int actionMasked = motionEvent.getActionMasked();
    if (actionMasked != MotionEvent.ACTION_DOWN) {
        return false;
    }//from  w ww .j a va  2s.  com
    if (mManualFocusEngaged) {
        Log.d(TAG, "Manual focus already engaged");
        return true;
    }

    final Rect sensorArraySize = mCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);

    //TODO: here I just flip x,y, but this needs to correspond with the sensor orientation (via SENSOR_ORIENTATION)
    final int y = (int) ((motionEvent.getX() / (float) getWidth()) * (float) sensorArraySize.height());
    final int x = (int) ((motionEvent.getY() / (float) getHeight()) * (float) sensorArraySize.width());
    final int halfTouchWidth = 150; //(int)motionEvent.getTouchMajor(); //TODO: this doesn't represent actual touch size in pixel. Values range in [3, 10]...
    final int halfTouchHeight = 150; //(int)motionEvent.getTouchMinor();
    MeteringRectangle focusAreaTouch = new MeteringRectangle(Math.max(x - halfTouchWidth, 0),
            Math.max(y - halfTouchHeight, 0), halfTouchWidth * 2, halfTouchHeight * 2,
            MeteringRectangle.METERING_WEIGHT_MAX - 1);

    CameraCaptureSession.CaptureCallback captureCallbackHandler = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                TotalCaptureResult result) {
            super.onCaptureCompleted(session, request, result);
            mManualFocusEngaged = false;

            if (request.getTag() == "FOCUS_TAG") {
                //the focus trigger is complete -
                //resume repeating (preview surface will get frames), clear AF trigger
                mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);
                try {
                    mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request,
                CaptureFailure failure) {
            super.onCaptureFailed(session, request, failure);
            Log.e(TAG, "Manual AF failure: " + failure);
            mManualFocusEngaged = false;
        }
    };

    //first stop the existing repeating request
    try {
        mPreviewSession.stopRepeating();
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    //cancel any existing AF trigger (repeated touches, etc.)
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
    try {
        mPreviewSession.capture(mPreviewBuilder.build(), captureCallbackHandler, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

    //Now add a new AF trigger with focus region
    if (isMeteringAreaAFSupported()) {
        mPreviewBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[] { focusAreaTouch });
    }
    mPreviewBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
    mPreviewBuilder.setTag("FOCUS_TAG"); //we'll capture this later for resuming the preview

    //            //then we ask for a single request (not repeating!)
    //            mPreviewSession.capture(mPreviewBuilder.build(), captureCallbackHandler, mBackgroundHandler);
    return null;
}

From source file:com.android.gallery3d.v5.filtershow.category.CategoryView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean ret = super.onTouchEvent(event);
    FilterShowActivity activity = (FilterShowActivity) getContext();

    if (event.getActionMasked() == MotionEvent.ACTION_UP) {
        activity.startTouchAnimation(this, event.getX(), event.getY());
    }//from w  w  w.  j  av a 2 s.c om
    if (!canBeRemoved()) {
        return ret;
    }
    if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
        mStartTouchY = event.getY();
        mStartTouchX = event.getX();
    }
    if (event.getActionMasked() == MotionEvent.ACTION_UP) {
        setTranslationX(0);
        setTranslationY(0);
    }
    if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
        float delta = event.getY() - mStartTouchY;
        if (getOrientation() == CategoryView.VERTICAL) {
            delta = event.getX() - mStartTouchX;
        }
        if (Math.abs(delta) > mDeleteSlope) {
            activity.setHandlesSwipeForView(this, mStartTouchX, mStartTouchY);
        }
    }
    return true;
}

From source file:org.mariotaku.twidere.fragment.support.AbsContentRecyclerViewFragment.java

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mDrawerCallback = new SimpleDrawerCallback(mRecyclerView);

    final View view = getView();
    if (view == null)
        throw new AssertionError();
    final Context context = view.getContext();
    final boolean compact = Utils.isCompactCards(context);
    final int backgroundColor = ThemeUtils.getThemeBackgroundColor(context);
    final int colorRes = TwidereColorUtils.getContrastYIQ(backgroundColor,
            R.color.bg_refresh_progress_color_light, R.color.bg_refresh_progress_color_dark);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(colorRes);
    mAdapter = onCreateAdapter(context, compact);
    mLayoutManager = new FixedLinearLayoutManager(context);
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override//from  www  . j  a  v  a 2s  . c  om
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                updateRefreshProgressOffset();
            }
            return false;
        }
    });
    if (compact) {
        mItemDecoration = new DividerItemDecoration(context, mLayoutManager.getOrientation());
        mRecyclerView.addItemDecoration(mItemDecoration);
    }
    mRecyclerView.setAdapter((RecyclerView.Adapter) mAdapter);

    mScrollListener = new ContentListScrollListener(this);
    mScrollListener.setTouchSlop(ViewConfiguration.get(context).getScaledTouchSlop());
}

From source file:com.arthurpitman.common.SlidingPanel.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    float y = event.getY();
    int action = event.getActionMasked();
    int pointerId = event.getPointerId(event.getActionIndex());

    switch (action) {
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_DOWN:
        startTouchY = y;/*w  w  w .  j a  va2  s  .c  o m*/
        startTouchScrollY = getScrollY();

        // if touch didn't occur on the actual control, ignore it
        float touchBoundary = viewHeight - collapsedHeight - startTouchScrollY;
        if (y < touchBoundary) {
            return false;
        }

        // start tracking velocity
        if (velocityTracker == null) {
            velocityTracker = VelocityTracker.obtain();
        } else {
            velocityTracker.clear();
        }
        velocityTracker.addMovement(event);
        break;

    case MotionEvent.ACTION_MOVE:
        // determine if a valid touch has started
        if (Math.abs(y - startTouchY) > touchSlop) {
            touchState = TOUCH_STARTED;
        }
        if (velocityTracker != null) {
            velocityTracker.addMovement(event);
        }

        // scroll as appropriate
        if (touchState == TOUCH_STARTED) {
            final int scrollDelta = (int) (startTouchY - y);
            scrollTo(0, Math.max(0, Math.min(viewHeight, startTouchScrollY + scrollDelta)));
        }
        break;

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_UP:
        if (touchState == TOUCH_STARTED) {
            final int currentScrollY = getScrollY();

            // get velocity
            float velocity = 0;
            if (velocityTracker != null) {
                velocityTracker.computeCurrentVelocity(1000);
                velocity = VelocityTrackerCompat.getYVelocity(velocityTracker, pointerId);
                velocityTracker.recycle();
                velocityTracker = null;
            }

            // snap to final scroll position
            int target = startTouchScrollY;
            if ((Math.abs(velocity) > minimumflingVelocity) || (Math.abs(y - startTouchY) > (viewHeight / 2))) {
                if (velocity < 0) {
                    target = viewHeight;
                } else {
                    target = 0;
                }
            }
            scroller.startScroll(0, currentScrollY, 0, target - currentScrollY);
            invalidate();
            touchState = TOUCH_NONE;
        }
        break;
    }
    return true;
}

From source file:com.kerkr.edu.recycleView.SwipeToDismissTouchListener.java

@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
    motionEvent.offsetLocation(mTranslationX, 0);

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_UP: {
        up(motionEvent);/*from w  ww  . j a va  2  s.  c  o m*/
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        move(motionEvent);
        break;
    }
    }
}

From source file:info.papdt.blacklight.ui.common.DragRelativeLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (mDraggable == null)
        return super.onInterceptHoverEvent(ev);

    boolean ret = mHelper.shouldInterceptTouchEvent(ev);

    if (!ret) {/*  w ww. ja v a 2s . c o  m*/
        float x = ev.getX();
        float y = ev.getY();

        ret = ev.getActionMasked() == MotionEvent.ACTION_DOWN && insideDraggable(x, y);
    }

    if (DEBUG) {
        Log.d(TAG, "onInterceptTouchEvent: " + ret);
    }

    return ret;
}

From source file:com.jungle.toolbaractivity.layout.HorizontalSwipeBackLayout.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (!mSwipeBackEnable) {
        return super.onInterceptTouchEvent(event);
    }//w w w. ja v a 2  s.c om

    final int action = event.getActionMasked();
    final float x = event.getRawX();
    final float y = event.getRawY();
    final float horzOffset = Math.abs(x - mLastX);
    final float vertOffset = Math.abs(y - mLastY);

    if (action == MotionEvent.ACTION_DOWN) {
        mLastX = x;
        mLastY = y;
        mDownTimestamp = System.currentTimeMillis();
        mSlideState = SlideState.None;
    }

    if (mSlideState == SlideState.SkipThisRound) {
        return super.onInterceptTouchEvent(event);
    }

    if (action == MotionEvent.ACTION_MOVE && handleTouchMove(x, y, horzOffset, vertOffset)) {
        return true;
    }

    return super.onInterceptTouchEvent(event);
}