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.jecelyin.editor.v2.widget.AnyDrawerLayout.java

/**
 * @param gravity the gravity of the child to return. If specified as a
 *            relative value, it will be resolved according to the current
 *            layout direction.//  w w w.j  ava2 s. com
 * @return the drawer with the specified gravity
 */
View findDrawerWithGravity(int gravity) {
    final int absHorizGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this))
            & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int absVerticalGravity = GravityCompat.getAbsoluteGravity(gravity,
            ViewCompat.getLayoutDirection(this)) & Gravity.VERTICAL_GRAVITY_MASK;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final int childAbsGravity = getDrawerViewAbsoluteGravity(child);
        if ((absHorizGravity != 0 && (childAbsGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == absHorizGravity)
                || (absVerticalGravity != 0
                        && (childAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) == absVerticalGravity)

        ) {
            return child;
        }
    }
    return null;
}

From source file:org.gateshipone.odyssey.activities.OdysseyMainActivity.java

@Override
public void openArtworkSettings() {
    // Create fragment and give it an argument for the selected directory
    ArtworkSettingsFragment newFragment = new ArtworkSettingsFragment();

    FragmentManager fragmentManager = getSupportFragmentManager();

    android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();

    // set enter / exit animation
    newFragment.setEnterTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.START,
            getResources().getConfiguration().getLayoutDirection())));
    newFragment.setExitTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.END,
            getResources().getConfiguration().getLayoutDirection())));

    transaction.addToBackStack("ArtworkSettingsFragment");

    transaction.replace(R.id.fragment_container, newFragment);

    // Commit the transaction
    transaction.commit();//from   w w w. j  av  a 2  s.c o m
}

From source file:com.abewy.android.apps.klyph.widget.KlyphDrawerLayout.java

/**
 * Close the specified drawer by animating it out of view.
 *
 * @param gravity Gravity.LEFT to move the left drawer or Gravity.RIGHT for the right.
 *                GravityCompat.START or GravityCompat.END may also be used.
 *///from w w  w.ja  v a2 s. c o m
public void closeDrawer(int gravity) {
    final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));
    final View drawerView = findDrawerWithGravity(absGravity);

    if (drawerView == null) {
        throw new IllegalArgumentException(
                "No drawer view found with absolute gravity " + gravityToString(absGravity));
    }
    closeDrawer(drawerView);
}

From source file:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.java

boolean isDrawerView(View child) {
    final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
    final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(child));
    return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
}

From source file:com.zyk.drawerlayout.widget.DrawerLayout.java

/**
 * @param gravity the gravity of the child to return. If specified as a
 *            relative value, it will be resolved according to the current
 *            layout direction./*from w w  w .  j a  v  a 2 s .  com*/
 * @return the drawer with the specified gravity
 */
