Example usage for android.view View getMeasuredHeight

List of usage examples for android.view View getMeasuredHeight

Introduction

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

Prototype

public final int getMeasuredHeight() 

Source Link

Document

Like #getMeasuredHeightAndState() , but only returns the raw height component (that is the result is masked by #MEASURED_SIZE_MASK ).

Usage

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredWidth = getMeasuredWidth();
    final int measuredHeight = getMeasuredHeight();
    final int maxGutterSize = (isOrientationHorizontal() ? measuredWidth : measuredHeight) / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
    int childHeightSize = measuredHeight - getPaddingTop() - getPaddingBottom();

    /*/*from  w ww .j a v  a2 s.  co m*/
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.MATCH_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.MATCH_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                if (isOrientationHorizontal()) {
                    final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.sizeFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(widthSpec, mChildHeightMeasureSpec);
                } else {
                    final int heightSpec = MeasureSpec.makeMeasureSpec((int) (childHeightSize * lp.sizeFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(mChildWidthMeasureSpec, heightSpec);
                }
            }
        }
    }
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

/**
 * Should be called with mPopulating set to true
 * /*from w  w w . j av  a2  s .  c o m*/
 * @param fromPosition
 *            Position to start filling from
 * @param overhang
 *            the number of extra pixels to fill beyond the current bottom
 *            edge
 * @return the max overhang beyond the end of the view of any added items at
 *         the bottom
 */
final int fillDown(int fromPosition, int overhang) {

    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    // final int itemMargin = mItemMargin;
    final int leftMargin = mItemLeftMargin;
    final int rightMargin = mItemRightMargin;
    final int topMargin = mItemTopMargin;
    final int bottomMargin = mItemBottomMargin;

    final int colWidth = (getWidth() - paddingLeft - paddingRight
            - (leftMargin + rightMargin) * (mColCount - 1)) / mColCount;
    final int gridBottom = getHeight() - getPaddingBottom();
    final int fillTo = gridBottom + overhang;
    int nextCol = getNextColumnDown(fromPosition);
    int position = fromPosition;

    while (nextCol >= 0 && mItemBottoms[nextCol] < fillTo && position < mItemCount) {

        final View child = obtainView(position, null);

        if (child == null)
            continue;

        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp == null) {
            lp = this.generateDefaultLayoutParams();
            child.setLayoutParams(lp);
        }
        if (child.getParent() != this) {
            if (mInLayout) {
                addViewInLayout(child, -1, lp);
            } else {
                addView(child);
            }
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + (leftMargin + rightMargin) * (span - 1);
        final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

        LayoutRecord rec;
        if (span > 1) {
            rec = getNextRecordDown(position, span);
            // nextCol = rec.column;
        } else {
            rec = mLayoutRecords.get(position);
        }

        boolean invalidateAfter = false;
        if (rec == null) {
            rec = new LayoutRecord();
            mLayoutRecords.put(position, rec);
            rec.column = nextCol;
            rec.span = span;
        } else if (span != rec.span) {
            rec.span = span;
            rec.column = nextCol;
            invalidateAfter = true;
        } else {
            // nextCol = rec.column;
        }

        if (mHasStableIds) {
            final long id = mAdapter.getItemId(position);
            rec.id = id;
            lp.id = id;
        }

        lp.column = nextCol;

        final int heightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }
        child.measure(widthSpec, heightSpec);

        final int childHeight = child.getMeasuredHeight();
        if (invalidateAfter || (childHeight != rec.height && rec.height > 0)) {
            invalidateLayoutRecordsAfterPosition(position);
        }
        rec.height = childHeight;

        final int startFrom;
        if (span > 1) {
            int lowest = mItemBottoms[nextCol];
            for (int i = nextCol + 1; i < nextCol + span; i++) {
                final int bottom = mItemBottoms[i];
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            startFrom = lowest;
        } else {
            startFrom = mItemBottoms[nextCol];
        }

        final int childTop = startFrom + topMargin;
        final int childBottom = childTop + childHeight - bottomMargin;
        final int childLeft = paddingLeft + nextCol * (colWidth + leftMargin);
        final int childRight = childLeft + child.getMeasuredWidth() - rightMargin;
        child.layout(childLeft, childTop, childRight, childBottom);

        // add the position to the mapping
        if (!mColMappings.get(nextCol).contains(position)) {

            // check to see if the mapping exists in other columns
            // this would happen if list has been updated
            for (ArrayList<Integer> list : mColMappings) {
                if (list.contains(position)) {
                    list.remove((Integer) position);
                }
            }

            mColMappings.get(nextCol).add(position);

        }

        for (int i = nextCol; i < nextCol + span; i++) {
            mItemBottoms[i] = childBottom + rec.getMarginBelow(i - nextCol);
        }

        position++;
        nextCol = getNextColumnDown(position);
    }

    int lowestView = 0;
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] > lowestView) {
            lowestView = mItemBottoms[i];
        }
    }
    return lowestView - gridBottom;
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

