Java Code Examples for android.graphics.Rect#contains()

The following examples show how to use android.graphics.Rect#contains() . 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: MaterialPlain.java    From KickAssSlidingMenu with Apache License 2.0 6 votes vote down vote up
private boolean findClickableViewInChild(View view, int x, int y) {
    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            final Rect rect = new Rect();
            child.getHitRect(rect);

            final boolean contains = rect.contains(x, y);
            if (contains) {
                return findClickableViewInChild(child, x - rect.left, y - rect.top);
            }
        }
    } else if (view != childView) {
        return (view.isEnabled() && (view.isClickable() || view.isLongClickable() || view.isFocusableInTouchMode()));
    }

    return view.isFocusableInTouchMode();
}
 
Example 2
Source File: RegistrationActivity.java    From Password-Storage with MIT License 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if ( v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent( event );
}
 
Example 3
Source File: MapObjectsOverlay.java    From Androzic with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onSingleTap(MotionEvent e, Rect mapTap, MapView mapView)
{
	Iterator<MapObject> mapObjects = application.getMapObjects().iterator();
	while (mapObjects.hasNext())
	{
		MapObject mo = mapObjects.next();
		synchronized (mo)
		{
			int[] pointXY = application.getXYbyLatLon(mo.latitude, mo.longitude);
			if (mapTap.contains(pointXY[0], pointXY[1]))
			{
				return application.getMapHolder().mapObjectTapped(mo._id, (int) e.getX(), (int) e.getY());
			}
		}
	}
	return false;
}
 
Example 4
Source File: WaypointsOverlay.java    From Androzic with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onSingleTap(MotionEvent e, Rect mapTap, MapView mapView)
{
	synchronized (waypoints)
	{
		for (int i = waypoints.size() - 1; i >= 0; i--)
		{
			Waypoint wpt = waypoints.get(i);
			int[] pointXY = application.getXYbyLatLon(wpt.latitude, wpt.longitude);
			if (mapTap.contains(pointXY[0], pointXY[1]))
			{
				return application.getMapHolder().waypointTapped(wpt, (int) e.getX(), (int) e.getY());
			}
		}
	}
	return false;
}
 
Example 5
Source File: CountDownButton.java    From CountDownButton with MIT License 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            Rect rect = new Rect();
            this.getGlobalVisibleRect(rect);
            if (onClickListener != null && rect.contains((int) event.getRawX(), (int) event.getRawY())) {
                onClickListener.onClick(this);
            }
            if (mEnableCountDown && rect.contains((int) event.getRawX(), (int) event.getRawY())) {
                // 设置按钮不可点击
                setEnabled(false);
                // 开始倒计时
                mCountDownTimer.start();
                isCountDownNow = true;
            }
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        default:
            break;
    }
    return super.onTouchEvent(event);
}
 
Example 6
Source File: AbsHListView.java    From letv with Apache License 2.0 6 votes vote down vote up
public int pointToPosition(int x, int y) {
    Rect frame = this.mTouchFrame;
    if (frame == null) {
        this.mTouchFrame = new Rect();
        frame = this.mTouchFrame;
    }
    for (int i = getChildCount() - 1; i >= 0; i--) {
        View child = getChildAt(i);
        if (child.getVisibility() == 0) {
            child.getHitRect(frame);
            if (frame.contains(x, y)) {
                return this.mFirstPosition + i;
            }
        }
    }
    return -1;
}
 
Example 7
Source File: ProviderPickerActivity.java    From OpenYOLO-Android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent event) {

    // finishWithUserCanceled() if the user tapped outside the picker
    if (MotionEvent.ACTION_DOWN == event.getAction()) {
        Rect visibilityRect = new Rect();
        mPickerContainer.getGlobalVisibleRect(visibilityRect);
        boolean tappedOutsidePicker =
                !visibilityRect.contains((int) event.getRawX(), (int) event.getRawY());
        if (tappedOutsidePicker) {
            finishWithUserCanceled();
        }
    }

    return super.dispatchTouchEvent(event);
}
 
Example 8
Source File: AnimatedPullToRefreshLayout.java    From AnimatedPullToRefresh-master with Apache License 2.0 6 votes vote down vote up
/**
 * Whether it is possible for the child view of this layout to scroll up.
 */
private boolean canChildrenScrollUp(View view, MotionEvent event) {
    if (view instanceof ViewGroup) {
        final ViewGroup viewgroup = (ViewGroup) view;
        int count = viewgroup.getChildCount();
        for (int i = 0; i < count; ++i) {
            View child = viewgroup.getChildAt(i);
            Rect bounds = new Rect();
            child.getHitRect(bounds);
            if (bounds.contains((int) event.getX(), (int) event.getY())) {
                return canViewScrollUp(child, event);
            }
        }
    }

    return false;
}
 
