Java Code Examples for android.view.MotionEvent#setLocation()

The following examples show how to use android.view.MotionEvent#setLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OverlayDisplayWindow.java    From media-samples with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouch(View view, MotionEvent event) {
    // Work in screen coordinates.
    final float oldX = event.getX();
    final float oldY = event.getY();
    event.setLocation(event.getRawX(), event.getRawY());

    mGestureDetector.onTouchEvent(event);
    mScaleGestureDetector.onTouchEvent(event);

    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            saveWindowParams();
            break;
    }

    // Revert to window coordinates.
    event.setLocation(oldX, oldY);
    return true;
}
 
Example 2
Source File: OverlayDisplayWindow.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouch(View view, MotionEvent event) {
    // Work in screen coordinates.
    final float oldX = event.getX();
    final float oldY = event.getY();
    event.setLocation(event.getRawX(), event.getRawY());

    mGestureDetector.onTouchEvent(event);
    mScaleGestureDetector.onTouchEvent(event);

    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            saveWindowParams();
            break;
    }

    // Revert to window coordinates.
    event.setLocation(oldX, oldY);
    return true;
}
 
Example 3
Source File: OverlayDisplayWindow.java    From V.FlyoutTest with MIT License 6 votes vote down vote up
@Override
public boolean onTouch(View view, MotionEvent event) {
    // Work in screen coordinates.
    final float oldX = event.getX();
    final float oldY = event.getY();
    event.setLocation(event.getRawX(), event.getRawY());

    mGestureDetector.onTouchEvent(event);
    mScaleGestureDetector.onTouchEvent(event);

    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            saveWindowParams();
            break;
    }

    // Revert to window coordinates.
    event.setLocation(oldX, oldY);
    return true;
}
 
Example 4
Source File: MapView.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private MotionEvent rotateTouchEvent(MotionEvent ev) {
    if (this.getMapOrientation() == 0) {
        return ev;
    }
    MotionEvent rotatedEvent = MotionEvent.obtain(ev);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        mRotatePoints[0] = ev.getX();
        mRotatePoints[1] = ev.getY();
        getProjection().rotatePoints(mRotatePoints);
        rotatedEvent.setLocation(mRotatePoints[0], mRotatePoints[1]);
    } else {
        // This method is preferred since it will rotate historical touch events too
        try {
            if (sMotionEventTransformMethod == null) {
                sMotionEventTransformMethod = MotionEvent.class.getDeclaredMethod("transform",
                        new Class[]{Matrix.class});
            }
            sMotionEventTransformMethod.invoke(rotatedEvent,
                    getProjection().getRotationMatrix());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return rotatedEvent;
}
 
Example 5
Source File: OverlayDisplayWindow.java    From android-MediaRouter with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouch(View view, MotionEvent event) {
    // Work in screen coordinates.
    final float oldX = event.getX();
    final float oldY = event.getY();
    event.setLocation(event.getRawX(), event.getRawY());

    mGestureDetector.onTouchEvent(event);
    mScaleGestureDetector.onTouchEvent(event);

    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            saveWindowParams();
            break;
    }

    // Revert to window coordinates.
    event.setLocation(oldX, oldY);
    return true;
}
 
Example 6
Source File: LocationBarTablet.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mTargets == null) return true;

    View selectedTarget = null;
    float selectedDistance = 0;
    // newX and newY are in the coordinates of the selectedTarget.
    float newX = 0;
    float newY = 0;
    for (View target : mTargets) {
        if (!target.isShown()) continue;

        mCachedTargetBounds.set(0, 0, target.getWidth(), target.getHeight());
        offsetDescendantRectToMyCoords(target, mCachedTargetBounds);
        float x = event.getX();
        float y = event.getY();
        float dx = distanceToRange(
                mCachedTargetBounds.left, mCachedTargetBounds.right, x);
        float dy = distanceToRange(
                mCachedTargetBounds.top, mCachedTargetBounds.bottom, y);
        float distance = Math.abs(dx) + Math.abs(dy);
        if (selectedTarget == null || distance < selectedDistance) {
            selectedTarget = target;
            selectedDistance = distance;
            newX = x + dx;
            newY = y + dy;
        }
    }

    if (selectedTarget == null) return false;

    event.setLocation(newX, newY);
    return selectedTarget.onTouchEvent(event);
}
 