/**
 * Should be called with mPopulating set to true
 * //w  w w.ja v  a2 s. c o m
 * @param fromPosition
 *            Position to start filling from
 * @param overhang
 *            the number of extra pixels to fill beyond the current top edge
 * @return the max overhang beyond the beginning of the view of any added
 *         items at the top
 */
final int fillUp(int fromPosition, int overhang) {

    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    // final int itemMargin = mItemMargin;
    final int leftMargin = mItemLeftMargin;
    final int rightMargin = mItemRightMargin;
    final int topMargin = mItemTopMargin;
    final int bottomMargin = mItemBottomMargin;

    final int colWidth = (getWidth() - paddingLeft - paddingRight
            - (leftMargin + rightMargin) * (mColCount - 1)) / mColCount;
    mColWidth = colWidth;
    final int gridTop = getPaddingTop();
    final int fillTo = gridTop - overhang;
    int nextCol = getNextColumnUp();
    int position = fromPosition;

    while (nextCol >= 0 && mItemTops[nextCol] > fillTo && position >= 0) {
        // make sure the nextCol is correct. check to see if has been mapped
        // otherwise stick to getNextColumnUp()
        if (!mColMappings.get(nextCol).contains((Integer) position)) {
            for (int i = 0; i < mColMappings.size(); i++) {
                if (mColMappings.get(i).contains((Integer) position)) {
                    nextCol = i;
                    break;
                }
            }
        }

        // displayMapping();

        final View child = obtainView(position, null);

        if (child == null)
            continue;

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

        if (lp == null) {
            lp = this.generateDefaultLayoutParams();
            child.setLayoutParams(lp);
        }

        if (child.getParent() != this) {
            if (mInLayout) {
                addViewInLayout(child, 0, lp);
            } else {
                addView(child, 0);
            }
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + (leftMargin + rightMargin) * (span - 1);
        final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

        LayoutRecord rec;
        if (span > 1) {
            rec = getNextRecordUp(position, span);
            // nextCol = rec.column;
        } else {
            rec = mLayoutRecords.get(position);
        }

        boolean invalidateBefore = false;
        if (rec == null) {
            rec = new LayoutRecord();
            mLayoutRecords.put(position, rec);
            rec.column = nextCol;
            rec.span = span;
        } else if (span != rec.span) {
            rec.span = span;
            rec.column = nextCol;
            invalidateBefore = true;
        } else {
            // nextCol = rec.column;
        }

        if (mHasStableIds) {
            final long id = mAdapter.getItemId(position);
            rec.id = id;
            lp.id = id;
        }

        lp.column = nextCol;

        final int heightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }
        child.measure(widthSpec, heightSpec);

        final int childHeight = child.getMeasuredHeight();
        if (invalidateBefore || (childHeight != rec.height && rec.height > 0)) {
            invalidateLayoutRecordsBeforePosition(position);
        }
        rec.height = childHeight;

        // int itemTop = mItemTops[nextCol];

        final int startFrom;
        if (span > 1) {
            int highest = mItemTops[nextCol];
            for (int i = nextCol + 1; i < nextCol + span; i++) {
                final int top = mItemTops[i];
                if (top < highest) {
                    highest = top;
                }
            }
            startFrom = highest;
        } else {
            startFrom = mItemTops[nextCol];
        }

        int childBottom = startFrom;
        int childTop = childBottom - childHeight;
        final int childLeft = paddingLeft + nextCol * (colWidth + (leftMargin + rightMargin));
        final int childRight = childLeft + child.getMeasuredWidth();

        // if(position == 0){
        // if(this.getChildCount()>1 && this.mColCount>1){
        // childTop = this.getChildAt(1).getTop();
        // childBottom = childTop + childHeight;
        // }
        // }

        child.layout(childLeft, childTop, childRight, childBottom);

        for (int i = nextCol; i < nextCol + span; i++) {
            mItemTops[i] = childTop - rec.getMarginAbove(i - nextCol) - (topMargin + bottomMargin);
        }

        nextCol = getNextColumnUp();
        mFirstPosition = position--;
    }

    int highestView = getHeight();

    for (int i = 0; i < mColCount; i++) {
        final View child = getFirstChildAtColumn(i);
        if (child == null) {
            highestView = 0;
            break;
        }
        final int top = child.getTop();

        if (top < highestView) {
            highestView = top;
        }
    }

    return gridTop - highestView;
}

