Example usage for android.view View getBaseline

List of usage examples for android.view View getBaseline

Introduction

In this page you can find the example usage for android.view View getBaseline.

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public int getBaseline() 

Source Link

Document

Return the offset of the widget's text baseline from the widget's top boundary.

Usage

From source file:com.umeitime.common.views.pagebottom.internal.BaselineLayout.java

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int count = getChildCount();
    int maxWidth = 0;
    int maxHeight = 0;
    int maxChildBaseline = -1;
    int maxChildDescent = -1;
    int childState = 0;

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == GONE) {
            continue;
        }/*from  w  ww  . ja  v a 2  s.  c om*/

        measureChild(child, widthMeasureSpec, heightMeasureSpec);
        final int baseline = child.getBaseline();
        if (baseline != -1) {
            maxChildBaseline = Math.max(maxChildBaseline, baseline);
            maxChildDescent = Math.max(maxChildDescent, child.getMeasuredHeight() - baseline);
        }
        maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
        maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
        childState = childState | ViewCompat.getMeasuredState(child);
    }
    if (maxChildBaseline != -1) {
        maxChildDescent = Math.max(maxChildDescent, getPaddingBottom());
        maxHeight = Math.max(maxHeight, maxChildBaseline + maxChildDescent);
        mBaseline = maxChildBaseline;
    }
    maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
    maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
    setMeasuredDimension(ViewCompat.resolveSizeAndState(maxWidth, widthMeasureSpec, childState), ViewCompat
            .resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT));
}

From source file:android.support.design.internal.BaselineLayout.java

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int count = getChildCount();
    int maxWidth = 0;
    int maxHeight = 0;
    int maxChildBaseline = -1;
    int maxChildDescent = -1;
    int childState = 0;

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == GONE) {
            continue;
        }//from  w  w  w . j  a  v  a  2 s .  com

        measureChild(child, widthMeasureSpec, heightMeasureSpec);
        final int baseline = child.getBaseline();
        if (baseline != -1) {
            maxChildBaseline = Math.max(maxChildBaseline, baseline);
            maxChildDescent = Math.max(maxChildDescent, child.getMeasuredHeight() - baseline);
        }
        maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
        maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
        childState = ViewUtils.combineMeasuredStates(childState, ViewCompat.getMeasuredState(child));
    }
    if (maxChildBaseline != -1) {
        maxChildDescent = Math.max(maxChildDescent, getPaddingBottom());
        maxHeight = Math.max(maxHeight, maxChildBaseline + maxChildDescent);
        mBaseline = maxChildBaseline;
    }
    maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
    maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
    setMeasuredDimension(ViewCompat.resolveSizeAndState(maxWidth, widthMeasureSpec, childState), ViewCompat
            .resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT));
}

From source file:android.support.design.internal.BaselineLayout.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int count = getChildCount();
    final int parentLeft = getPaddingLeft();
    final int parentRight = right - left - getPaddingRight();
    final int parentContentWidth = parentRight - parentLeft;
    final int parentTop = getPaddingTop();

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == GONE) {
            continue;
        }// w  w w. j a  v  a 2 s .c  o m

        final int width = child.getMeasuredWidth();
        final int height = child.getMeasuredHeight();

        final int childLeft = parentLeft + (parentContentWidth - width) / 2;
        final int childTop;
        if (mBaseline != -1 && child.getBaseline() != -1) {
            childTop = parentTop + mBaseline - child.getBaseline();
        } else {
            childTop = parentTop;
        }

        child.layout(childLeft, childTop, childLeft + width, childTop + height);
    }
}

From source file:com.actionbarsherlock.internal.widget.IcsSpinner.java

@Override
public int getBaseline() {
    View child = null;

    if (getChildCount() > 0) {
        child = getChildAt(0);/*from   w w w.  j  a v  a 2s. co  m*/
    } else if (mAdapter != null && mAdapter.getCount() > 0) {
        child = makeAndAddView(0);
        mRecycler.put(0, child);
        removeAllViewsInLayout();
    }

    if (child != null) {
        final int childBaseline = child.getBaseline();
        return childBaseline >= 0 ? child.getTop() + childBaseline : -1;
    } else {
        return -1;
    }
}

From source file:android.support.v7.internal.widget.SpinnerCompat.java

