Example usage for android.graphics Rect sort

List of usage examples for android.graphics Rect sort

Introduction

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

Prototype

public void sort() 

Source Link

Document

Swap top/bottom or left/right if there are flipped (i.e.

Usage

From source file:com.android.switchaccess.SwitchAccessNodeCompat.java

/**
 * Find the largest sub-rectangle that doesn't intersect a specified one.
 *
 * @param rectToModify The rect that may be modified to avoid intersections
 * @param otherRect The rect that should be avoided
 *///from  w w w .  j ava 2 s.co m
private static void adjustRectToAvoidIntersection(Rect rectToModify, Rect otherRect) {
    /*
     * Some rectangles are flipped around (left > right). Make sure we have two Rects free of
     * such pathologies.
     */
    rectToModify.sort();
    otherRect.sort();
    /*
     * Intersect rectToModify with four rects that represent cuts of the entire space along
     * lines defined by the otherRect's edges
     */
    Rect[] cuts = { new Rect(Integer.MIN_VALUE, Integer.MIN_VALUE, otherRect.left, Integer.MAX_VALUE),
            new Rect(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE, otherRect.top),
            new Rect(otherRect.right, Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE),
            new Rect(Integer.MIN_VALUE, otherRect.bottom, Integer.MAX_VALUE, Integer.MAX_VALUE) };

    int maxIntersectingRectArea = 0;
    int indexOfLargestIntersection = -1;
    for (int i = 0; i < cuts.length; i++) {
        if (cuts[i].intersect(rectToModify)) {
            /* Reassign this cut to its intersection with rectToModify */
            int visibleRectArea = cuts[i].width() * cuts[i].height();
            if (visibleRectArea > maxIntersectingRectArea) {
                maxIntersectingRectArea = visibleRectArea;
                indexOfLargestIntersection = i;
            }
        }
    }
    if (maxIntersectingRectArea <= 0) {
        // The rectToModify isn't within any of our cuts, so it's entirely occuled by otherRect.
        rectToModify.setEmpty();
        return;
    }
    rectToModify.set(cuts[indexOfLargestIntersection]);
}

From source file:com.android.switchaccess.SwitchAccessNodeCompat.java

private void updateVisibility() {
    if (!mVisibilityCalculated) {
        mVisibleBoundsInScreen = new Rect();
        getBoundsInScreen(mVisibleBoundsInScreen);
        /* Deal with visibility implications for windows above */
        Rect windowBoundsInScreen = new Rect();
        for (int i = 0; i < mWindowsAbove.size(); ++i) {
            mWindowsAbove.get(i).getBoundsInScreen(windowBoundsInScreen);
            mVisibleBoundsInScreen.sort();
            windowBoundsInScreen.sort();
            if (Rect.intersects(mVisibleBoundsInScreen, windowBoundsInScreen)) {
                adjustRectToAvoidIntersection(mVisibleBoundsInScreen, windowBoundsInScreen);
            }/*from   www .java  2  s .  c  o  m*/
        }
        mVisibilityCalculated = true;
    }
}