From source file:android.improving.utils.views.cardsview.OrientedViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, our internal size is always 0.
    // We depend on the container to specify the layout size of
    // our view.  We can't really know what it is since we will be
    // adding and removing different arbitrary views and do not
    // want the layout to change as this happens.
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    final int measuredSize = (mOrientation == Orientation.VERTICAL) ? getMeasuredHeight() : getMeasuredWidth();
    final int maxGutterSize = measuredSize / 10;
    mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);

    // Children are just made to fill our space.
    int childWidthSize;
    int childHeightSize;

    if (mOrientation == Orientation.VERTICAL) {
        childWidthSize = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        childHeightSize = measuredSize - getPaddingTop() - getPaddingBottom();
    } else {/*from www . java  2  s . co  m*/
        childWidthSize = measuredSize - getPaddingLeft() - getPaddingRight();
        childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    }

    /*
     * Make sure all children have been properly measured. Decor views first.
     * Right now we cheat and make this less complicated by assuming decor
     * views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                }

                int widthSize = childWidthSize;
                int heightSize = childHeightSize;
                if (lp.width != LayoutParams.WRAP_CONTENT) {
                    widthMode = MeasureSpec.EXACTLY;
                    if (lp.width != LayoutParams.FILL_PARENT) {
                        widthSize = lp.width;
                    }
                }
                if (lp.height != LayoutParams.WRAP_CONTENT) {
                    heightMode = MeasureSpec.EXACTLY;
                    if (lp.height != LayoutParams.FILL_PARENT) {
                        heightSize = lp.height;
                    }
                }
                final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                }
            }
        }
    }

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            if (DEBUG)
                Log.v(TAG, "Measuring #" + i + " " + child + ": " + mChildWidthMeasureSpec);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                if (mOrientation == Orientation.VERTICAL) {
                    final int heightSpec = MeasureSpec
                            .makeMeasureSpec((int) (childHeightSize * lp.heightFactor), MeasureSpec.EXACTLY);
                    child.measure(mChildWidthMeasureSpec, heightSpec);
                } else {

                    final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * lp.widthFactor),
                            MeasureSpec.EXACTLY);
                    child.measure(widthSpec, mChildHeightMeasureSpec);
                }
            }
        }
    }
}

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

/**
 * {@inheritDoc}//w w w. j ava2s  .  c om
 */
