private void checkCollision() { if (r.intersects(r, Robot.bodyUpper) || r.intersects(r, Robot.bodyLower) || r.intersects(r, Robot.armStageLeft) || r.intersects(r, Robot.armStageRight)) { System.out.println("collision"); } }
private void updateSubviewClipStatus(View subview) { if (!mRemoveClippedSubviews || getParent() == null) { return; } Assertions.assertNotNull(mClippingRect); Assertions.assertNotNull(mAllChildren); // do fast check whether intersect state changed sHelperRect.set(subview.getLeft(), subview.getTop(), subview.getRight(), subview.getBottom()); boolean intersects = mClippingRect.intersects( sHelperRect.left, sHelperRect.top, sHelperRect.right, sHelperRect.bottom); // If it was intersecting before, should be attached to the parent boolean oldIntersects = (subview.getParent() != null); if (intersects != oldIntersects) { int clippedSoFar = 0; for (int i = 0; i < mAllChildrenCount; i++) { if (mAllChildren[i] == subview) { updateSubviewClipStatus(mClippingRect, i, clippedSoFar); break; } if (mAllChildren[i].getParent() == null) { clippedSoFar++; } } } }
/** * 检查UI事件是否发生在视图view的区域内 * * @param v * @param ev * @return */ public static boolean intersects(View v, MotionEvent ev) { if (v == null) return false; Rect rect = new Rect(); v.getHitRect(rect); Rect r = new Rect(); r.left = r.right = (int) ev.getX(); r.bottom = r.top = (int) ev.getY(); return rect.intersects(r.left, r.top, r.right, r.bottom); }
/** * This function finds gameObjects inside a given rectangle. * * @param rectangle the rectangle specified in a left, top to bottom,right location * @return the list of items it found at the location the size is zero when nothing is found. */ public final Vector<GameObject> findItemAt(Rect rectangle) { Vector<GameObject> foundItems = new Vector<GameObject>(); for (int i = 0; i < items.size(); i++) { if (Rect.intersects(rectangle, (items.get(i).position))) { foundItems.add(items.get(i)); } } return foundItems; }
public void update() { follow(); centerX += speedX; centerY += speedY; speedX = bg.getSpeedX() * 5 + movementSpeedX; speedY = movementSpeedY; r.set(centerX - 25, centerY - 25, 50, 60); if (r.intersects(r, Robot.yellowRed)) { checkCollision(); } }
public boolean checkAndAdd(Rect cur) { for (Rect o : objs.subSet( new Rect(0, cur.top - maxtextsize + 1, 0, 0), new Rect(0, cur.bottom + maxtextsize, 0, 0))) { if (Rect.intersects(o, cur)) { return false; } } objs.add(cur); return true; }
@Override public void draw(Canvas canvas, Rect canvasDimensions) { if (enabled) { // normalize left left = (left % width); for (int i = 0; i < repeat; i++) { final int tLeft = left + i * width; if (canvasDimensions.intersects(tLeft, top, tLeft + width, top + height)) { canvas.drawBitmap(bitmap, tLeft, top, null); } } } }
public static View getFirstIntersectsChild(RecyclerView recyclerView) { int childCount = recyclerView.getChildCount(); if (childCount > 0) { for (int i = 0; i < childCount; i++) { View child = recyclerView.getChildAt(i); Rect rect1 = new Rect( recyclerView.getLeft(), recyclerView.getTop(), recyclerView.getRight(), recyclerView.getBottom()); Rect rect2 = new Rect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom()); if (Rect.intersects(rect1, rect2)) { return child; } } } return null; }
@Override public void draw(Canvas canvas) { final Rect r = new Rect(getBounds()); if (canvas.getClipBounds(sTempRect) && !Rect.intersects(sTempRect, r)) { // The draw region has been clipped. return; } if (mIndicatorRectDirty) { initIndicatorRect(); } final float iconScale; if ((mAnimationProgress >= ANIMATION_PROGRESS_STARTED) && (mAnimationProgress < ANIMATION_PROGRESS_COMPLETED)) { mPaint.setAlpha((int) ((1 - mAnimationProgress) * 255)); mBgDrawable.setAlpha(mPaint.getAlpha()); mBgDrawable.draw(canvas); canvas.drawOval(mIndicatorRect, mPaint); iconScale = ICON_SCALE_FACTOR + (1 - ICON_SCALE_FACTOR) * mAnimationProgress; } else if (mAnimationProgress == ANIMATION_PROGRESS_STOPPED) { mPaint.setAlpha(255); iconScale = ICON_SCALE_FACTOR; mBgDrawable.setAlpha(255); mBgDrawable.draw(canvas); if (mProgress >= 100) { canvas.drawOval(mIndicatorRect, mPaint); } else if (mProgress > 0) { canvas.drawArc(mIndicatorRect, -90, mProgress * 3.6f, false, mPaint); } } else { iconScale = 1; } canvas.save(); canvas.scale(iconScale, iconScale, r.exactCenterX(), r.exactCenterY()); mIcon.draw(canvas); canvas.restore(); }
private void updateSubviewClipStatus(Rect clippingRect, int idx, int clippedSoFar) { View child = Assertions.assertNotNull(mAllChildren)[idx]; sHelperRect.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom()); boolean intersects = clippingRect.intersects( sHelperRect.left, sHelperRect.top, sHelperRect.right, sHelperRect.bottom); boolean needUpdateClippingRecursive = false; // We never want to clip children that are being animated, as this can easily break layout : // when layout animation changes size and/or position of views contained inside a listview that // clips offscreen children, we need to ensure that, when view exits the viewport, final size // and position is set prior to removing the view from its listview parent. // Otherwise, when view gets re-attached again, i.e when it re-enters the viewport after scroll, // it won't be size and located properly. Animation animation = child.getAnimation(); boolean isAnimating = animation != null && !animation.hasEnded(); if (!intersects && child.getParent() != null && !isAnimating) { // We can try saving on invalidate call here as the view that we remove is out of visible area // therefore invalidation is not necessary. super.removeViewsInLayout(idx - clippedSoFar, 1); needUpdateClippingRecursive = true; } else if (intersects && child.getParent() == null) { super.addViewInLayout(child, idx - clippedSoFar, sDefaultLayoutParam, true); invalidate(); needUpdateClippingRecursive = true; } else if (intersects) { // If there is any intersection we need to inform the child to update its clipping rect needUpdateClippingRecursive = true; } if (needUpdateClippingRecursive) { if (child instanceof ReactClippingViewGroup) { // we don't use {@link sHelperRect} until the end of this loop, therefore it's safe // to call this method that may write to the same {@link sHelperRect} object. ReactClippingViewGroup clippingChild = (ReactClippingViewGroup) child; if (clippingChild.getRemoveClippedSubviews()) { clippingChild.updateClippingRect(); } } } }
@Override protected void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { if (shadow) { return; } final int size = mPoints.size(); if (size < 2) { // nothing to paint return; } final Projection pj = mapView.getProjection(); // 180° in longitude in pixels, for avoiding lines > 180° in length final int halfMapSize = TileSystem.MapSize(mapView.getProjection().getZoomLevel()) / 2; // southern Limit of the map in Pixels, for detecting map crossing lines final int southLimit = pj.toPixelsFromMercator(0, halfMapSize * 2, null).y - 1; final int northLimit = southLimit - halfMapSize * 2 + 1; // calculate clipBounds in screen coordinates for removing "invisible" line segments final Rect clipBounds = new Rect(0, 0, mapView.getWidth() - 1, mapView.getHeight() - 1); // take into account map orientation: if (mapView.getMapOrientation() != 0.0f) GeometryMath.getBoundingBoxForRotatatedRectangle( clipBounds, mapView.getMapOrientation(), clipBounds); // area of visible line segments Rect pathExtension = new Rect( clipBounds); // used for decision: canvas.draw (fast) or bitmap draw (slow, avoiding // OpenGLRenderer problem) boolean lineVisible = false; // precompute new points to the intermediate projection. precomputePoints(pj); final Point projectedPoint0 = mPoints.get(0); // points from the points list final Point screenPoint0 = pj.toPixelsFromProjected(projectedPoint0, mTempPoint1); // points on screen if (screenPoint0.y < northLimit) screenPoint0.y += 2 * halfMapSize; Point projectedPoint1; Point screenPoint1; mPath.rewind(); for (int i = 1; i < size; i++) { // compute next points projectedPoint1 = mPoints.get(i); screenPoint1 = pj.toPixelsFromProjected(projectedPoint1, this.mTempPoint2); if (screenPoint1.y < northLimit) screenPoint1.y += 2 * halfMapSize; if (Math.abs(screenPoint1.x - screenPoint0.x) + Math.abs(screenPoint1.y - screenPoint0.y) <= 1 && screenPoint0.y != southLimit && screenPoint1.y != southLimit) { // do not skip points crossing maps! // skip this point, too close to previous point continue; } // check for lines exceeding 180° in longitude, cut line into two segments if ((Math.abs(screenPoint1.x - screenPoint0.x) > halfMapSize) // check for lines crossing the southern limit || (screenPoint1.y > southLimit) != (screenPoint0.y > southLimit)) { // handle x and y coordinates separately int x0 = screenPoint0.x; int y0 = screenPoint0.y; int x1 = screenPoint1.x; int y1 = screenPoint1.y; // first check x if (Math.abs(screenPoint1.x - screenPoint0.x) > halfMapSize) { // x has to be adjusted if (screenPoint1.x < mapView.getWidth() / 2) { // screenPoint1 is left of screenPoint0 x1 += halfMapSize * 2; // set x1 360° east of screenPoint1 x0 -= halfMapSize * 2; // set x0 360° west of screenPoint0 } else { x1 -= halfMapSize * 2; x0 += halfMapSize * 2; } } // now check y if ((screenPoint1.y > southLimit) != (screenPoint0.y > southLimit)) { // line is crossing from one map to the other if (screenPoint1.y > southLimit) { // screenPoint1 was switched to map below y1 -= halfMapSize * 2; // set y1 into northern map y0 += halfMapSize * 2; // set y0 into map below } else { y1 += halfMapSize * 2; y0 -= halfMapSize * 2; } } // create rectangle of line segment, ensure top < bottom and right < left to obtain valid // rectangle! mLineBounds.set( Math.min(screenPoint0.x, x1), Math.min(screenPoint0.y, y1), Math.max(screenPoint0.x, x1), Math.max(screenPoint0.y, y1)); // check whether this line segment is visible if (Rect.intersects(clipBounds, mLineBounds)) { mPath.moveTo(screenPoint0.x, screenPoint0.y); mPath.lineTo(x1, y1); pathExtension.union( mLineBounds); // caution! buggy for invalid rectangles (top > bottom or right > left) // ! lineVisible = true; } screenPoint0.x = x0; screenPoint0.y = y0; } // end of line break check // check, whether this line segment is visible, ensure top < bottom and right < left to // obtain valid rectangle! mLineBounds.set( Math.min(screenPoint0.x, screenPoint1.x), Math.min(screenPoint0.y, screenPoint1.y), Math.max(screenPoint0.x, screenPoint1.x), Math.max(screenPoint0.y, screenPoint1.y)); if (Rect.intersects(clipBounds, mLineBounds)) { mPath.moveTo(screenPoint0.x, screenPoint0.y); mPath.lineTo(screenPoint1.x, screenPoint1.y); pathExtension.union(mLineBounds); lineVisible = true; } // update starting point to next position screenPoint0.x = screenPoint1.x; screenPoint0.y = screenPoint1.y; } // send only visible lines to canvas, not segments outside screen area if (lineVisible) { // check, whether visible line segments cover less than 2048 pixels rectangle. if (Math.max(pathExtension.width(), pathExtension.height()) <= 2048) { // Use fast canvas.drawPath method. canvas.drawPath(mPath, mPaint); } else { // fixing W/OpenGLRenderer(29239): Shape path too large to be rendered into a texture. // This will occur, if memory exceeds 2040 pixels => Path will disappear. // Draw path into temporary bitmap in screen size, then send bitmap to screen canvas. // <application android:hardwareAccelerated="false" > does not fix the OpenGLRenderer // problem! // Line (80, 80) to (-80, 80) will disappear at zoomLevel 4, Line (90, 90) to (-90, 90) at // zoomLevel 3 final Rect dest = new Rect(0, 0, mapView.getWidth() - 1, mapView.getHeight() - 1); // dest.set(0, 0, mapView.getWidth()-1, mapView.getHeight()-1); // final Bitmap.Config conf = Bitmap.Config.ARGB_8888; // 8 bit Aplpha, RGB if (mBmp == null || mBmp.getWidth() != mapView.getWidth() || mBmp.getHeight() != mapView.getHeight()) { // create Bitmap only once if needed mBmp = Bitmap.createBitmap( mapView.getWidth(), mapView.getHeight(), Bitmap.Config.ARGB_8888); // this most time consuming step, app. 30 ms! } else mBmp.eraseColor( Color .TRANSPARENT); // recycle bitmap and erase the old bitmap, much faster 0.6 .. 0.9 // ms final Canvas bmpcanvas = new Canvas(mBmp); // Draw path to bitmap according to actual screen rotation if (mapView.getMapOrientation() != 0.0f) bmpcanvas.rotate( mapView.getMapOrientation(), mapView.getWidth() / 2, mapView.getHeight() / 2); bmpcanvas.drawPath(mPath, mPaint); // Draw bitmap according to actual screen rotation, derotate canvas if necessary if (mapView.getMapOrientation() != 0.0f) canvas.rotate( -mapView.getMapOrientation(), mapView.getWidth() / 2, mapView.getHeight() / 2); canvas.drawBitmap(mBmp, dest, dest, null); // restore canvas rotation if necessary if (mapView.getMapOrientation() != 0.0f) canvas.rotate( mapView.getMapOrientation(), mapView.getWidth() / 2, mapView.getHeight() / 2); } } }
protected void drawOld(final Canvas canvas, final MapView mapView, final boolean shadow) { if (shadow) { return; } final int size = this.mPoints.size(); if (size < 2) { // nothing to paint return; } final Projection pj = mapView.getProjection(); // precompute new points to the intermediate projection. precomputePoints(pj); Point screenPoint0 = null; // points on screen Point screenPoint1; Point projectedPoint0; // points from the points list Point projectedPoint1; // clipping rectangle in the intermediate projection, to avoid performing projection. BoundingBoxE6 boundingBox = pj.getBoundingBox(); Point topLeft = pj.toProjectedPixels(boundingBox.getLatNorthE6(), boundingBox.getLonWestE6(), null); Point bottomRight = pj.toProjectedPixels(boundingBox.getLatSouthE6(), boundingBox.getLonEastE6(), null); final Rect clipBounds = new Rect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y); // take into account map orientation: if (mapView.getMapOrientation() != 0.0f) GeometryMath.getBoundingBoxForRotatatedRectangle( clipBounds, mapView.getMapOrientation(), clipBounds); mPath.rewind(); projectedPoint0 = this.mPoints.get(size - 1); mLineBounds.set(projectedPoint0.x, projectedPoint0.y, projectedPoint0.x, projectedPoint0.y); for (int i = size - 2; i >= 0; i--) { // compute next points projectedPoint1 = this.mPoints.get(i); mLineBounds.union(projectedPoint1.x, projectedPoint1.y); if (!Rect.intersects(clipBounds, mLineBounds)) { // skip this line, move to next point projectedPoint0 = projectedPoint1; screenPoint0 = null; continue; } // the starting point may be not calculated, because previous segment was out of clip // bounds if (screenPoint0 == null) { screenPoint0 = pj.toPixelsFromProjected(projectedPoint0, this.mTempPoint1); mPath.moveTo(screenPoint0.x, screenPoint0.y); } screenPoint1 = pj.toPixelsFromProjected(projectedPoint1, this.mTempPoint2); // skip this point, too close to previous point if (Math.abs(screenPoint1.x - screenPoint0.x) + Math.abs(screenPoint1.y - screenPoint0.y) <= 1) { continue; } mPath.lineTo(screenPoint1.x, screenPoint1.y); // update starting point to next position projectedPoint0 = projectedPoint1; screenPoint0.x = screenPoint1.x; screenPoint0.y = screenPoint1.y; mLineBounds.set(projectedPoint0.x, projectedPoint0.y, projectedPoint0.x, projectedPoint0.y); } canvas.drawPath(mPath, mPaint); }
public boolean colision(Rect s1, Rect s2) { return Rect.intersects(s1, s2); }
public boolean collision(GameObject a, GameObject b) { if (Rect.intersects(a.getRectangle(), b.getRectangle())) { return true; } return false; }
public boolean isTouched(Rect touchRect) { return Rect.intersects(rect, touchRect); }
/** * This method draws the line. Note - highly optimized to handle long paths, proceed with care. * Should be fine up to 10K points. */ @Override protected void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { if (shadow) { return; } if (this.mPoints.size() < 2) { // nothing to paint return; } final Projection pj = mapView.getProjection(); // precompute new points to the intermediate projection. final int size = this.mPoints.size(); while (this.mPointsPrecomputed < size) { final Point pt = this.mPoints.get(this.mPointsPrecomputed); pj.toMapPixelsProjected(pt.x, pt.y, pt); this.mPointsPrecomputed++; } Point screenPoint0 = null; // points on screen Point screenPoint1 = null; Point projectedPoint0; // points from the points list Point projectedPoint1; // clipping rectangle in the intermediate projection, to avoid performing projection. final Rect clipBounds = pj.fromPixelsToProjected(pj.getScreenRect()); mPath.rewind(); projectedPoint0 = this.mPoints.get(size - 1); mLineBounds.set(projectedPoint0.x, projectedPoint0.y, projectedPoint0.x, projectedPoint0.y); for (int i = size - 2; i >= 0; i--) { // compute next points projectedPoint1 = this.mPoints.get(i); mLineBounds.union(projectedPoint1.x, projectedPoint1.y); if (!Rect.intersects(clipBounds, mLineBounds)) { // skip this line, move to next point projectedPoint0 = projectedPoint1; screenPoint0 = null; continue; } // the starting point may be not calculated, because previous segment was out of clip // bounds if (screenPoint0 == null) { screenPoint0 = pj.toMapPixelsTranslated(projectedPoint0, this.mTempPoint1); mPath.moveTo(screenPoint0.x, screenPoint0.y); } screenPoint1 = pj.toMapPixelsTranslated(projectedPoint1, this.mTempPoint2); // skip this point, too close to previous point if (Math.abs(screenPoint1.x - screenPoint0.x) + Math.abs(screenPoint1.y - screenPoint0.y) <= 1) { continue; } mPath.lineTo(screenPoint1.x, screenPoint1.y); // update starting point to next position projectedPoint0 = projectedPoint1; screenPoint0.x = screenPoint1.x; screenPoint0.y = screenPoint1.y; mLineBounds.set(projectedPoint0.x, projectedPoint0.y, projectedPoint0.x, projectedPoint0.y); } canvas.drawPath(mPath, this.mPaint); }