View findDrawerWithGravity(int gravity) {
    final int absHorizGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this))
            & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int absVerticalGravity = GravityCompat.getAbsoluteGravity(gravity,
            ViewCompat.getLayoutDirection(this)) & Gravity.VERTICAL_GRAVITY_MASK;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final int childAbsGravity = getDrawerViewAbsoluteGravity(child);
        boolean h = (absHorizGravity != 0)
                && ((childAbsGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == absHorizGravity);
        boolean v = (absVerticalGravity != 0)
                && ((childAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) == absVerticalGravity);
        if (h || v) {
            return child;
        }
    }
    return null;
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.AT_MOST) {
                widthMode = MeasureSpec.EXACTLY;
            } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = MeasureSpec.EXACTLY;
                widthSize = 300;/*  w  ww. jav a  2 s .  c  om*/
            }
            if (heightMode == MeasureSpec.AT_MOST) {
                heightMode = MeasureSpec.EXACTLY;
            } else if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = MeasureSpec.EXACTLY;
                heightSize = 300;
            }
        } else {
            throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }
    }

    setMeasuredDimension(widthSize, heightSize);

    final boolean applyInsets = mLastInsets != null && ViewCompat.getFitsSystemWindows(this);
    final int layoutDirection = ViewCompat.getLayoutDirection(this);

    // Only one drawer is permitted along each vertical edge (left / right). These two booleans
    // are tracking the presence of the edge drawers.
    boolean hasDrawerOnLeftEdge = false;
    boolean hasDrawerOnRightEdge = false;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }

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

        if (applyInsets) {
            final int cgrav = GravityCompat.getAbsoluteGravity(lp.gravity, layoutDirection);
            if (ViewCompat.getFitsSystemWindows(child)) {
                IMPL.dispatchChildInsets(child, mLastInsets, cgrav);
            } else {
                IMPL.applyMarginInsets(lp, mLastInsets, cgrav);
            }
        }

        if (isContentView(child)) {
            // Content views get measured at exactly the layout's size.
            final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin,
                    MeasureSpec.EXACTLY);
            final int contentHeightSpec = MeasureSpec
                    .makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
            child.measure(contentWidthSpec, contentHeightSpec);
        } else if (isDrawerView(child)) {
            if (SET_DRAWER_SHADOW_FROM_ELEVATION) {
                if (ViewCompat.getElevation(child) != mDrawerElevation) {
                    ViewCompat.setElevation(child, mDrawerElevation);
                }
            }
            final @EdgeGravity int childGravity = getDrawerViewAbsoluteGravity(child)
                    & Gravity.HORIZONTAL_GRAVITY_MASK;
            // Note that the isDrawerView check guarantees that childGravity here is either
            // LEFT or RIGHT
            boolean isLeftEdgeDrawer = (childGravity == Gravity.LEFT);
            if ((isLeftEdgeDrawer && hasDrawerOnLeftEdge) || (!isLeftEdgeDrawer && hasDrawerOnRightEdge)) {
                throw new IllegalStateException(
                        "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this "
                                + TAG + " already has a " + "drawer view along that edge");
            }
            if (isLeftEdgeDrawer) {
                hasDrawerOnLeftEdge = true;
            } else {
                hasDrawerOnRightEdge = true;
            }
            final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                    mMinDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
            final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin,
                    lp.height);
            child.measure(drawerWidthSpec, drawerHeightSpec);
        } else {
            throw new IllegalStateException("Child " + child + " at index " + i
                    + " does not have a valid layout_gravity - must be Gravity.LEFT, "
                    + "Gravity.RIGHT or Gravity.NO_GRAVITY");
        }
    }
}

From source file:com.aidy.bottomdrawerlayout.AllDrawerLayout.java