/*
 The layout operation is implemented by delegating the heavy lifting to the
 to the mHorizontalAxis and mVerticalAxis instances of the internal Axis class.
 Together they compute the locations of the vertical and horizontal lines of
 the grid (respectively!).
        
 This method is then left with the simpler task of applying margins, gravity
 and sizing to each child view and then placing it in its cell.
 */
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    consistencyCheck();

    int targetWidth = right - left;
    int targetHeight = bottom - top;

    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();

    mHorizontalAxis.layout(targetWidth - paddingLeft - paddingRight);
    mVerticalAxis.layout(targetHeight - paddingTop - paddingBottom);

    int[] hLocations = mHorizontalAxis.getLocations();
    int[] vLocations = mVerticalAxis.getLocations();

    for (int i = 0, N = getChildCount(); i < N; i++) {
        View c = getChildAt(i);
        if (c.getVisibility() == View.GONE)
            continue;
        LayoutParams lp = getLayoutParams(c);
        Spec columnSpec = lp.columnSpec;
        Spec rowSpec = lp.rowSpec;

        Interval colSpan = columnSpec.span;
        Interval rowSpan = rowSpec.span;

        int x1 = hLocations[colSpan.min];
        int y1 = vLocations[rowSpan.min];

        int x2 = hLocations[colSpan.max];
        int y2 = vLocations[rowSpan.max];

        int cellWidth = x2 - x1;
        int cellHeight = y2 - y1;

        int pWidth = getMeasurement(c, true);
        int pHeight = getMeasurement(c, false);

        Alignment hAlign = columnSpec.getAbsoluteAlignment(true);
        Alignment vAlign = rowSpec.getAbsoluteAlignment(false);

        Bounds boundsX = mHorizontalAxis.getGroupBounds().getValue(i);
        Bounds boundsY = mVerticalAxis.getGroupBounds().getValue(i);

        // Gravity offsets: the location of the alignment group relative to its cell group.
        int gravityOffsetX = hAlign.getGravityOffset(c, cellWidth - boundsX.size(true));
        int gravityOffsetY = vAlign.getGravityOffset(c, cellHeight - boundsY.size(true));

        int leftMargin = getMargin(c, true, true);
        int topMargin = getMargin(c, false, true);
        int rightMargin = getMargin(c, true, false);
        int bottomMargin = getMargin(c, false, false);

        int sumMarginsX = leftMargin + rightMargin;
        int sumMarginsY = topMargin + bottomMargin;

        // Alignment offsets: the location of the view relative to its alignment group.
        int alignmentOffsetX = boundsX.getOffset(this, c, hAlign, pWidth + sumMarginsX, true);
        int alignmentOffsetY = boundsY.getOffset(this, c, vAlign, pHeight + sumMarginsY, false);

        int width = hAlign.getSizeInCell(c, pWidth, cellWidth - sumMarginsX);
        int height = vAlign.getSizeInCell(c, pHeight, cellHeight - sumMarginsY);

        int dx = x1 + gravityOffsetX + alignmentOffsetX;

        int cx = !isLayoutRtlCompat() ? paddingLeft + leftMargin + dx
                : targetWidth - width - paddingRight - rightMargin - dx;
        int cy = paddingTop + y1 + gravityOffsetY + alignmentOffsetY + topMargin;

        if (width != c.getMeasuredWidth() || height != c.getMeasuredHeight()) {
            c.measure(makeMeasureSpec(width, EXACTLY), makeMeasureSpec(height, EXACTLY));
        }
        c.layout(cx, cy, cx + width, cy + height);
    }
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 * /* www  . j  a v a 2s . c  o  m*/
 * @param queryAdapter
 *            true to requery the adapter for view data
 */
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    // final int itemMargin = mItemMargin;
    final int leftMargin = mItemLeftMargin;
    final int rightMargin = mItemRightMargin;
    final int topMargin = mItemTopMargin;
    final int bottomMargin = mItemBottomMargin;

    final int colWidth = (getWidth() - paddingLeft - paddingRight
            - (leftMargin + rightMargin) * (mColCount - 1)) / mColCount;
    mColWidth = colWidth;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    int amountRemoved = 0;

    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {

            View newView = obtainView(position, child);
            if (newView == null) {
                // child has been removed
                removeViewAt(i);
                if (i - 1 >= 0)
                    invalidateLayoutRecordsAfterPosition(i - 1);
                amountRemoved++;
                continue;
            } else if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have
            // changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + (leftMargin + rightMargin) * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + bottomMargin
                : child.getTop();

        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + bottomMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }

        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + leftMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < (childCount - amountRemoved); i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }

    if (this.mSelectorPosition != INVALID_POSITION) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else if (mTouchMode > TOUCH_MODE_DOWN) {
        View child = getChildAt(mMotionPosition - mFirstPosition);
        if (child != null)
            positionSelector(mMotionPosition, child);
    } else {
        mSelectorRect.setEmpty();
    }
    invokeOnItemScrollListener();

}

From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java

/**
 * Sub method for {@link #onMeasure(int, int)} when the main axis direction is vertical
 * (either from top to bottom or bottom to top).
 *
 * @param widthMeasureSpec  horizontal space requirements as imposed by the parent
 * @param heightMeasureSpec vertical space requirements as imposed by the parent
 * @see #onMeasure(int, int)//from   w  w w . j a va  2s  .com
 * @see #setFlexDirection(int)
 * @see #setFlexWrap(int)
 * @see #setAlignItems(int)
 * @see #setAlignContent(int)
 */
