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.sanron.sunweather.view.v4.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;// ww  w.  ja v  a  2s .  co  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);

    // Gravity value for each drawer we've seen. Only one of each permitted.
    int foundDrawers = 0;
    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)) {
            final int childGravity = getDrawerViewAbsoluteGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
            if ((foundDrawers & childGravity) != 0) {
                throw new IllegalStateException(
                        "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this "
                                + TAG + " already has a " + "drawer view along that edge");
            }
            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:org.gateshipone.malp.application.activities.MainActivity.java

@Override
public void openPath(String path) {
    // Create fragment and give it an argument for the selected directory
    FilesFragment newFragment = new FilesFragment();
    Bundle args = new Bundle();
    args.putString(FilesFragment.EXTRA_FILENAME, path);

    newFragment.setArguments(args);/*www.j a  va2 s . c  o  m*/

    FragmentManager fragmentManager = getSupportFragmentManager();

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

    newFragment.setEnterTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.START,
            getResources().getConfiguration().getLayoutDirection())));
    newFragment.setExitTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.END,
            getResources().getConfiguration().getLayoutDirection())));

    transaction.addToBackStack("FilesFragment" + path);
    transaction.replace(R.id.fragment_container, newFragment);

    // Commit the transaction
    transaction.commit();

}

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

@Override
public void onPlaylistSelected(String playlistTitle, long playlistID) {
    // Create fragment and give it an argument for the selected playlist
    PlaylistTracksFragment newFragment = new PlaylistTracksFragment();
    Bundle args = new Bundle();
    args.putString(PlaylistTracksFragment.ARG_PLAYLISTTITLE, playlistTitle);
    args.putLong(PlaylistTracksFragment.ARG_PLAYLISTID, playlistID);

    newFragment.setArguments(args);//from  www.  j  a va  2 s.com

    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().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())));

    // Replace whatever is in the fragment_container view with this
    // fragment,
    // and add the transaction to the back stack so the user can navigate
    // back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack("PlaylistTracksFragment");

    // Commit the transaction
    transaction.commit();
}

From source file:org.gateshipone.malp.application.activities.MainActivity.java

@Override
public void showAlbumsForPath(String path) {
    if (mNowPlayingDragStatus == DRAG_STATUS.DRAGGED_UP) {
        NowPlayingView nowPlayingView = (NowPlayingView) findViewById(R.id.now_playing_layout);
        if (nowPlayingView != null) {
            View coordinatorLayout = findViewById(R.id.main_coordinator_layout);
            coordinatorLayout.setVisibility(View.VISIBLE);
            nowPlayingView.minimize();/*from   ww  w.  j a v  a 2 s  . c  o  m*/
        }
    }
    // Create fragment and give it an argument for the selected article
    AlbumsFragment newFragment = new AlbumsFragment();
    Bundle args = new Bundle();
    args.putString(AlbumsFragment.BUNDLE_STRING_EXTRA_PATH, path);

    newFragment.setArguments(args);

    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    newFragment.setEnterTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.START,
            getResources().getConfiguration().getLayoutDirection())));
    newFragment.setExitTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.END,
            getResources().getConfiguration().getLayoutDirection())));
    // Replace whatever is in the fragment_container view with this
    // fragment,
    // and add the transaction to the back stack so the user can navigate
    // back
    transaction.replace(R.id.fragment_container, newFragment, AlbumsFragment.TAG);
    transaction.addToBackStack("DirectoryAlbumsFragment");

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setCheckedItem(R.id.nav_library);

    // Commit the transaction
    transaction.commit();
}

From source file:org.gateshipone.malp.application.activities.MainActivity.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();

    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 a2  s  . c om*/
}

From source file:com.android.llibs.views.bottomnavigation.CoordinatorLayout.java

/**
 * Calculate the desired child rect relative to an anchor rect, respecting both
 * gravity and anchorGravity./* w  ww. j  av  a 2s.c o m*/
 *
 * @param child child view to calculate a rect for
 * @param layoutDirection the desired layout direction for the CoordinatorLayout
 * @param anchorRect rect in CoordinatorLayout coordinates of the anchor view area
 * @param out rect to set to the output values
 */