boolean isDrawerView(View child) {
    final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
    final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(child));
    return (absGravity & (Gravity.LEFT | Gravity.RIGHT | Gravity.TOP | Gravity.BOTTOM)) != 0;
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.AT_MOST) {
                widthMode = MeasureSpec.EXACTLY;
            } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = MeasureSpec.EXACTLY;
                widthSize = 300;//w  ww .  j  ava 2 s .com
            }
            if (heightMode == MeasureSpec.AT_MOST) {
                heightMode = MeasureSpec.EXACTLY;
            } else if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = MeasureSpec.EXACTLY;
                heightSize = 300;
            }
        } else {
            throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }
    }

    setMeasuredDimension(widthSize, heightSize);

    final boolean applyInsets = mLastInsets != null && ViewCompat.getFitsSystemWindows(this);
    final int layoutDirection = ViewCompat.getLayoutDirection(this);

    // Only one drawer is permitted along each vertical edge (left / right). These two booleans
    // are tracking the presence of the edge drawers.
    boolean hasDrawerOnLeftEdge = false;
    boolean hasDrawerOnRightEdge = false;
    boolean hasDrawerOnBottomEdge = false;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }
        final @EdgeGravity int vchildGravity = getDrawerViewAbsoluteGravity(child)
                & Gravity.VERTICAL_GRAVITY_MASK;
        boolean isBottomEdgeDrawer = (vchildGravity == Gravity.BOTTOM);

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

        if (applyInsets && !isBottomEdgeDrawer) {
            final int cgrav = GravityCompat.getAbsoluteGravity(lp.gravity, layoutDirection);
            if (ViewCompat.getFitsSystemWindows(child)) {
                IMPL.dispatchChildInsets(child, mLastInsets, cgrav);
            } else {
                IMPL.applyMarginInsets(lp, mLastInsets, cgrav);
            }
        }

        if (isContentView(child)) {
            // Content views get measured at exactly the layout's size.
            final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin,
                    MeasureSpec.EXACTLY);
            final int contentHeightSpec = MeasureSpec
                    .makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
            child.measure(contentWidthSpec, contentHeightSpec);
        } else if (isDrawerView(child)) {
            if (SET_DRAWER_SHADOW_FROM_ELEVATION) {
                if (ViewCompat.getElevation(child) != mDrawerElevation) {
                    ViewCompat.setElevation(child, mDrawerElevation);
                }
            }
            final @EdgeGravity int childGravity = getDrawerViewAbsoluteGravity(child)
                    & Gravity.HORIZONTAL_GRAVITY_MASK;
            //                final @EdgeGravity int vchildGravity =
            //                        getDrawerViewAbsoluteGravity(child) & Gravity.VERTICAL_GRAVITY_MASK;
            // Note that the isDrawerView check guarantees that childGravity here is either
            // LEFT or RIGHT
            boolean isLeftEdgeDrawer = (childGravity == Gravity.LEFT);
            if ((isLeftEdgeDrawer && hasDrawerOnLeftEdge) || (isBottomEdgeDrawer && hasDrawerOnBottomEdge)
                    || (!isBottomEdgeDrawer && !isLeftEdgeDrawer && hasDrawerOnRightEdge)) {
                throw new IllegalStateException("Child drawer has absolute gravity "
                        + gravityToString(isBottomEdgeDrawer ? vchildGravity : childGravity) + " but this "
                        + TAG + " already has a " + "drawer view along that edge");
            }
            if (isBottomEdgeDrawer) {
                hasDrawerOnBottomEdge = true;
            } else if (isLeftEdgeDrawer) {
                hasDrawerOnLeftEdge = true;
            } else {
                hasDrawerOnRightEdge = true;
            }
            // View ???
            int minDrawerMargin = isBottomEdgeDrawer ? 0 : mMinDrawerMargin;
            final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                    minDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
            final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin,
                    lp.height);
            child.measure(drawerWidthSpec, drawerHeightSpec);
        } else {
            throw new IllegalStateException("Child " + child + " at index " + i
                    + " does not have a valid layout_gravity - must be Gravity.LEFT, "
                    + "Gravity.RIGHT, Gravity.BOTTOM or Gravity.NO_GRAVITY");
        }
    }
}

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

/**
 * Lay out a child view with respect to a keyline.
 *
 * <p>The keyline represents a horizontal offset from the unpadded starting edge of
 * the CoordinatorLayout. The child's gravity will affect how it is positioned with
 * respect to the keyline.</p>/*w w  w .j  a va 2 s .  c  o  m*/
 *
 * @param child child to lay out
 * @param keyline offset from the starting edge in pixels of the keyline to align with
 * @param layoutDirection ViewCompat constant for layout direction
 */
private void layoutChildWithKeyline(View child, int keyline, int layoutDirection) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int absGravity = GravityCompat.getAbsoluteGravity(resolveKeylineGravity(lp.gravity), layoutDirection);

    final int hgrav = absGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int vgrav = absGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int width = getWidth();
    final int height = getHeight();
    final int childWidth = child.getMeasuredWidth();
    final int childHeight = child.getMeasuredHeight();

    if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) {
        keyline = width - keyline;
    }

    int left = getKeyline(keyline) - childWidth;
    int top = 0;

    switch (hgrav) {
    default:
    case Gravity.LEFT:
        // Nothing to do.
        break;
    case Gravity.RIGHT:
        left += childWidth;
        break;
    case Gravity.CENTER_HORIZONTAL:
        left += childWidth / 2;
        break;
    }

    switch (vgrav) {
    default:
    case Gravity.TOP:
        // Do nothing, we're already in position.
        break;
    case Gravity.BOTTOM:
        top += childHeight;
        break;
    case Gravity.CENTER_VERTICAL:
        top += childHeight / 2;
        break;
    }

    // Obey margins and padding
    left = Math.max(getPaddingLeft() + lp.leftMargin,
            Math.min(left, width - getPaddingRight() - childWidth - lp.rightMargin));
    top = Math.max(getPaddingTop() + lp.topMargin,
            Math.min(top, height - getPaddingBottom() - childHeight - lp.bottomMargin));

    child.layout(left, top, left + childWidth, top + childHeight);
}