@Override
public int getBaseline() {
    View child = null;

    if (getChildCount() > 0) {
        child = getChildAt(0);/*from w w w.j a va  2s.  c  o m*/
    } else if (mAdapter != null && mAdapter.getCount() > 0) {
        child = makeView(0, false);
        mRecycler.put(0, child);
    }

    if (child != null) {
        final int childBaseline = child.getBaseline();
        return childBaseline >= 0 ? child.getTop() + childBaseline : -1;
    } else {
        return -1;
    }
}

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

@Override
public int getBaseline() {
    if (mBaselineAlignedChildIndex < 0) {
        return super.getBaseline();
    }/*  ww  w  .java  2 s . co  m*/

    if (getChildCount() <= mBaselineAlignedChildIndex) {
        throw new RuntimeException(
                "mBaselineAlignedChildIndex of LinearLayout " + "set to an index that is out of bounds.");
    }

    final View child = getChildAt(mBaselineAlignedChildIndex);
    final int childBaseline = child.getBaseline();

    if (childBaseline == -1) {
        if (mBaselineAlignedChildIndex == 0) {
            // this is just the default case, safe to return -1
            return -1;
        }
        // the user picked an index that points to something that doesn't
        // know how to calculate its baseline.
        throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout "
                + "points to a View that doesn't know how to get its baseline.");
    }

    // TODO: This should try to take into account the virtual offsets
    // (See getNextLocationOffset and getLocationOffset)
    // We should add to childTop:
    // sum([getNextLocationOffset(getChildAt(i)) / i < mBaselineAlignedChildIndex])
    // and also add:
    // getLocationOffset(child)
    int childTop = mBaselineChildTop;

    if (mOrientation == VERTICAL) {
        final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
        if (majorGravity != Gravity.TOP) {
            switch (majorGravity) {
            case Gravity.BOTTOM:
                childTop = getBottom() - getTop() - getPaddingBottom() - mTotalLength;
                break;

            case Gravity.CENTER_VERTICAL:
                childTop += ((getBottom() - getTop() - getPaddingTop() - getPaddingBottom()) - mTotalLength)
                        / 2;
                break;
            }
        }
    }

    LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child.getLayoutParams();
    return childTop + lp.topMargin + childBaseline;
}

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  .  j a va2  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.google.android.flexbox.FlexboxHelper.java

/**
 * Place a single View when the layout direction is horizontal
 * ({@link FlexContainer#getFlexDirection()} is either {@link FlexDirection#ROW} or
 * {@link FlexDirection#ROW_REVERSE})./*w ww .j  a v a2 s  . co  m*/
 *
 * @param view     the View to be placed
 * @param flexLine the {@link FlexLine} where the View belongs to
 * @param left     the left position of the View, which the View's margin is already taken
 *                 into account
 * @param top      the top position of the flex line where the View belongs to. The actual
 *                 View's top position is shifted depending on the flexWrap and alignItems
 *                 attributes
 * @param right    the right position of the View, which the View's margin is already taken
 *                 into account
 * @param bottom   the bottom position of the flex line where the View belongs to. The actual
 *                 View's bottom position is shifted depending on the flexWrap and alignItems
 *                 attributes
 * @see FlexContainer#getAlignItems()
 * @see FlexContainer#setAlignItems(int)
 * @see FlexItem#getAlignSelf()
 */