Example 7
Source File: LocationBarTablet.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (mTargets == null) return true;

    View selectedTarget = null;
    float selectedDistance = 0;
    // newX and newY are in the coordinates of the selectedTarget.
    float newX = 0;
    float newY = 0;
    for (View target : mTargets) {
        if (!target.isShown()) continue;

        mCachedTargetBounds.set(0, 0, target.getWidth(), target.getHeight());
        offsetDescendantRectToMyCoords(target, mCachedTargetBounds);
        float x = event.getX();
        float y = event.getY();
        float dx = distanceToRange(
                mCachedTargetBounds.left, mCachedTargetBounds.right, x);
        float dy = distanceToRange(
                mCachedTargetBounds.top, mCachedTargetBounds.bottom, y);
        float distance = Math.abs(dx) + Math.abs(dy);
        if (selectedTarget == null || distance < selectedDistance) {
            selectedTarget = target;
            selectedDistance = distance;
            newX = x + dx;
            newY = y + dy;
        }
    }

    if (selectedTarget == null) return false;

    event.setLocation(newX, newY);
    return selectedTarget.onTouchEvent(event);
}
 
Example 8
Source File: ReversibleSeekBar.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean reverse = mReverse;
    float x = 0.0f, y = 0.0f;
    if (reverse) {
        x = event.getX();
        y = event.getY();
        event.setLocation(getWidth() - x, y);
    }
    boolean result = super.onTouchEvent(event);
    if (reverse) {
        event.setLocation(x, y);
    }
    return result;
}
 
Example 9
Source File: PairedTouchListener.java    From AppCompat-Extension-Library with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    v.onTouchEvent(event);
    // put the location in the middle of the paired view
    event.setLocation(mPairedView.getWidth()/2, mPairedView.getHeight()/2);
    mPairedView.onTouchEvent(event);
    return true;
}
 
Example 10
Source File: TransformingTouchDelegate.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Will forward touch events to the delegate view if the event is within the bounds
 * specified in the constructor.
 *
 * @param event The touch event to forward
 * @return True if the event was forwarded to the delegate, false otherwise.
 */
public boolean onTouchEvent(MotionEvent event) {
    boolean sendToDelegate = false;
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mDelegateTargeted = mTouchCheckBounds.contains(event.getX(), event.getY());
            if (mDelegateTargeted) {
                mWasTouchOutsideBounds = !mBounds.contains(event.getX(), event.getY());
                sendToDelegate = true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            sendToDelegate = mDelegateTargeted;
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            sendToDelegate = mDelegateTargeted;
            mDelegateTargeted = false;
            break;
    }
    boolean handled = false;
    if (sendToDelegate) {
        float x = event.getX();
        float y = event.getY();
        if (mWasTouchOutsideBounds) {
            event.setLocation(mBounds.centerX(), mBounds.centerY());
        } else {
            event.offsetLocation(-mBounds.left, -mBounds.top);
        }
        handled = mDelegateView.dispatchTouchEvent(event);
        event.setLocation(x, y);
    }
    return handled;
}
 
Example 11
Source File: MultiTouchGestureDetector.java    From document-viewer with GNU General Public License v3.0 5 votes vote down vote up
private MotionEvent calculateCenterEvent(final MotionEvent ev) {
    final PointF center = calculateCenter(ev);
    final MotionEvent newEvent = MotionEvent.obtain(ev);
    newEvent.setLocation(center.x, center.y);
    return newEvent;

}
 
Example 12
Source File: VerticalViewPager.java    From VerticalViewPager with Apache License 2.0 5 votes vote down vote up
private MotionEvent swapTouchEvent(MotionEvent event) {
    float width = getWidth();
    float height = getHeight();

    float swappedX = (event.getY() / height) * width;
    float swappedY = (event.getX() / width) * height;

    event.setLocation(swappedX, swappedY);

    return event;
}
 
Example 13
Source File: CoolViewPager.java    From CoolViewPager with Apache License 2.0 5 votes vote down vote up
/**
 * 将事件原始X,Y坐标进行置换
 *
 * @param event
 * @return
 */
private MotionEvent swapTouchEvent(MotionEvent event) {
    float width = getWidth();
    float height = getHeight();
    float swappedX = (event.getY() / height) * width;
    float swappedY = (event.getX() / width) * height;
    event.setLocation(swappedX, swappedY);
    return event;
}
 
Example 14
Source File: VerticalViewPager.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
private MotionEvent swapTouchEvent(MotionEvent event) {
    float width = getWidth();
    float height = getHeight();

    float swappedX = (event.getY() / height) * width;
    float swappedY = (event.getX() / width) * height;

    event.setLocation(swappedX, swappedY);

    return event;
}
 
Example 15
Source File: VerticalViewPager.java    From VerticalViewPager with Apache License 2.0 5 votes vote down vote up
private MotionEvent swapXY(MotionEvent ev) {
    float width = getWidth();
    float height = getHeight();

    float newX = (ev.getY() / height) * width;
    float newY = (ev.getX() / width) * height;

    ev.setLocation(newX, newY);

    return ev;
}
 