From source file:com.zyk.drawerlayout.widget.DrawerLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.AT_MOST) {
                widthMode = MeasureSpec.EXACTLY;
            } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = MeasureSpec.EXACTLY;
                widthSize = 300;//from   w  ww .  j  a  va  2 s  . c  o  m
            }
            if (heightMode == MeasureSpec.AT_MOST) {
                heightMode = MeasureSpec.EXACTLY;
            } else if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = MeasureSpec.EXACTLY;
                heightSize = 300;
            }
        } else {
            throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }
    }

    setMeasuredDimension(widthSize, heightSize);

    final boolean applyInsets = mLastInsets != null && ViewCompat.getFitsSystemWindows(this);
    final int layoutDirection = ViewCompat.getLayoutDirection(this);

    // Only one drawer is permitted along each vertical edge (left / right). These two booleans
    // are tracking the presence of the edge drawers.
    boolean hasDrawerOnLeftEdge = false;
    boolean hasDrawerOnRightEdge = false;
    boolean hasDrawerOnTopEdge = false;
    boolean hasDrawerOnBottomEdge = false;

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }

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

        if (applyInsets) {
            final int cgrav = GravityCompat.getAbsoluteGravity(lp.gravity, layoutDirection);
            if (ViewCompat.getFitsSystemWindows(child)) {
                IMPL.dispatchChildInsets(child, mLastInsets, cgrav);
            } else {
                IMPL.applyMarginInsets(lp, mLastInsets, cgrav);
            }
        }

        if (isContentView(child)) {
            // Content views get measured at exactly the layout's size.
            final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin,
                    MeasureSpec.EXACTLY);
            final int contentHeightSpec = MeasureSpec
                    .makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
            child.measure(contentWidthSpec, contentHeightSpec);
        } else if (isDrawerView(child)) {
            if (SET_DRAWER_SHADOW_FROM_ELEVATION) {
                if (ViewCompat.getElevation(child) != mDrawerElevation) {
                    ViewCompat.setElevation(child, mDrawerElevation);
                }
            }
            final @EdgeGravity int childGravity = getDrawerViewAbsoluteGravity(child)
                    & Gravity.HORIZONTAL_GRAVITY_MASK;
            // Note that the isDrawerView check guarantees that childGravity here is either
            // LEFT or RIGHT
            boolean isLeftEdgeDrawer = (childGravity == Gravity.LEFT);
            boolean isRightEdgeDrawer = (childGravity == Gravity.RIGHT);
            boolean isTopEdageDrawer = (childGravity == Gravity.TOP);
            boolean isBottomEdageDrawer = (childGravity == Gravity.BOTTOM);
            if ((isLeftEdgeDrawer && hasDrawerOnLeftEdge) || (isRightEdgeDrawer && hasDrawerOnRightEdge)
                    || (isTopEdageDrawer && hasDrawerOnTopEdge)
                    || (isBottomEdageDrawer && hasDrawerOnBottomEdge)) {
                throw new IllegalStateException(
                        "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this "
                                + TAG + " already has a " + "drawer view along that edge");
            }
            if (isLeftEdgeDrawer) {
                hasDrawerOnLeftEdge = true;
            } else if (isRightEdgeDrawer) {
                hasDrawerOnRightEdge = true;
            } else if (isTopEdageDrawer) {
                hasDrawerOnTopEdge = true;
            } else {
                hasDrawerOnBottomEdge = true;
            }

            final int drawerWidthSpec;
            final int drawerHeightSpec;
            if (isLeftEdgeDrawer || isRightEdgeDrawer) {
                drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                        mMinDrawerMarginWidth + lp.leftMargin + lp.rightMargin, lp.width);
                drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin,
                        lp.height);
            } else {
                drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, lp.leftMargin + lp.rightMargin,
                        lp.width);
                drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                        mMinDrawerMarginHeight + lp.topMargin + lp.bottomMargin, lp.height);
            }

            child.measure(drawerWidthSpec, drawerHeightSpec);
        } else {
            throw new IllegalStateException("Child " + child + " at index " + i
                    + " does not have a valid layout_gravity - must be Gravity.LEFT, "
                    + "Gravity.RIGHT ,Gravity.TOP, Gravity.BOTTOM, or Gravity.NO_GRAVITY");
        }
    }
}