private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int childState = 0;

    mFlexLines.clear();

    // Determine how many flex lines are needed in this layout by measuring each child.
    // (Expand or shrink the view depending on the flexGrow and flexShrink attributes in a later
    // loop)
    int childCount = getChildCount();
    int paddingTop = getPaddingTop();
    int paddingBottom = getPaddingBottom();
    int largestWidthInColumn = Integer.MIN_VALUE;
    FlexLine flexLine = new FlexLine();
    flexLine.mMainSize = paddingTop + paddingBottom;
    // The index of the view in a same flex line.
    int indexInFlexLine = 0;
    for (int i = 0; i < childCount; i++) {
        View child = getReorderedChildAt(i);
        if (child == null) {
            addFlexLineIfLastFlexItem(i, childCount, flexLine);
            continue;
        } else if (child.getVisibility() == View.GONE) {
            flexLine.mItemCount++;
            flexLine.mGoneItemCount++;
            addFlexLineIfLastFlexItem(i, childCount, flexLine);
            continue;
        }

        FlexboxLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp.alignSelf == LayoutParams.ALIGN_SELF_STRETCH) {
            flexLine.mIndicesAlignSelfStretch.add(i);
        }

        int childHeight = lp.height;
        if (lp.flexBasisPercent != LayoutParams.FLEX_BASIS_PERCENT_DEFAULT
                && heightMode == MeasureSpec.EXACTLY) {
            childHeight = Math.round(heightSize * lp.flexBasisPercent);
            // Use the dimension from the layout_height attribute if the heightMode is not
            // MeasureSpec.EXACTLY even if any fraction value is set to layout_flexBasisPercent.
            // There are likely quite few use cases where assigning any fraction values
            // with heightMode is not MeasureSpec.EXACTLY (e.g. FlexboxLayout's layout_height
            // is set to wrap_content)
        }

        int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, lp.width);
        int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, childHeight);
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

        // Check the size constraint after the first measurement for the child
        // To prevent the child's width/height violate the size constraints imposed by the
        // {@link LayoutParams#minWidth}, {@link LayoutParams#minHeight},
        // {@link LayoutParams#maxWidth} and {@link LayoutParams#maxHeight} attributes.
        // E.g. When the child's layout_height is wrap_content the measured height may be
        // less than the min height after the first measurement.
        checkSizeConstraints(child);

        childState = ViewCompat.combineMeasuredStates(childState, ViewCompat.getMeasuredState(child));
        largestWidthInColumn = Math.max(largestWidthInColumn,
                child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);

        if (isWrapRequired(heightMode, heightSize, flexLine.mMainSize,
                child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin, lp, i, indexInFlexLine)) {
            if (flexLine.getItemCountNotGone() > 0) {
                addFlexLine(flexLine);
            }

            flexLine = new FlexLine();
            flexLine.mItemCount = 1;
            flexLine.mMainSize = paddingTop + paddingBottom;
            largestWidthInColumn = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            indexInFlexLine = 0;
        } else {
            flexLine.mItemCount++;
            indexInFlexLine++;
        }
        flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
        flexLine.mTotalFlexGrow += lp.flexGrow;
        flexLine.mTotalFlexShrink += lp.flexShrink;
        // Temporarily set the cross axis length as the largest child width in the column
        // Expand along the cross axis depending on the mAlignContent property if needed
        // later
        flexLine.mCrossSize = Math.max(flexLine.mCrossSize, largestWidthInColumn);

        if (hasDividerBeforeChildAtAlongMainAxis(i, indexInFlexLine)) {
            flexLine.mMainSize += mDividerHorizontalHeight;
        }
        addFlexLineIfLastFlexItem(i, childCount, flexLine);
    }

    determineMainSize(mFlexDirection, widthMeasureSpec, heightMeasureSpec);
    determineCrossSize(mFlexDirection, widthMeasureSpec, heightMeasureSpec,
            getPaddingLeft() + getPaddingRight());
    // Now cross size for each flex line is determined.
    // Expand the views if alignItems (or alignSelf in each child view) is set to stretch
    stretchViews(mFlexDirection, mAlignItems);
    setMeasuredDimensionForFlex(mFlexDirection, widthMeasureSpec, heightMeasureSpec, childState);
}

From source file:com.android.launcher3.Workspace.java