void layoutSingleChildHorizontal(View view, FlexLine flexLine, int left, int top, int right, int bottom) {
    FlexItem flexItem = (FlexItem) view.getLayoutParams();
    int alignItems = mFlexContainer.getAlignItems();
    if (flexItem.getAlignSelf() != AlignSelf.AUTO) {
        // Expecting the values for alignItems and mAlignSelf match except for ALIGN_SELF_AUTO.
        // Assigning the mAlignSelf value as alignItems should work.
        alignItems = flexItem.getAlignSelf();
    }
    int crossSize = flexLine.mCrossSize;
    switch (alignItems) {
    case AlignItems.FLEX_START: // Intentional fall through
    case AlignItems.STRETCH:
        if (mFlexContainer.getFlexWrap() != FlexWrap.WRAP_REVERSE) {
            view.layout(left, top + flexItem.getMarginTop(), right, bottom + flexItem.getMarginTop());
        } else {
            view.layout(left, top - flexItem.getMarginBottom(), right, bottom - flexItem.getMarginBottom());
        }
        break;
    case AlignItems.BASELINE:
        if (mFlexContainer.getFlexWrap() != FlexWrap.WRAP_REVERSE) {
            int marginTop = flexLine.mMaxBaseline - view.getBaseline();
            marginTop = Math.max(marginTop, flexItem.getMarginTop());
            view.layout(left, top + marginTop, right, bottom + marginTop);
        } else {
            int marginBottom = flexLine.mMaxBaseline - view.getMeasuredHeight() + view.getBaseline();
            marginBottom = Math.max(marginBottom, flexItem.getMarginBottom());
            view.layout(left, top - marginBottom, right, bottom - marginBottom);
        }
        break;
    case AlignItems.FLEX_END:
        if (mFlexContainer.getFlexWrap() != FlexWrap.WRAP_REVERSE) {
            view.layout(left, top + crossSize - view.getMeasuredHeight() - flexItem.getMarginBottom(), right,
                    top + crossSize - flexItem.getMarginBottom());
        } else {
            // If the flexWrap == WRAP_REVERSE, the direction of the
            // flexEnd is flipped (from top to bottom).
            view.layout(left, top - crossSize + view.getMeasuredHeight() + flexItem.getMarginTop(), right,
                    bottom - crossSize + view.getMeasuredHeight() + flexItem.getMarginTop());
        }
        break;
    case AlignItems.CENTER:
        int topFromCrossAxis = (crossSize - view.getMeasuredHeight() + flexItem.getMarginTop()
                - flexItem.getMarginBottom()) / 2;
        if (mFlexContainer.getFlexWrap() != FlexWrap.WRAP_REVERSE) {
            view.layout(left, top + topFromCrossAxis, right, top + topFromCrossAxis + view.getMeasuredHeight());
        } else {
            view.layout(left, top - topFromCrossAxis, right, top - topFromCrossAxis + view.getMeasuredHeight());
        }
        break;
    }
}

From source file:com.devabit.takestock.ui.widget.FlexboxLayout.java

