private int x1; private int y1; private int width1; private int height1; private int x2; private int y2; private int width2; private int height2; private int endx; private int startx; private int endy; private int starty; private int height; private int width; private int area; private int area1; private int area2; private float ratio; /** * 检测两个矩形是否重叠,重叠的面积为50以上才认定为重叠 */ private boolean isOverlap(View view1, View view2) { x1 = view1.getLeft(); y1 = view1.getRight(); width1 = view1.getWidth(); height1 = view1.getHeight(); x2 = view2.getLeft(); y2 = view2.getRight(); width2 = view2.getWidth(); height2 = view2.getHeight(); endx = Math.max(x1 + width1, x2 + height2); startx = Math.min(x1, x2); width = width1 + width2 - (endx - startx); endy = Math.max(y1 + height1, y2 + height2); starty = Math.min(y1, y2); height = height1 + height2 - (endy - starty); if (width <= 0 || height <= 0) return false; else { area = width * height; area1 = width1 * height1; area2 = width2 * height2; ratio = area / (area1 + area2 - area); if (ratio >= 0.5) { return true; } } return false; }
进行了一万次的运算用了45毫秒