Example 9
Source File: MultiFloatingActionButton.java    From ReadMark with Apache License 2.0 5 votes vote down vote up
private boolean judgeIfTouchBackground(int x, int y){
    Rect a = new Rect();
    Rect b = new Rect();
    a.set(0, 0, getWidth(), getHeight() - getChildAt(getChildCount() - 1).getTop());
    b.set(0, getChildAt(getChildCount() - 1).getTop(), getChildAt(getChildCount() - 1).getLeft(), getHeight());
    if(a.contains(x, y) || b.contains(x, y)){
        return true;
    }
    return false;
}
 
Example 10
Source File: DocPageView.java    From Mupdf with Apache License 2.0 5 votes vote down vote up
public boolean hitTest(Point pt)
{
	PointF ul = new PointF(mPoint.x, mPoint.y-50);
	PointF dr = new PointF(mPoint.x+50, mPoint.y);
	Rect rect = new Rect((int)ul.x, (int)ul.y, (int)dr.x, (int)dr.y);

	if (rect.contains(pt.x, pt.y))
		return true;

	return false;
}
 
Example 11
Source File: ComplicationWatchFaceService.java    From complications with Apache License 2.0 5 votes vote down vote up
private int getTappedComplicationId(int x, int y) {

            int complicationId;
            ComplicationData complicationData;
            ComplicationDrawable complicationDrawable;

            long currentTimeMillis = System.currentTimeMillis();

            for (int i = 0; i < COMPLICATION_IDS.length; i++) {
                complicationId = COMPLICATION_IDS[i];
                complicationData = mActiveComplicationDataSparseArray.get(complicationId);

                if ((complicationData != null)
                        && (complicationData.isActive(currentTimeMillis))
                        && (complicationData.getType() != ComplicationData.TYPE_NOT_CONFIGURED)
                        && (complicationData.getType() != ComplicationData.TYPE_EMPTY)) {

                    complicationDrawable = mComplicationDrawableSparseArray.get(complicationId);
                    Rect complicationBoundingRect = complicationDrawable.getBounds();

                    if (complicationBoundingRect.width() > 0) {
                        if (complicationBoundingRect.contains(x, y)) {
                            return complicationId;
                        }
                    } else {
                        Log.e(TAG, "Not a recognized complication id.");
                    }
                }
            }
            return -1;
        }
 
Example 12
Source File: StickyHeaderListView.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
protected boolean isTouchInFloatingSectionHeader(float touchX, float touchY) {
	if (floatingHeader != null) {
		Rect hitRect = new Rect();
		floatingHeader.getHitRect(hitRect);

		hitRect.top += floatingHeaderOffset;
		hitRect.bottom += floatingHeaderOffset + getPaddingBottom();
		hitRect.left += getPaddingLeft();
		hitRect.right += getPaddingRight();

		return hitRect.contains((int) touchX, (int) touchY);
	}

	return false;
}
 
Example 13
Source File: DiscreteSeekBar.java    From Panoramic-Screenshot with GNU General Public License v3.0 5 votes vote down vote up
private boolean startDragging(MotionEvent ev, boolean ignoreTrackIfInScrollContainer) {
    final Rect bounds = mTempRect;
    mThumb.copyBounds(bounds);
    //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);
        //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;
}
 
Example 14
Source File: StickyRecyclerHeadersDecoration.java    From toktok-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the position of the header under the specified (x, y) coordinates.
 *
 * @param x x-coordinate
 * @param y y-coordinate
 * @return position of header, or -1 if not found
 */
public int findHeaderPositionUnder(int x, int y) {
    for (int i = 0; i < mHeaderRects.size(); i++) {
        Rect rect = mHeaderRects.get(mHeaderRects.keyAt(i));
        if (rect.contains(x, y)) {
            int position = mHeaderRects.keyAt(i);
            if (mVisibilityAdapter == null || mVisibilityAdapter.isPositionVisible(position)) {
                return position;
            }
        }
    }
    return -1;
}
 
Example 15
Source File: DiscreteSeekBar.java    From FuAgoraDemoDroid with MIT License 5 votes vote down vote up
private boolean startDragging(MotionEvent ev, boolean ignoreTrackIfInScrollContainer) {
    final Rect bounds = mTempRect;
    mThumb.copyBounds(bounds);
    //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);
        //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;
}
 
Example 16
Source File: PositionHelper.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
/**
 * Translates coordinates relative to an element rectangle into absolute coordinates.
 *
 * @param point             A point in relative coordinates.
 * @param displayRect       The display rectangle to which the point is relative.
 * @param offsets           X and Y values by which to offset the point. These are typically the
 *                          absolute coordinates of the display rectangle.
 * @param shouldCheckBounds Throw if the translated point is outside displayRect?
 */