public void onExternalDragStartedWithItem(View v) {
    // Compose a drag bitmap with the view scaled to the icon size
    LauncherAppState app = LauncherAppState.getInstance();
    DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
    int iconSize = grid.iconSizePx;
    int bmpWidth = v.getMeasuredWidth();
    int bmpHeight = v.getMeasuredHeight();

    // If this is a text view, use its drawable instead
    if (v instanceof TextView) {
        TextView tv = (TextView) v;/*from   w ww . j  a  v  a  2 s. c o m*/
        Drawable d = tv.getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        bmpWidth = bounds.width();
        bmpHeight = bounds.height();
    }

    // Compose the bitmap to create the icon from
    Bitmap b = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    mCanvas.setBitmap(b);
    drawDragView(v, mCanvas, 0);
    mCanvas.setBitmap(null);

    // The outline is used to visualize where the item will land if dropped
    mDragOutline = createDragOutline(b, DRAG_BITMAP_PADDING, iconSize, iconSize, true);
}

From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java

/**
 * Sub method for {@link #onMeasure(int, int)}, when the main axis direction is horizontal
 * (either left to right or right to left).
 *
 * @param widthMeasureSpec  horizontal space requirements as imposed by the parent
 * @param heightMeasureSpec vertical space requirements as imposed by the parent
 * @see #onMeasure(int, int)// w w  w . jav  a 2 s .  c o m
 * @see #setFlexDirection(int)
 * @see #setFlexWrap(int)
 * @see #setAlignItems(int)
 * @see #setAlignContent(int)
 */
