Example usage for android.graphics Rect intersect

List of usage examples for android.graphics Rect intersect

Introduction

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

Prototype

@CheckResult
public boolean intersect(int left, int top, int right, int bottom) 

Source Link

Document

If the rectangle specified by left,top,right,bottom intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.

Usage

From source file:com.huyn.demogroup.bahavior.widget.CoordinatorLayout.java

private void offsetChildByInset(final View child, final Rect inset, final int layoutDirection) {
    if (!ViewCompat.isLaidOut(child)) {
        // The view has not been laid out yet,
        // so we can't obtain its bounds.
        return;/*w  w  w.  j  a v a 2 s . co m*/
    }

    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int absDodgeInsetEdges = GravityCompat.getAbsoluteGravity(lp.dodgeInsetEdges, layoutDirection);

    final Behavior behavior = lp.getBehavior();
    final Rect rect = mTempRect3;
    if (behavior != null && behavior.getInsetDodgeRect(this, child, rect)) {
        // Make sure that it intersects the views bounds
        if (!rect.intersect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom())) {
            throw new IllegalArgumentException("Rect should intersect with child's bounds.");
        }
    } else {
        rect.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
    }

    if (rect.isEmpty()) {
        // Rect is empty so there is nothing to dodge against, skip...
        return;
    }

    boolean offsetY = false;
    if ((absDodgeInsetEdges & Gravity.TOP) == Gravity.TOP) {
        int distance = rect.top - lp.topMargin - lp.mInsetOffsetY;
        if (distance < inset.top) {
            setInsetOffsetY(child, inset.top - distance);
            offsetY = true;
        }
    }
    if ((absDodgeInsetEdges & Gravity.BOTTOM) == Gravity.BOTTOM) {
        int distance = getHeight() - rect.bottom - lp.bottomMargin + lp.mInsetOffsetY;
        if (distance < inset.bottom) {
            setInsetOffsetY(child, distance - inset.bottom);
            offsetY = true;
        }
    }
    if (!offsetY) {
        SysoutUtil.sysout("CoordinatorLayout", "offsetChildByInset");
        setInsetOffsetY(child, 0);
    }

    boolean offsetX = false;
    if ((absDodgeInsetEdges & Gravity.LEFT) == Gravity.LEFT) {
        int distance = rect.left - lp.leftMargin - lp.mInsetOffsetX;
        if (distance < inset.left) {
            setInsetOffsetX(child, inset.left - distance);
            offsetX = true;
        }
    }
    if ((absDodgeInsetEdges & Gravity.RIGHT) == Gravity.RIGHT) {
        int distance = getWidth() - rect.right - lp.rightMargin + lp.mInsetOffsetX;
        if (distance < inset.right) {
            setInsetOffsetX(child, distance - inset.right);
            offsetX = true;
        }
    }
    if (!offsetX) {
        setInsetOffsetX(child, 0);
    }
}