public static Point getAbsolutePosition(final Point point, final Rect displayRect,
                                        final Point offsets, final boolean shouldCheckBounds) {
    final Point absolutePosition = new Point(
            translateCoordinate(point.x, displayRect.width(), offsets.x),
            translateCoordinate(point.y, displayRect.height(), offsets.y)
    );
    if (shouldCheckBounds &&
            !displayRect.contains(absolutePosition.x.intValue(), absolutePosition.y.intValue())) {
        throw new InvalidCoordinatesException("Coordinate " + absolutePosition.toString() +
                " is outside of element rect: " + displayRect.toShortString());
    }
    return absolutePosition;
}
 
Example 17
Source File: HorizontalListView.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
private boolean isEventWithinView(MotionEvent e, View child)
{
	Rect viewRect = new Rect();
	int[] childPosition = new int[2];
	child.getLocationOnScreen(childPosition);
	int left = childPosition[0];
	int right = left + child.getWidth();
	int top = childPosition[1];
	int bottom = top + child.getHeight();
	viewRect.set(left, top, right, bottom);
	return viewRect.contains((int) e.getRawX(), (int) e.getRawY());
}
 
Example 18
Source File: HorizontalListView.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
private boolean isEventWithinView(MotionEvent e, View child) {
    Rect viewRect = new Rect();
    int[] childPosition = new int[2];
    child.getLocationOnScreen(childPosition);
    int left = childPosition[0];
    int right = left + child.getWidth();
    int top = childPosition[1];
    int bottom = top + child.getHeight();
    viewRect.set(left, top, right, bottom);
    return viewRect.contains((int)e.getRawX(), (int)e.getRawY());
}
 
Example 19
Source File: KlyphSlidingDrawer.java    From Klyph with MIT License 4 votes vote down vote up
@Override
public boolean  onInterceptTouchEvent(MotionEvent event) {
    if (mLocked) {
        return false;
    }

    final int action = event.getAction();

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

    final Rect frame = mFrame;
    final View handle = mHandle;

    handle.getHitRect(frame);
    if (!mTracking && !frame.contains((int) x, (int) y)) {
        return false;
    }

    if (action == MotionEvent.ACTION_DOWN) {
        mTracking = true;

        handle.setPressed(true);
        // Must be called before prepareTracking()
        prepareContent();

        // Must be called after prepareContent()
        if (mOnDrawerScrollListener != null) {
            mOnDrawerScrollListener.onScrollStarted();
        }

        if (mVertical) {
            final int top = mHandle.getTop();
            mTouchDelta = (int) y - top;
            prepareTracking(top);
        } else {
            final int left = mHandle.getLeft();
            mTouchDelta = (int) x - left;
            prepareTracking(left);
        }
        mVelocityTracker.addMovement(event);
    }

    return true;
}
 
Example 20
Source File: PopupCircleView.java    From PopupCircleMenu with MIT License 4 votes vote down vote up
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    int downX = (int) ev.getRawX();
    int downY = (int) ev.getRawY();

    switch (action) {
        case MotionEvent.ACTION_DOWN:


            if (isshowing) {
                return false;
            } else {
                mTriggerRect = new Rect(downX - mTriggerPixle, downY - mTriggerPixle, downX + mTriggerPixle, downY + mTriggerPixle);
                getParent().requestDisallowInterceptTouchEvent(true);

                mAbleToggle = true;
                Message msg = Message.obtain();
                msg.obj = ev;
                msg.what = ACTION_DOWN;
                if (mPopup.getVisibility() == INVISIBLE) {
                    mHandler.sendMessageDelayed(msg, mAnimDuration);
                }

                return true;
            }
        case MotionEvent.ACTION_MOVE:
            int mCurrentX = (int) ev.getRawX();
            int mCurrentY = (int) ev.getRawY();

            if (mTriggerRect.contains(mCurrentX, mCurrentY)) {
                if (mPopup.getVisibility() == VISIBLE && isshowing) {
                    mPopup.dispatchTouchEvent(ev);
                }
            } else {
                if (mPopup.getVisibility() == INVISIBLE) {
                    mAbleToggle = false;
                    getParent().requestDisallowInterceptTouchEvent(false);
                } else {
                    mPopup.dispatchTouchEvent(ev);
                }
            }

            break;
        case MotionEvent.ACTION_UP:
            dismiss(ev);
            break;
        case MotionEvent.ACTION_CANCEL:
            dismiss(ev);
            break;
    }
    return false;
}