/**
 * Place a single View when the layout direction is horizontal ({@link #mFlexDirection} is
 * either {@link #FLEX_DIRECTION_ROW} or {@link #FLEX_DIRECTION_ROW_REVERSE}).
 *
 * @param view       the View to be placed
 * @param flexLine   the {@link FlexLine} where the View belongs to
 * @param flexWrap   the flex wrap attribute of this FlexboxLayout
 * @param alignItems the align items attribute of this FlexboxLayout
 * @param left       the left position of the View, which the View's margin is already taken
 *                   into account//from   www  .j  ava 2 s .  c o  m
 * @param top        the top position of the flex line where the View belongs to. The actual
 *                   View's top position is shifted depending on the flexWrap and alignItems
 *                   attributes
 * @param right      the right position of the View, which the View's margin is already taken
 *                   into account
 * @param bottom     the bottom position of the flex line where the View belongs to. The actual
 *                   View's bottom position is shifted depending on the flexWrap and alignItems
 *                   attributes
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutSingleChildHorizontal(View view, FlexLine flexLine, @FlexWrap int flexWrap, int alignItems,
        int left, int top, int right, int bottom) {
    LayoutParams lp = (LayoutParams) view.getLayoutParams();
    if (lp.alignSelf != LayoutParams.ALIGN_SELF_AUTO) {
        // Expecting the values for alignItems and alignSelf match except for ALIGN_SELF_AUTO.
        // Assigning the alignSelf value as alignItems should work.
        alignItems = lp.alignSelf;
    }
    int crossSize = flexLine.crossSize;
    switch (alignItems) {
    case ALIGN_ITEMS_FLEX_START: // Intentional fall through
    case ALIGN_ITEMS_STRETCH:
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            view.layout(left, top + lp.topMargin, right, bottom + lp.topMargin);
        } else {
            view.layout(left, top - lp.bottomMargin, right, bottom - lp.bottomMargin);
        }
        break;
    case ALIGN_ITEMS_BASELINE:
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            int marginTop = flexLine.maxBaseline - view.getBaseline();
            marginTop = Math.max(marginTop, lp.topMargin);
            view.layout(left, top + marginTop, right, bottom + marginTop);
        } else {
            int marginBottom = flexLine.maxBaseline - view.getMeasuredHeight() + view.getBaseline();
            marginBottom = Math.max(marginBottom, lp.bottomMargin);
            view.layout(left, top - marginBottom, right, bottom - marginBottom);
        }
        break;
    case ALIGN_ITEMS_FLEX_END:
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            view.layout(left, top + crossSize - view.getMeasuredHeight() - lp.bottomMargin, right,
                    top + crossSize - lp.bottomMargin);
        } else {
            // If the flexWrap == FLEX_WRAP_WRAP_REVERSE, the direction of the
            // flexEnd is flipped (from top to bottom).
            view.layout(left, top - crossSize + view.getMeasuredHeight() + lp.topMargin, right,
                    bottom - crossSize + view.getMeasuredHeight() + lp.topMargin);
        }
        break;
    case ALIGN_ITEMS_CENTER:
        int topFromCrossAxis = (crossSize - view.getMeasuredHeight()) / 2;
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            view.layout(left, top + topFromCrossAxis + lp.topMargin - lp.bottomMargin, right,
                    top + topFromCrossAxis + view.getMeasuredHeight() + lp.topMargin - lp.bottomMargin);
        } else {
            view.layout(left, top - topFromCrossAxis + lp.topMargin - lp.bottomMargin, right,
                    top - topFromCrossAxis + view.getMeasuredHeight() + lp.topMargin - lp.bottomMargin);
        }
        break;
    }
}

From source file:com.google.android.flexbox.FlexboxLayout.java

/**
 * Place a single View when the layout direction is horizontal ({@link #mFlexDirection} is
 * either {@link #FLEX_DIRECTION_ROW} or {@link #FLEX_DIRECTION_ROW_REVERSE}).
 *
 * @param view       the View to be placed
 * @param flexLine   the {@link FlexLine} where the View belongs to
 * @param flexWrap   the flex wrap attribute of this FlexboxLayout
 * @param alignItems the align items attribute of this FlexboxLayout
 * @param left       the left position of the View, which the View's margin is already taken
 *                   into account//from  w  ww  .  jav a  2s . c  om
 * @param top        the top position of the flex line where the View belongs to. The actual
 *                   View's top position is shifted depending on the flexWrap and alignItems
 *                   attributes
 * @param right      the right position of the View, which the View's margin is already taken
 *                   into account
 * @param bottom     the bottom position of the flex line where the View belongs to. The actual
 *                   View's bottom position is shifted depending on the flexWrap and alignItems
 *                   attributes
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutSingleChildHorizontal(View view, FlexLine flexLine, @FlexWrap int flexWrap, int alignItems,
        int left, int top, int right, int bottom) {
    LayoutParams lp = (LayoutParams) view.getLayoutParams();
    if (lp.alignSelf != LayoutParams.ALIGN_SELF_AUTO) {
        // Expecting the values for alignItems and alignSelf match except for ALIGN_SELF_AUTO.
        // Assigning the alignSelf value as alignItems should work.
        alignItems = lp.alignSelf;
    }
    int crossSize = flexLine.mCrossSize;
    switch (alignItems) {
    case ALIGN_ITEMS_FLEX_START: // Intentional fall through
    case ALIGN_ITEMS_STRETCH:
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            view.layout(left, top + lp.topMargin, right, bottom + lp.topMargin);
        } else {
            view.layout(left, top - lp.bottomMargin, right, bottom - lp.bottomMargin);
        }
        break;
    case ALIGN_ITEMS_BASELINE:
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            int marginTop = flexLine.mMaxBaseline - view.getBaseline();
            marginTop = Math.max(marginTop, lp.topMargin);
            view.layout(left, top + marginTop, right, bottom + marginTop);
        } else {
            int marginBottom = flexLine.mMaxBaseline - view.getMeasuredHeight() + view.getBaseline();
            marginBottom = Math.max(marginBottom, lp.bottomMargin);
            view.layout(left, top - marginBottom, right, bottom - marginBottom);
        }
        break;
    case ALIGN_ITEMS_FLEX_END:
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            view.layout(left, top + crossSize - view.getMeasuredHeight() - lp.bottomMargin, right,
                    top + crossSize - lp.bottomMargin);
        } else {
            // If the flexWrap == FLEX_WRAP_WRAP_REVERSE, the direction of the
            // flexEnd is flipped (from top to bottom).
            view.layout(left, top - crossSize + view.getMeasuredHeight() + lp.topMargin, right,
                    bottom - crossSize + view.getMeasuredHeight() + lp.topMargin);
        }
        break;
    case ALIGN_ITEMS_CENTER:
        int topFromCrossAxis = (crossSize - view.getMeasuredHeight()) / 2;
        if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
            view.layout(left, top + topFromCrossAxis + lp.topMargin - lp.bottomMargin, right,
                    top + topFromCrossAxis + view.getMeasuredHeight() + lp.topMargin - lp.bottomMargin);
        } else {
            view.layout(left, top - topFromCrossAxis + lp.topMargin - lp.bottomMargin, right,
                    top - topFromCrossAxis + view.getMeasuredHeight() + lp.topMargin - lp.bottomMargin);
        }
        break;
    }
}