Example usage for android.support.v4.view GravityCompat getAbsoluteGravity

List of usage examples for android.support.v4.view GravityCompat getAbsoluteGravity

Introduction

In this page you can find the example usage for android.support.v4.view GravityCompat getAbsoluteGravity.

Prototype

public static int getAbsoluteGravity(int i, int i2) 

Source Link

Usage

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

/**
 * Dispatch any dependent view changes to the relevant {@link Behavior} instances.
 *
 * Usually run as part of the pre-draw step when at least one child view has a reported
 * dependency on another view. This allows CoordinatorLayout to account for layout
 * changes and animations that occur outside of the normal layout pass.
 *
 * It can also be ran as part of the nested scrolling dispatch to ensure that any offsetting
 * is completed within the correct coordinate window.
 *
 * The offsetting behavior implemented here does not store the computed offset in
 * the LayoutParams; instead it expects that the layout process will always reconstruct
 * the proper positioning./* w  w w.  ja va 2 s .  c  om*/
 *
 * @param type the type of event which has caused this call
 */
final void onChildViewsChanged(@DispatchChangeEvent final int type) {
    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    final int childCount = mDependencySortedChildren.size();
    final Rect inset = mTempRect4;
    inset.setEmpty();
    for (int i = 0; i < childCount; i++) {
        final View child = mDependencySortedChildren.get(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        // Check child views before for anchor
        for (int j = 0; j < i; j++) {
            final View checkChild = mDependencySortedChildren.get(j);

            if (lp.mAnchorDirectChild == checkChild) {
                offsetChildToAnchor(child, layoutDirection);
            }
        }

        // Get the current draw rect of the view
        final Rect drawRect = mTempRect1;
        getChildRect(child, true, drawRect);

        // Accumulate inset sizes
        if (lp.insetEdge != Gravity.NO_GRAVITY && !drawRect.isEmpty()) {
            final int absInsetEdge = GravityCompat.getAbsoluteGravity(lp.insetEdge, layoutDirection);
            switch (absInsetEdge & Gravity.VERTICAL_GRAVITY_MASK) {
            case Gravity.TOP:
                inset.top = Math.max(inset.top, drawRect.bottom);
                break;
            case Gravity.BOTTOM:
                inset.bottom = Math.max(inset.bottom, getHeight() - drawRect.top);
                break;
            }
            switch (absInsetEdge & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.LEFT:
                inset.left = Math.max(inset.left, drawRect.right);
                break;
            case Gravity.RIGHT:
                inset.right = Math.max(inset.right, getWidth() - drawRect.left);
                break;
            }
        }

        // Dodge inset edges if necessary
        if (lp.dodgeInsetEdges != Gravity.NO_GRAVITY && child.getVisibility() == View.VISIBLE) {
            offsetChildByInset(child, inset, layoutDirection);
        }

        if (type == EVENT_PRE_DRAW) {
            // Did it change? if not continue
            final Rect lastDrawRect = mTempRect2;
            getLastChildRect(child, lastDrawRect);
            if (lastDrawRect.equals(drawRect)) {
                continue;
            }
            recordLastChildRect(child, drawRect);
        }

        // Update any behavior-dependent views for the change
        for (int j = i + 1; j < childCount; j++) {
            final View checkChild = mDependencySortedChildren.get(j);
            final LayoutParams checkLp = (LayoutParams) checkChild.getLayoutParams();
            final Behavior b = checkLp.getBehavior();

            if (b != null && b.layoutDependsOn(this, checkChild, child)) {
                if (type == EVENT_PRE_DRAW && checkLp.getChangedAfterNestedScroll()) {
                    // If this is from a pre-draw and we have already been changed
                    // from a nested scroll, skip the dispatch and reset the flag
                    checkLp.resetChangedAfterNestedScroll();
                    continue;
                }

                final boolean handled;
                switch (type) {
                case EVENT_VIEW_REMOVED:
                    // EVENT_VIEW_REMOVED means that we need to dispatch
                    // onDependentViewRemoved() instead
                    b.onDependentViewRemoved(this, checkChild, child);
                    handled = true;
                    break;
                default:
                    // Otherwise we dispatch onDependentViewChanged()
                    handled = b.onDependentViewChanged(this, checkChild, child);
                    break;
                }

                if (type == EVENT_NESTED_SCROLL) {
                    // If this is from a nested scroll, set the flag so that we may skip
                    // any resulting onPreDraw dispatch (if needed)
                    checkLp.setChangedAfterNestedScroll(handled);
                }
            }
        }
    }
}

From source file:android.support.design.widget.CoordinatorLayout.java

/**
 * Dispatch any dependent view changes to the relevant {@link Behavior} instances.
 *
 * Usually run as part of the pre-draw step when at least one child view has a reported
 * dependency on another view. This allows CoordinatorLayout to account for layout
 * changes and animations that occur outside of the normal layout pass.
 *
 * It can also be ran as part of the nested scrolling dispatch to ensure that any offsetting
 * is completed within the correct coordinate window.
 *
 * The offsetting behavior implemented here does not store the computed offset in
 * the LayoutParams; instead it expects that the layout process will always reconstruct
 * the proper positioning.//from  www .jav  a2  s .com
 *
 * @param type the type of event which has caused this call
 */
final void onChildViewsChanged(@DispatchChangeEvent final int type) {
    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    final int childCount = mDependencySortedChildren.size();
    final Rect inset = acquireTempRect();
    final Rect drawRect = acquireTempRect();
    final Rect lastDrawRect = acquireTempRect();

    for (int i = 0; i < childCount; i++) {
        final View child = mDependencySortedChildren.get(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (type == EVENT_PRE_DRAW && child.getVisibility() == View.GONE) {
            // Do not try to update GONE child views in pre draw updates.
            continue;
        }

        // Check child views before for anchor
        for (int j = 0; j < i; j++) {
            final View checkChild = mDependencySortedChildren.get(j);

            if (lp.mAnchorDirectChild == checkChild) {
                offsetChildToAnchor(child, layoutDirection);
            }
        }

        // Get the current draw rect of the view
        getChildRect(child, true, drawRect);

        // Accumulate inset sizes
        if (lp.insetEdge != Gravity.NO_GRAVITY && !drawRect.isEmpty()) {
            final int absInsetEdge = GravityCompat.getAbsoluteGravity(lp.insetEdge, layoutDirection);
            switch (absInsetEdge & Gravity.VERTICAL_GRAVITY_MASK) {
            case Gravity.TOP:
                inset.top = Math.max(inset.top, drawRect.bottom);
                break;
            case Gravity.BOTTOM:
                inset.bottom = Math.max(inset.bottom, getHeight() - drawRect.top);
                break;
            }
            switch (absInsetEdge & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.LEFT:
                inset.left = Math.max(inset.left, drawRect.right);
                break;
            case Gravity.RIGHT:
                inset.right = Math.max(inset.right, getWidth() - drawRect.left);
                break;
            }
        }

        // Dodge inset edges if necessary
        if (lp.dodgeInsetEdges != Gravity.NO_GRAVITY && child.getVisibility() == View.VISIBLE) {
            offsetChildByInset(child, inset, layoutDirection);
        }

        if (type != EVENT_VIEW_REMOVED) {
            // Did it change? if not continue
            getLastChildRect(child, lastDrawRect);
            if (lastDrawRect.equals(drawRect)) {
                continue;
            }
            recordLastChildRect(child, drawRect);
        }

        // Update any behavior-dependent views for the change
        for (int j = i + 1; j < childCount; j++) {
            final View checkChild = mDependencySortedChildren.get(j);
            final LayoutParams checkLp = (LayoutParams) checkChild.getLayoutParams();
            final Behavior b = checkLp.getBehavior();

            if (b != null && b.layoutDependsOn(this, checkChild, child)) {
                if (type == EVENT_PRE_DRAW && checkLp.getChangedAfterNestedScroll()) {
                    // If this is from a pre-draw and we have already been changed
                    // from a nested scroll, skip the dispatch and reset the flag
                    checkLp.resetChangedAfterNestedScroll();
                    continue;
                }

                final boolean handled;
                switch (type) {
                case EVENT_VIEW_REMOVED:
                    // EVENT_VIEW_REMOVED means that we need to dispatch
                    // onDependentViewRemoved() instead
                    b.onDependentViewRemoved(this, checkChild, child);
                    handled = true;
                    break;
                default:
                    // Otherwise we dispatch onDependentViewChanged()
                    handled = b.onDependentViewChanged(this, checkChild, child);
                    break;
                }

                if (type == EVENT_NESTED_SCROLL) {
                    // If this is from a nested scroll, set the flag so that we may skip
                    // any resulting onPreDraw dispatch (if needed)
                    checkLp.setChangedAfterNestedScroll(handled);
                }
            }
        }
    }

    releaseTempRect(inset);
    releaseTempRect(drawRect);
    releaseTempRect(lastDrawRect);
}

From source file:com.appunite.list.GridView.java

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly./*from  w  ww. jav a2 s.  c om*/
 *
 * @param child The view to add
 * @param position The position of the view
 * @param y The y position relative to which this view will be positioned
 * @param flow if true, align top edge to y. If false, align bottom edge
 *        to y.
 * @param childrenLeft Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 * @param where Where to add the item in the list
 *
 */
private void setupChild(View child, int position, int y, boolean flow, int childrenLeft, boolean selected,
        boolean recycled, int where) {
    boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();

    boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make
    // some up...
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if (recycled && !p.forceAdd) {
        attachViewToParent(child, where, p);
    } else {
        p.forceAdd = false;
        addViewInLayout(child, where, p, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
        if (isSelected) {
            requestFocus();
        }
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (getContext()
                .getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            Compat.setActivated(child, mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup
                .getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);

        int childWidthSpec = ViewGroup.getChildMeasureSpec(
                MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();

    int childLeft;
    final int childTop = flow ? y : y - h;

    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    final int absoluteGravity = GravityCompat.getAbsoluteGravity(mGravity, layoutDirection);
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        childLeft = childrenLeft;
        break;
    case Gravity.CENTER_HORIZONTAL:
        childLeft = childrenLeft + ((mColumnWidth - w) / 2);
        break;
    case Gravity.RIGHT:
        childLeft = childrenLeft + mColumnWidth - w;
        break;
    default:
        childLeft = childrenLeft;
        break;
    }

    if (needToMeasure) {
        final int childRight = childLeft + w;
        final int childBottom = childTop + h;
        child.layout(childLeft, childTop, childRight, childBottom);
    } else {
        child.offsetLeftAndRight(childLeft - child.getLeft());
        child.offsetTopAndBottom(childTop - child.getTop());
    }

    if (mCachingStarted) {
        child.setDrawingCacheEnabled(true);
    }

    if (recycled && (((LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
        Compat.jumpDrawablesToCurrentState(child);
    }
}

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;/* www.ja v a  2 s  . c  o  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);
    }
}

From source file:com.huangj.huangjlibrary.widget.drawerlayout.DrawerLayout.java

boolean isDrawerView(View child) {
    final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
    final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(child));
    if ((absGravity & Gravity.LEFT) != 0) {
        // This child is a left-edge drawer
        return true;
    }/* ww w.j  a va2s.co m*/
    if ((absGravity & Gravity.RIGHT) != 0) {
        // This child is a right-edge drawer
        return true;
    }
    return false;
}

From source file:android.support.design.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 . ja va 2  s  . c om
    }

    if (child.getWidth() <= 0 || child.getHeight() <= 0) {
        // Bounds are empty so there is nothing to dodge against, skip...
        return;
    }

    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final Behavior behavior = lp.getBehavior();
    final Rect dodgeRect = acquireTempRect();
    final Rect bounds = acquireTempRect();
    bounds.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());

    if (behavior != null && behavior.getInsetDodgeRect(this, child, dodgeRect)) {
        // Make sure that the rect is within the view's bounds
        if (!bounds.contains(dodgeRect)) {
            throw new IllegalArgumentException("Rect should be within the child's bounds." + " Rect:"
                    + dodgeRect.toShortString() + " | Bounds:" + bounds.toShortString());
        }
    } else {
        dodgeRect.set(bounds);
    }

    // We can release the bounds rect now
    releaseTempRect(bounds);

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

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

    boolean offsetY = false;
    if ((absDodgeInsetEdges & Gravity.TOP) == Gravity.TOP) {
        int distance = dodgeRect.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() - dodgeRect.bottom - lp.bottomMargin + lp.mInsetOffsetY;
        if (distance < inset.bottom) {
            setInsetOffsetY(child, distance - inset.bottom);
            offsetY = true;
        }
    }
    if (!offsetY) {
        setInsetOffsetY(child, 0);
    }

    boolean offsetX = false;
    if ((absDodgeInsetEdges & Gravity.LEFT) == Gravity.LEFT) {
        int distance = dodgeRect.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() - dodgeRect.right - lp.rightMargin + lp.mInsetOffsetX;
        if (distance < inset.right) {
            setInsetOffsetX(child, distance - inset.right);
            offsetX = true;
        }
    }
    if (!offsetX) {
        setInsetOffsetX(child, 0);
    }

    releaseTempRect(dodgeRect);
}

From source file:android.support.v7.widget.LinearLayoutCompat.java

/**
 * Position the children during a layout pass if the orientation of this
 * LinearLayout is set to {@link #VERTICAL}.
 *
 * @see #getOrientation()//  w w w .  ja  v  a 2s. c  om
 * @see #setOrientation(int)
 * @see #onLayout(boolean, int, int, int, int)
 * @param left
 * @param top
 * @param right
 * @param bottom
 */
void layoutVertical(int left, int top, int right, int bottom) {
    final int paddingLeft = getPaddingLeft();

    int childTop;
    int childLeft;

    // Where right end of child should go
    final int width = right - left;
    int childRight = width - getPaddingRight();

    // Space available for child
    int childSpace = width - paddingLeft - getPaddingRight();

    final int count = getVirtualChildCount();

    final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int minorGravity = mGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK;

    switch (majorGravity) {
    case Gravity.BOTTOM:
        // mTotalLength contains the padding already
        childTop = getPaddingTop() + bottom - top - mTotalLength;
        break;

    // mTotalLength contains the padding already
    case Gravity.CENTER_VERTICAL:
        childTop = getPaddingTop() + (bottom - top - mTotalLength) / 2;
        break;

    case Gravity.TOP:
    default:
        childTop = getPaddingTop();
        break;
    }

    for (int i = 0; i < count; i++) {
        final View child = getVirtualChildAt(i);
        if (child == null) {
            childTop += measureNullChild(i);
        } else if (child.getVisibility() != GONE) {
            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();

            final LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child
                    .getLayoutParams();

            int gravity = lp.gravity;
            if (gravity < 0) {
                gravity = minorGravity;
            }
            final int layoutDirection = ViewCompat.getLayoutDirection(this);
            final int absoluteGravity = GravityCompat.getAbsoluteGravity(gravity, layoutDirection);
            switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.CENTER_HORIZONTAL:
                childLeft = paddingLeft + ((childSpace - childWidth) / 2) + lp.leftMargin - lp.rightMargin;
                break;

            case Gravity.RIGHT:
                childLeft = childRight - childWidth - lp.rightMargin;
                break;

            case Gravity.LEFT:
            default:
                childLeft = paddingLeft + lp.leftMargin;
                break;
            }

            if (hasDividerBeforeChildAt(i)) {
                childTop += mDividerHeight;
            }

            childTop += lp.topMargin;
            setChildFrame(child, childLeft, childTop + getLocationOffset(child), childWidth, childHeight);
            childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);

            i += getChildrenSkipCount(child, i);
        }
    }
}

From source file:android.support.v7.widget.LinearLayoutCompat.java

/**
 * Position the children during a layout pass if the orientation of this
 * LinearLayout is set to {@link #HORIZONTAL}.
 *
 * @see #getOrientation()/*from w w  w .  ja  v a2 s.c o  m*/
 * @see #setOrientation(int)
 * @see #onLayout(boolean, int, int, int, int)
 * @param left
 * @param top
 * @param right
 * @param bottom
 */
void layoutHorizontal(int left, int top, int right, int bottom) {
    final boolean isLayoutRtl = ViewUtils.isLayoutRtl(this);
    final int paddingTop = getPaddingTop();

    int childTop;
    int childLeft;

    // Where bottom of child should go
    final int height = bottom - top;
    int childBottom = height - getPaddingBottom();

    // Space available for child
    int childSpace = height - paddingTop - getPaddingBottom();

    final int count = getVirtualChildCount();

    final int majorGravity = mGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK;
    final int minorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;

    final boolean baselineAligned = mBaselineAligned;

    final int[] maxAscent = mMaxAscent;
    final int[] maxDescent = mMaxDescent;

    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    switch (GravityCompat.getAbsoluteGravity(majorGravity, layoutDirection)) {
    case Gravity.RIGHT:
        // mTotalLength contains the padding already
        childLeft = getPaddingLeft() + right - left - mTotalLength;
        break;

    case Gravity.CENTER_HORIZONTAL:
        // mTotalLength contains the padding already
        childLeft = getPaddingLeft() + (right - left - mTotalLength) / 2;
        break;

    case Gravity.LEFT:
    default:
        childLeft = getPaddingLeft();
        break;
    }

    int start = 0;
    int dir = 1;
    //In case of RTL, start drawing from the last child.
    if (isLayoutRtl) {
        start = count - 1;
        dir = -1;
    }

    for (int i = 0; i < count; i++) {
        int childIndex = start + dir * i;
        final View child = getVirtualChildAt(childIndex);

        if (child == null) {
            childLeft += measureNullChild(childIndex);
        } else if (child.getVisibility() != GONE) {
            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();
            int childBaseline = -1;

            final LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child
                    .getLayoutParams();

            if (baselineAligned && lp.height != LayoutParams.MATCH_PARENT) {
                childBaseline = child.getBaseline();
            }

            int gravity = lp.gravity;
            if (gravity < 0) {
                gravity = minorGravity;
            }

            switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
            case Gravity.TOP:
                childTop = paddingTop + lp.topMargin;
                if (childBaseline != -1) {
                    childTop += maxAscent[INDEX_TOP] - childBaseline;
                }
                break;

            case Gravity.CENTER_VERTICAL:
                // Removed support for baseline alignment when layout_gravity or
                // gravity == center_vertical. See bug #1038483.
                // Keep the code around if we need to re-enable this feature
                // if (childBaseline != -1) {
                //     // Align baselines vertically only if the child is smaller than us
                //     if (childSpace - childHeight > 0) {
                //         childTop = paddingTop + (childSpace / 2) - childBaseline;
                //     } else {
                //         childTop = paddingTop + (childSpace - childHeight) / 2;
                //     }
                // } else {
                childTop = paddingTop + ((childSpace - childHeight) / 2) + lp.topMargin - lp.bottomMargin;
                break;

            case Gravity.BOTTOM:
                childTop = childBottom - childHeight - lp.bottomMargin;
                if (childBaseline != -1) {
                    int descent = child.getMeasuredHeight() - childBaseline;
                    childTop -= (maxDescent[INDEX_BOTTOM] - descent);
                }
                break;
            default:
                childTop = paddingTop;
                break;
            }

            if (hasDividerBeforeChildAt(childIndex)) {
                childLeft += mDividerWidth;
            }

            childLeft += lp.leftMargin;
            setChildFrame(child, childLeft + getLocationOffset(child), childTop, childWidth, childHeight);
            childLeft += childWidth + lp.rightMargin + getNextLocationOffset(child);

            i += getChildrenSkipCount(child, childIndex);
        }
    }
}

From source file:com.jecelyin.editor.v2.widget.AnyDrawerLayout.java

boolean isDrawerView(View child) {
    final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
    final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(child));
    if ((absGravity & Gravity.LEFT) != 0) {
        // This child is a left-edge drawer
        return true;
    }// w  w  w  .ja  v  a2  s.co  m
    if ((absGravity & Gravity.RIGHT) != 0) {
        // This child is a right-edge drawer
        return true;
    }
    if ((absGravity & Gravity.BOTTOM) != 0) {
        // This child is a bottom-edge drawer
        return true;
    }
    return false;
}

From source file:android.support.v7.widget.Toolbar.java

/**
 * Prepare a list of non-SYSTEM child views. If the layout direction is RTL
 * this will be in reverse child order.//from   w  w  w.j av a2 s.com
 *
 * @param views List to populate. It will be cleared before use.
 * @param gravity Horizontal gravity to match against
 */
private void addCustomViewsWithGravity(List<View> views, int gravity) {
    final boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
    final int childCount = getChildCount();
    final int absGrav = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));

    views.clear();

    if (isRtl) {
        for (int i = childCount - 1; i >= 0; i--) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child)
                    && getChildHorizontalGravity(lp.gravity) == absGrav) {
                views.add(child);
            }
        }
    } else {
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp.mViewType == LayoutParams.CUSTOM && shouldLayout(child)
                    && getChildHorizontalGravity(lp.gravity) == absGrav) {
                views.add(child);
            }
        }
    }
}