/** * Determines if two views intersect in the window. */ public static boolean viewsIntersect(View view1, View view2) { if (view1 == null || view2 == null) return false; final int[] view1Loc = new int[2]; view1.getLocationOnScreen(view1Loc); final Rect view1Rect = new Rect(view1Loc[0], view1Loc[1], view1Loc[0] + view1.getWidth(), view1Loc[1] + view1.getHeight()); int[] view2Loc = new int[2]; view2.getLocationOnScreen(view2Loc); final Rect view2Rect = new Rect(view2Loc[0], view2Loc[1], view2Loc[0] + view2.getWidth(), view2Loc[1] + view2.getHeight()); return view1Rect.intersect(view2Rect); }
/** * Determines if two views intersect in the window. */ public static boolean viewsIntersect(View view1, View view2) { if (view1 == null || view2 == null) return false; final int[] view1Loc = new int[2]; view1.getLocationOnScreen(view1Loc); final Rect view1Rect = new Rect(view1Loc[0], view1Loc[1], view1Loc[0] + view1.getWidth(), view1Loc[1] + view1.getHeight()); int[] view2Loc = new int[2]; view2.getLocationOnScreen(view2Loc); final Rect view2Rect = new Rect(view2Loc[0], view2Loc[1], view2Loc[0] + view2.getWidth(), view2Loc[1] + view2.getHeight()); return view1Rect.intersect(view2Rect); }
boolean intersects = intersection.intersect(surface);
@Test public void intersectRect() { Rect a = new Rect(0, 0, 10, 10); Rect b = new Rect(5, 0, 15, 10); assertThat(a.intersect(b)).isTrue(); }
@Test public void intersectCoordinates() { Rect r = new Rect(0, 0, 10, 10); assertThat(r.intersect(5, 0, 15, 10)).isTrue(); }
public Boolean CollisionTest (Rect one, Rect two) { if (one.intersect(two)) { collided = true; } else { collided = false; } return(collided); }
Rect scrollViewRect; // the bounds of your scrollview Rect tooltipRect; // the bounds of your tooltip bool intersects = tooltipRect.intersect(scrollViewRect) if(intersects) { canvas.clipRect(tooltipRect, Region.Op.REPLACE); draw(canvas); }
Rect rect1, rect2; if(rect1.intersect(rect2) //do something
public int[] extractY(byte[] yuv420, Size szYuv, Rect rcY) { rcY.intersect(0, 0, szYuv.width, szYuv.height); final int[] res = int[rcY.width()*rcY.height()]; int yCount = 0; for (int row=rcY.left; row<rcY.right; row++) { for (int col=rcY.top; col<rcY.bottom; col++) { res[yCount++] = yuv420[col+rcY.left+(row+rcY.top)*szYuv.width]; } } return res; }
public Rect getBounds(){ //put this in your sprite and enemy-class. return new Rect(x, y, x+width, y+height); } Public void checkCollision(){ for (int i = 0; i<spriteArray.size(); i++){ Rect mySprite = spriteArray.get(i).getBounds(); //create rect for every sprite in array for (int j = 0; j<spriteArray.size(); i++){ Rect myOtherSprite = spriteArray.get(i).getBounds(); if(mySprite.intersect(myOtherSprite)){ //check if they touch //Todo code here } } } }
int[] firstPosition = new int[2]; int[] secondPosition = new int[2]; firstView.getLocationOnScreen(firstPosition); secondView.getLocationOnScreen(secondPosition); // Rect constructor parameters: left, top, right, bottom Rect rectFirstView = new Rect(firstPosition[0], firstPosition[1], firstPosition[0] + firstView.getMeasuredWidth(), firstPosition[1] + firstView.getMeasuredHeight()); Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1], secondPosition[0] + secondView.getMeasuredWidth(), secondPosition[1] + secondView.getMeasuredHeight()); return rectFirstView.intersect(rectSecondView);
static boolean collide(Rect r1, Rect r2){ if(r1.intersect(r2) || r1.contains(r2) || r2.contains(r1)){ return true; } return false; }
tw.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Rect recTest1 = new Rect(); img1.getDrawingRect(recTest1); Rect recTest2 = new Rect(); img2.getDrawingRect(recTest2); System.out.println("Test : "+recTest1.intersect(recTest2)); } });
va=ValueAnimator.ofFloat(0.0f,size.y); va.setDuration(5000); va.setRepeatCount(va.INFINITE); va.setRepeatMode(va.REVERSE); va.start(); va.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { // TODO Auto-generated method stub bullet[0].setTranslationY((Float) va.getAnimatedValue()); Rect R11=new Rect(bullet[0].getLeft(),bullet[0].getTop()+(int)bullet[0].getTranslationY(),bullet[0].getRight(),bullet[0].getBottom()+(int)bullet[0].getTranslationY()); Rect R21=new Rect(ball.getLeft(), ball.getTop(), ball.getRight(), ball.getBottom()); if(R11.intersect(R21)) va.cancel(); } });
public boolean CheckCollision(View v1,View v2) { Rect R1=new Rect(v1.getLeft(), v1.getTop(), v1.getRight(), v1.getBottom()); Rect R2=new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom()); return R1.intersect(R2); }
private boolean checkCollision(View v1, View v2) { int leftMargin = ((RelativeLayout.LayoutParams) v1.getLayoutParams()).leftMargin; int topMargin = ((RelativeLayout.LayoutParams) v1.getLayoutParams()).topMargin; Rect r1 = new Rect(root.getPaddingLeft() + leftMargin, root.getPaddingTop() + topMargin, root.getPaddingLeft() + leftMargin + v2.getWidth(), root.getPaddingTop() + topMargin + v2.getHeight()); Rect r2 = new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom()); return r1.intersect(r2); }
private boolean checkHit(View v, View hit){ Rect rv = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); Rect rhit = new Rect(hit.getLeft(), hit.getTop(), hit.getRight(), hit.getBottom()); return rv.intersect(rhit); }
private boolean viewsOverlap(View v1, View v2) { int[] v1_coords = new int[2]; v1.getLocationOnScreen(v1_coords); int v1_w = v1.getWidth(); int v1_h = v1.getHeight(); Rect v1_rect = new Rect(v1_coords[0], v1_coords[1], v1_coords[0] + v1_w, v1_coords[1] + v1_h); int[] v2_coords = new int[2]; v2.getLocationOnScreen(v1_coords); int v2_w = v2.getWidth(); int v2_h = v2.getHeight(); Rect v2_rect = new Rect(v2_coords[0], v2_coords[1], v2_coords[0] + v2_w, v2_coords[1] + v2_h); return v1_rect.intersect(v2_rect) || v1_rect.contains(v2_rect) || v2_rect.contains(v1_rect); }
/** * If the specified rectangle intersects this rectangle, return true and set * this rectangle to that intersection, otherwise return false and do not * change this rectangle. No check is performed to see if either rectangle * is empty. To just test for intersection, use intersects() * * @param r The rectangle being intersected with this rectangle. * @return true if the specified rectangle and this rectangle intersect * (and this rectangle is then set to that intersection) else * return false and do not change this rectangle. */ @CheckResult public boolean intersect(Rect r) { return intersect(r.left, r.top, r.right, r.bottom); }
private boolean isViewOverlapping(View firstView, View secondView) { final int[] location = new int[2]; firstView.getLocationInWindow(location); Rect rect1 = new Rect(location[0], location[1],location[0] + firstView.getWidth(), location[1] + firstView.getHeight()); secondView.getLocationInWindow(location); Rect rect2 = new Rect(location[0], location[1],location[0] + secondView.getWidth(), location[1] + secondView.getHeight()); return rect1.intersect(rect2); // return (rect1.contains(rect2)) || (rect2.contains(rect1)); }