Java Code Examples for android.view.View.getBaseline()

The following are Jave code examples for showing how to use getBaseline() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: CSipSimple   File: IcsSpinner.java   View Source Code Vote up 7 votes
@Override
public int getBaseline() {
    View child = null;

    if (getChildCount() > 0) {
        child = getChildAt(0);
    } 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;
    }
}
 
Example 2
Project: MiPushFramework   File: BaselineLayout.java   View Source Code Vote up 6 votes
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;
        }

        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) {
        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));
}
 
Example 3
Project: MiPushFramework   File: BaselineLayout.java   View Source Code Vote up 6 votes
@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;
        }

        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);
    }
}
 
Example 4
Project: boohee_v5.6   File: LinearLayoutCompat.java   View Source Code Vote up 6 votes
public int getBaseline() {
    if (this.mBaselineAlignedChildIndex < 0) {
        return super.getBaseline();
    }
    if (getChildCount() <= this.mBaselineAlignedChildIndex) {
        throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout set to an index that is out of bounds.");
    }
    View child = getChildAt(this.mBaselineAlignedChildIndex);
    int childBaseline = child.getBaseline();
    if (childBaseline != -1) {
        int childTop = this.mBaselineChildTop;
        if (this.mOrientation == 1) {
            int majorGravity = this.mGravity & 112;
            if (majorGravity != 48) {
                switch (majorGravity) {
                    case 16:
                        childTop += ((((getBottom() - getTop()) - getPaddingTop()) - getPaddingBottom()) - this.mTotalLength) / 2;
                        break;
                    case 80:
                        childTop = ((getBottom() - getTop()) - getPaddingBottom()) - this.mTotalLength;
                        break;
                }
            }
        }
        return (((LayoutParams) child.getLayoutParams()).topMargin + childTop) + childBaseline;
    } else if (this.mBaselineAlignedChildIndex == 0) {
        return -1;
    } else {
        throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout points to a View that doesn't know how to get its baseline.");
    }
}
 
Example 5
Project: JD-Test   File: FlexboxLayout.java   View Source Code Vote up 5 votes
/**
 * 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
 * @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()
                    + lp.topMargin - lp.bottomMargin) / 2;
            if (flexWrap != FLEX_WRAP_WRAP_REVERSE) {
                view.layout(left, top + topFromCrossAxis,
                        right, top + topFromCrossAxis + view.getMeasuredHeight());
            } else {
                view.layout(left, top - topFromCrossAxis,
                        right, top - topFromCrossAxis + view.getMeasuredHeight());
            }
            break;
    }
}