void getDesiredAnchoredChildRect(View child, int layoutDirection, Rect anchorRect, Rect out) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int absGravity = GravityCompat.getAbsoluteGravity(resolveAnchoredChildGravity(lp.gravity),
            layoutDirection);
    final int absAnchorGravity = GravityCompat.getAbsoluteGravity(resolveGravity(lp.anchorGravity),
            layoutDirection);

    final int hgrav = absGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int vgrav = absGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int anchorHgrav = absAnchorGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int anchorVgrav = absAnchorGravity & Gravity.VERTICAL_GRAVITY_MASK;

    final int childWidth = child.getMeasuredWidth();
    final int childHeight = child.getMeasuredHeight();

    int left;
    int top;

    // Align to the anchor. This puts us in an assumed right/bottom child view gravity.
    // If this is not the case we will subtract out the appropriate portion of
    // the child size below.
    switch (anchorHgrav) {
    default:
    case Gravity.LEFT:
        left = anchorRect.left;
        break;
    case Gravity.RIGHT:
        left = anchorRect.right;
        break;
    case Gravity.CENTER_HORIZONTAL:
        left = anchorRect.left + anchorRect.width() / 2;
        break;
    }

    switch (anchorVgrav) {
    default:
    case Gravity.TOP:
        top = anchorRect.top;
        break;
    case Gravity.BOTTOM:
        top = anchorRect.bottom;
        break;
    case Gravity.CENTER_VERTICAL:
        top = anchorRect.top + anchorRect.height() / 2;
        break;
    }

    // Offset by the child view's gravity itself. The above assumed right/bottom gravity.
    switch (hgrav) {
    default:
    case Gravity.LEFT:
        left -= childWidth;
        break;
    case Gravity.RIGHT:
        // Do nothing, we're already in position.
        break;
    case Gravity.CENTER_HORIZONTAL:
        left -= childWidth / 2;
        break;
    }

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

    final int width = getWidth();
    final int height = getHeight();

    // 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));

    out.set(left, top, left + childWidth, top + childHeight);
}

From source file:maximsblog.blogspot.com.formuladict.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;// ww w . 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);

    // Gravity value for each drawer we've seen. Only one of each permitted.
    int foundDrawers = 0;
    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 int childGravity = getDrawerViewAbsoluteGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
            if ((foundDrawers & childGravity) != 0) {
                throw new IllegalStateException(
                        "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this "
                                + TAG + " already has a " + "drawer view along that edge");
            }
            foundDrawers = foundDrawers | childGravity;
            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:android.support.design.widget.CoordinatorLayout.java

private void getDesiredAnchoredChildRectWithoutConstraints(View child, int layoutDirection, Rect anchorRect,
        Rect out, LayoutParams lp, int childWidth, int childHeight) {
    final int absGravity = GravityCompat.getAbsoluteGravity(resolveAnchoredChildGravity(lp.gravity),
            layoutDirection);/*from  www.  java  2  s .  c o m*/
    final int absAnchorGravity = GravityCompat.getAbsoluteGravity(resolveGravity(lp.anchorGravity),
            layoutDirection);

    final int hgrav = absGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int vgrav = absGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int anchorHgrav = absAnchorGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int anchorVgrav = absAnchorGravity & Gravity.VERTICAL_GRAVITY_MASK;

    int left;
    int top;

    // Align to the anchor. This puts us in an assumed right/bottom child view gravity.
    // If this is not the case we will subtract out the appropriate portion of
    // the child size below.
    switch (anchorHgrav) {
    default:
    case Gravity.LEFT:
        left = anchorRect.left;
        break;
    case Gravity.RIGHT:
        left = anchorRect.right;
        break;
    case Gravity.CENTER_HORIZONTAL:
        left = anchorRect.left + anchorRect.width() / 2;
        break;
    }

    switch (anchorVgrav) {
    default:
    case Gravity.TOP:
        top = anchorRect.top;
        break;
    case Gravity.BOTTOM:
        top = anchorRect.bottom;
        break;
    case Gravity.CENTER_VERTICAL:
        top = anchorRect.top + anchorRect.height() / 2;
        break;
    }

    // Offset by the child view's gravity itself. The above assumed right/bottom gravity.
    switch (hgrav) {
    default:
    case Gravity.LEFT:
        left -= childWidth;
        break;
    case Gravity.RIGHT:
        // Do nothing, we're already in position.
        break;
    case Gravity.CENTER_HORIZONTAL:
        left -= childWidth / 2;
        break;
    }

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

    out.set(left, top, left + childWidth, top + childHeight);
}

From source file:com.xiaosu.lib.base.widget.drawerLayout.DrawerLayout.java

boolean isDrawerView(View child) {
    final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
    final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(child));

    return (absGravity & Gravity.BOTTOM) != 0;
}

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

/**
 * Open 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 ww .j a va  2s .  co m*/
public void openDrawer(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));
    }
    openDrawer(drawerView);
}