Example 16
Source File: SHListView.java    From SHSwipeRefreshLayout with MIT License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {

    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        ox = ev.getX();
        oy = ev.getY();
        // Dispatch touch event to parent view
        startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL);
    }

    if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
        // stop nested scrolling dispatch
        stopNestedScroll();
    }

    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        float clampedX = ev.getX();
        float clampedY = ev.getY();
        int dx = (int) (ox - clampedX);
        int dy = (int) (oy - clampedY);

        if (dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)) {
            // sub dx/dy was consumed by parent view!!!
            ev.setLocation(clampedX + consumed[0], clampedY + consumed[1]);
        }
        ox = ev.getX();
        oy = ev.getY();
    }

    return super.onTouchEvent(ev);
}
 
Example 17
Source File: InputView.java    From openboard with GNU General Public License v3.0 5 votes vote down vote up
public boolean onTouchEvent(final int x, final int y, final MotionEvent me) {
    mReceiverView.getGlobalVisibleRect(mEventReceivingRect);
    // Translate global coordinates to <code>ReceiverView</code> local coordinates.
    me.setLocation(translateX(x), translateY(y));
    mReceiverView.dispatchTouchEvent(me);
    onForwardingEvent(me);
    return true;
}
 
Example 18
Source File: FlingYResolver.java    From PullRefreshView with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean tryToMove(MotionEvent ev, float oldX, float oldY, float x, float y) {
    int dataX = (int) (x - oldX);
    int dataY = (int) (y - oldY);
    if (isScrolling || Math.abs(dataY) > Math.abs(dataX)) {
        isScrolling = true;
        c.getFlingLayout().getParent().requestDisallowInterceptTouchEvent(true);
        float moveY = c.getMoveP();
        if (moveY == 0) {
            //开始时 在0,0处
            //判断是否可以滑动
            if ((dataY > 0 && c.canOverStart()) || (dataY < 0 && c.canOverEnd())) {
                c.moveBy(dataY);
                return true;
            }
        } else {
            //当不在0,0处
            ev.setAction(MotionEvent.ACTION_CANCEL);//屏蔽原事件

            if ((moveY < 0 && moveY + dataY >= 0) || (moveY > 0 && moveY + dataY <= 0)) {
                //在0,0附近浮动
                ev.setAction(MotionEvent.ACTION_DOWN);
                c.moveTo(0);
            } else if ((moveY > 0 && dataY > 0) || (moveY < 0 && dataY < 0)) {
                //是否超过最大距离
                int maxDistance = c.getMaxDistance();
                if (Math.abs(moveY) < maxDistance) {
                    int ps = 0;
                    int hDataY = dataY / 2;
                    ps = (int) (-hDataY * Math.abs(moveY) / (float) maxDistance) - hDataY;
                    c.moveBy(ps + dataY);
                } else if (moveY > maxDistance) {
                    c.moveTo(maxDistance);
                } else if (moveY < -maxDistance) {
                    c.moveTo(-maxDistance);
                }
            } else {
                c.moveBy(dataY);
            }
        }
    } else {
        ev.setLocation(x, downY);
    }
    return false;
}
 
Example 19
Source File: SearchView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    boolean sendToDelegate = false;
    boolean hit = true;
    boolean handled = false;

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (mTargetBounds.contains(x, y)) {
                mDelegateTargeted = true;
                sendToDelegate = true;
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_MOVE:
            sendToDelegate = mDelegateTargeted;
            if (sendToDelegate) {
                if (!mSlopBounds.contains(x, y)) {
                    hit = false;
                }
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            sendToDelegate = mDelegateTargeted;
            mDelegateTargeted = false;
            break;
    }
    if (sendToDelegate) {
        if (hit && !mActualBounds.contains(x, y)) {
            // Offset event coordinates to be in the center of the target view since we
            // are within the targetBounds, but not inside the actual bounds of
            // mDelegateView
            event.setLocation(mDelegateView.getWidth() / 2,
                    mDelegateView.getHeight() / 2);
        } else {
            // Offset event coordinates to the target view coordinates.
            event.setLocation(x - mActualBounds.left, y - mActualBounds.top);
        }

        handled = mDelegateView.dispatchTouchEvent(event);
    }
    return handled;
}
 
Example 20
Source File: ReversedSeekBar.java    From particle-android with Apache License 2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent event) {
    event.setLocation(this.getWidth() - event.getX(), event.getY());
    return super.onTouchEvent(event);
}