private void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int childState = 0;

    mFlexLines.clear();

    // Determine how many flex lines are needed in this layout by measuring each child.
    // (Expand or shrink the view depending on the flexGrow and flexShrink attributes in a later
    // loop)
    {
        int childCount = getChildCount();
        int paddingStart = ViewCompat.getPaddingStart(this);
        int paddingEnd = ViewCompat.getPaddingEnd(this);
        int largestHeightInRow = Integer.MIN_VALUE;
        FlexLine flexLine = new FlexLine();

        // The index of the view in a same flex line.
        int indexInFlexLine = 0;
        flexLine.mMainSize = paddingStart + paddingEnd;
        for (int i = 0; i < childCount; i++) {
            View child = getReorderedChildAt(i);
            if (child == null) {
                addFlexLineIfLastFlexItem(i, childCount, flexLine);
                continue;
            } else if (child.getVisibility() == View.GONE) {
                flexLine.mItemCount++;
                flexLine.mGoneItemCount++;
                addFlexLineIfLastFlexItem(i, childCount, flexLine);
                continue;
            }

            FlexboxLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp.alignSelf == LayoutParams.ALIGN_SELF_STRETCH) {
                flexLine.mIndicesAlignSelfStretch.add(i);
            }

            int childWidth = lp.width;
            if (lp.flexBasisPercent != LayoutParams.FLEX_BASIS_PERCENT_DEFAULT
                    && widthMode == MeasureSpec.EXACTLY) {
                childWidth = Math.round(widthSize * lp.flexBasisPercent);
                // Use the dimension from the layout_width attribute if the widthMode is not
                // MeasureSpec.EXACTLY even if any fraction value is set to
                // layout_flexBasisPercent.
                // There are likely quite few use cases where assigning any fraction values
                // with widthMode is not MeasureSpec.EXACTLY (e.g. FlexboxLayout's layout_width
                // is set to wrap_content)
            }
            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                    getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin, childWidth);
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                    getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, lp.height);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            // Check the size constraint after the first measurement for the child
            // To prevent the child's width/height violate the size constraints imposed by the
            // {@link LayoutParams#minWidth}, {@link LayoutParams#minHeight},
            // {@link LayoutParams#maxWidth} and {@link LayoutParams#maxHeight} attributes.
            // E.g. When the child's layout_width is wrap_content the measured width may be
            // less than the min width after the first measurement.
            checkSizeConstraints(child);

            childState = ViewCompat.combineMeasuredStates(childState, ViewCompat.getMeasuredState(child));
            largestHeightInRow = Math.max(largestHeightInRow,
                    child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);

            if (isWrapRequired(widthMode, widthSize, flexLine.mMainSize,
                    child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin, lp, i, indexInFlexLine)) {
                if (flexLine.getItemCountNotGone() > 0) {
                    addFlexLine(flexLine);
                }

                flexLine = new FlexLine();
                flexLine.mItemCount = 1;
                flexLine.mMainSize = paddingStart + paddingEnd;
                largestHeightInRow = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
                indexInFlexLine = 0;
            } else {
                flexLine.mItemCount++;
                indexInFlexLine++;
            }
            flexLine.mMainSize += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            flexLine.mTotalFlexGrow += lp.flexGrow;
            flexLine.mTotalFlexShrink += lp.flexShrink;
            // Temporarily set the cross axis length as the largest child in the row
            // Expand along the cross axis depending on the mAlignContent property if needed
            // later
            flexLine.mCrossSize = Math.max(flexLine.mCrossSize, largestHeightInRow);

            // Check if the beginning or middle divider is required for the flex item
            if (hasDividerBeforeChildAtAlongMainAxis(i, indexInFlexLine)) {
                flexLine.mMainSize += mDividerVerticalWidth;
                flexLine.mDividerLengthInMainSize += mDividerVerticalWidth;
            }

            if (mFlexWrap != FLEX_WRAP_WRAP_REVERSE) {
                flexLine.mMaxBaseline = Math.max(flexLine.mMaxBaseline, child.getBaseline() + lp.topMargin);
            } else {
                // if the flex wrap property is FLEX_WRAP_WRAP_REVERSE, calculate the
                // baseline as the distance from the cross end and the baseline
                // since the cross size calculation is based on the distance from the cross end
                flexLine.mMaxBaseline = Math.max(flexLine.mMaxBaseline,
                        child.getMeasuredHeight() - child.getBaseline() + lp.bottomMargin);
            }
            addFlexLineIfLastFlexItem(i, childCount, flexLine);
        }
    }

    determineMainSize(mFlexDirection, widthMeasureSpec, heightMeasureSpec);

    // TODO: Consider the case any individual child's alignSelf is set to ALIGN_SELF_BASELINE
    if (mAlignItems == ALIGN_ITEMS_BASELINE) {
        int viewIndex = 0;
        for (FlexLine flexLine : mFlexLines) {
            // The largest height value that also take the baseline shift into account
            int largestHeightInLine = Integer.MIN_VALUE;
            for (int i = viewIndex; i < viewIndex + flexLine.mItemCount; i++) {
                View child = getReorderedChildAt(i);
                LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (mFlexWrap != FLEX_WRAP_WRAP_REVERSE) {
                    int marginTop = flexLine.mMaxBaseline - child.getBaseline();
                    marginTop = Math.max(marginTop, lp.topMargin);
                    largestHeightInLine = Math.max(largestHeightInLine,
                            child.getHeight() + marginTop + lp.bottomMargin);
                } else {
                    int marginBottom = flexLine.mMaxBaseline - child.getMeasuredHeight() + child.getBaseline();
                    marginBottom = Math.max(marginBottom, lp.bottomMargin);
                    largestHeightInLine = Math.max(largestHeightInLine,
                            child.getHeight() + lp.topMargin + marginBottom);
                }
            }
            flexLine.mCrossSize = largestHeightInLine;
            viewIndex += flexLine.mItemCount;
        }
    }

    determineCrossSize(mFlexDirection, widthMeasureSpec, heightMeasureSpec,
            getPaddingTop() + getPaddingBottom());
    // Now cross size for each flex line is determined.
    // Expand the views if alignItems (or alignSelf in each child view) is set to stretch
    stretchViews(mFlexDirection, mAlignItems);
    setMeasuredDimensionForFlex(mFlexDirection, widthMeasureSpec, heightMeasureSpec, childState);
}

From source file:com.android.soma.Launcher.java

boolean startActivity(View v, Intent intent, Object tag) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    try {/* w  w  w .  ja  v a  2s  .c  o  m*/
        // Only launch using the new animation if the shortcut has not opted out (this is a
        // private contract between launcher and may be ignored in the future).
        boolean useLaunchAnimation = (v != null) && !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
        if (useLaunchAnimation) {
            ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(),
                    v.getMeasuredHeight());

            startActivity(intent, opts.toBundle());
        } else {
            startActivity(intent);
        }
        return true;
    } catch (SecurityException e) {
        Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
        Log.e(TAG, "Launcher does not have the permission to launch " + intent
                + ". Make sure to create a MAIN intent-filter for the corresponding activity "
                + "or use the exported attribute for this activity. " + "tag=" + tag + " intent=" + intent, e);
    }
    return false;
}