Example usage for android.graphics Rect contains

List of usage examples for android.graphics Rect contains

Introduction

In this page you can find the example usage for android.graphics Rect contains.

Prototype

public boolean contains(int x, int y) 

Source Link

Document

Returns true if (x,y) is inside the rectangle.

Usage

From source file:nz.ac.otago.psyanlab.common.designer.program.stage.StageView.java

/**
 * Maps a point to a position in the list.
 *
 * @param x X in local coordinate/* w ww .j a va2 s  .  co m*/
 * @param y Y in local coordinate
 * @return The position of the item which contains the specified point, or {@link
 * #INVALID_POSITION} if the point does not intersect an item.
 */
private int pointToPosition(int x, int y) {
    Rect frame = mTouchFrame;
    if (frame == null) {
        mTouchFrame = new Rect();
        frame = mTouchFrame;
    }

    final int count = getChildCount();
    for (int i = count - 1; i >= 0; i--) {
        final View child = getChildAt(i);

        child.getHitRect(frame);
        if (frame.contains(x, y)) {
            return i;
        }
    }
    return NO_MATCHED_CHILD;
}

From source file:de.timroes.swipetodismiss.SwipeDismissList.java

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }//from  ww w . j  av a 2  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:cn.org.eshow.framwork.view.slidingmenu.CustomViewAbove.java

/**
 * Checks if is in ignored view./*w  ww .  j  a  v  a 2 s  .c  om*/
 *
 * @param ev the ev
 * @return true, if is in ignored view
 */
private boolean isInIgnoredView(MotionEvent ev) {
    Rect rect = new Rect();
    for (View v : mIgnoredViews) {
        v.getHitRect(rect);
        if (rect.contains((int) ev.getX(), (int) ev.getY()))
            return true;
    }
    return false;
}

From source file:com.base.view.slidemenu.SlideMenu.java

private boolean isTapInEdgeSlide(float x, float y) {
    final Rect rect = mEdgeSlideDetectRect;
    boolean result = false;
    if (null != mPrimaryMenu) {
        getHitRect(rect);// w  w  w. jav  a  2  s.  c  om
        rect.right = mEdgeSlideWidth;
        result |= rect.contains((int) x, (int) y);
    }

    if (null != mSecondaryMenu) {
        getHitRect(rect);
        rect.left = rect.right - mEdgeSlideWidth;
        result |= rect.contains((int) x, (int) y);
    }
    return result;
}

From source file:com.android.hcframe.DraggableGridViewPager.java

private int getTargetByXY(int x, int y) {
    final int position = getPositionByXY(x, y);
    if (position < 0) {
        return -1;
    }/*ww  w  .j a  v a 2  s .co  m*/
    final Rect r = getRectByPosition(position);
    final int page = position / mPageSize;
    r.inset(r.width() / 4, r.height() / 4);
    r.offset(-getWidth() * page, 0);
    if (!r.contains(x, y)) {
        return -1;
    }
    return position;
}

From source file:in.sc9.discreteslider.DiscreteSlider.java

private boolean startDragging(MotionEvent ev, boolean ignoreTrackIfInScrollContainer) {
    final Rect bounds = mTempRect;
    mThumb.copyBounds(bounds);//w ww  .  j a  v  a2  s  . c  o  m
    //Grow the current thumb rect for a bigger touch area
    bounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
    mIsDragging = (bounds.contains((int) ev.getX(), (int) ev.getY()));
    if (!mIsDragging && mAllowTrackClick && !ignoreTrackIfInScrollContainer) {
        //If the user clicked outside the thumb, we compute the current position
        //and force an immediate drag to it.
        mIsDragging = true;
        mDragOffset = (bounds.width() / 2) - mAddedTouchBounds;
        updateDragging(ev);
        Log.d("startDragging", ev.getX() + "");
        //As the thumb may have moved, get the bounds again
        mThumb.copyBounds(bounds);
        bounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
    }
    if (mIsDragging) {
        setPressed(true);
        attemptClaimDrag();
        setHotspot(ev.getX(), ev.getY());
        mDragOffset = (int) (ev.getX() - bounds.left - mAddedTouchBounds);
        if (mPublicChangeListener != null) {
            mPublicChangeListener.onStartTrackingTouch(this);
        }
    }
    return mIsDragging;
}

From source file:net.osmand.plus.views.controls.SwipeDismissListViewTouchListener.java

@Override
public boolean onTouch(View view, MotionEvent ev) {
    if (mViewWidth < 2) {
        mViewWidth = mListView.getWidth();
    }/*from  ww  w .java  2 s  .com*/

    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:com.mediatek.galleryfeature.stereo.fancycolor.FancyColorActivity.java

private Point calcClickPosition(int viewW, int viewH, float x, float y) {
    int thumbW = mThumbBitmapWidth;
    int thumbH = mThumbBitmapHeight;
    int gap;//w  ww  .j  a v a  2  s. c  om
    int mapX;
    int mapY;
    Rect validRange = new Rect();
    if (thumbH >= thumbW) {
        // bitmap height fulfills view height
        gap = (viewW - viewH * thumbW / thumbH) / 2;
        validRange.left = gap;
        validRange.right = viewW - gap;
        validRange.top = 0;
        validRange.bottom = viewH;
        mapX = thumbW * ((int) x - gap) / validRange.width();
        mapY = thumbH * (int) y / validRange.height();
    } else {
        // bitmap width fullfills view width
        gap = (viewH - viewW * thumbH / thumbW) / 2;
        validRange.left = 0;
        validRange.right = viewW;
        validRange.top = gap;
        validRange.bottom = viewH - gap;
        mapX = thumbW * (int) x / validRange.width();
        mapY = thumbH * ((int) y - gap) / validRange.height();
    }
    MtkLog.d(TAG,
            "<calcClickPosition> thumbW " + thumbW + ", thumbH " + thumbH + ", gap " + gap + ", rect lrtb "
                    + validRange.left + ", " + validRange.right + ", " + validRange.top + ", "
                    + validRange.bottom + ", mapX " + mapX + ", mapY " + mapY);
    if (!validRange.contains((int) x, (int) y)) {
        MtkLog.d(TAG, "<calcClickPosition> invalid click");
        return null;
    }
    return new Point(mapX, mapY);
}

From source file:org.mariotaku.twidere.activity.support.ComposeActivity.java

@Override
public boolean onTouchEvent(final MotionEvent event) {
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        final Rect rect = new Rect();
        getWindow().getDecorView().getHitRect(rect);
        if (!rect.contains(Math.round(event.getX()), Math.round(event.getY()))) {
            onBackPressed();//from www. ja  v  a 2s.  c  o  m
            return true;
        }
        break;
    }
    }
    return super.onTouchEvent(event);
}

From source file:com.example.SmartBoard.DrawingView.java

private JSONObject getTouchedObject(final int xTouch, final int yTouch) {
    JSONObject touched = null;//from w  w  w.j  a  v a  2s  .  com

    //makes textObjects preferable on selection for drag
    //search for text object first
    for (JSONObject textObject : textObjects.values()) {
        Rect region = Rect.unflattenFromString(textObject.optString("region"));
        //System.out.println(region);
        if (region.contains(xTouch, yTouch)) {

            touched = textObject;
            return touched;
        }
    }

    for (JSONObject object : objectDrawables.values()) {

        if (object.optString("type").compareTo("Circle") == 0) {
            int x = object.optInt("x");
            int y = object.optInt("y");
            int radius = object.optInt("radius");

            if ((x - xTouch) * (x - xTouch) + (y - yTouch) * (y - yTouch) <= radius * radius) {
                touched = object;
                break;
            }
        } else if (object.optString("type").compareTo("Rectangle") == 0) {
            Rect tempRect = Rect.unflattenFromString(object.optString("dimens"));
            if (tempRect.contains(xTouch, yTouch)) {
                touched = object;
                break;
            }
        } else if (object.optString("type").compareTo("Line") == 0) {

            double tempGradient = gradient(object.optInt("startx"), object.optInt("starty"), xTouch, yTouch);
            int startx = object.optInt("startx");
            int stopx = object.optInt("stopx");
            int starty = object.optInt("starty");
            int stopy = object.optInt("stopy");

            if (lengthOfLine(startx, starty, xTouch, yTouch)
                    + lengthOfLine(stopx, stopy, xTouch, yTouch) <= object.optDouble("length") + 5) {
                touched = object;
                break;
            }

        } else if (object.optString("type").compareTo("Text") == 0) {

            Rect region = Rect.unflattenFromString(object.optString("region"));
            //System.out.println(region);
            if (region.contains(xTouch, yTouch)) {

                touched = object;
                break;
            }
        }
    }

    return touched;
}