Example usage for android.view View measure

List of usage examples for android.view View measure

Introduction

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

Prototype

public final void measure(int widthMeasureSpec, int heightMeasureSpec) 

Source Link

Document

This is called to find out how big a view should be.

Usage

From source file:com.coleman.demo.view.page.DirectionalViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, or 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));

    // Children are just made to fill our space.
    mChildWidthMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;/*from  www . ja  va2s  . co  m*/
    populate();
    mInLayout = false;

    // Make sure all children have been properly measured.
    final int 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 + " x "
                        + mChildHeightMeasureSpec);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

From source file:cn.com.zzwfang.view.directionalviewpager.DirectionalViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // For simple implementation, or 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));

    // Children are just made to fill our space.
    mChildWidthMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;/*from w ww . j  a v  a  2 s  .c o  m*/
    populate();
    mInLayout = false;

    // Make sure all children have been properly measured.
    final int 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 + " x "
                        + mChildHeightMeasureSpec);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

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

/**
 * Measures the height of the given range of children (inclusive) and returns the height
 * with this ListView's padding and divider heights included. If maxHeight is provided, the
 * measuring will stop when the current height reaches maxHeight.
 *
 * @param widthMeasureSpec             The width measure spec to be given to a child's
 *                                     {@link View#measure(int, int)}.
 * @param startPosition                The position of the first child to be shown.
 * @param endPosition                  The (inclusive) position of the last child to be
 *                                     shown. Specify {@link #NO_POSITION} if the last child
 *                                     should be the last available child from the adapter.
 * @param maxHeight                    The maximum height that will be returned (if all the
 *                                     children don't fit in this value, this value will be
 *                                     returned).
 * @param disallowPartialChildPosition In general, whether the returned height should only
 *                                     contain entire children. This is more powerful--it is
 *                                     the first inclusive position at which partial
 *                                     children will not be allowed. Example: it looks nice
 *                                     to have at least 3 completely visible children, and
 *                                     in portrait this will most likely fit; but in
 *                                     landscape there could be times when even 2 children
 *                                     can not be completely shown, so a value of 2
 *                                     (remember, inclusive) would be good (assuming
 *                                     startPosition is 0).
 * @return The height of this ListView with the given children.
 */// ww  w  .j  ava  2  s.  c  o  m
public int measureHeightOfChildrenCompat(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getListPaddingTop();
    final int paddingBottom = getListPaddingBottom();
    final int paddingLeft = getListPaddingLeft();
    final int paddingRight = getListPaddingRight();
    final int reportedDividerHeight = getDividerHeight();
    final Drawable divider = getDivider();

    final ListAdapter adapter = getAdapter();

    if (adapter == null) {
        return paddingTop + paddingBottom;
    }

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int dividerHeight = ((reportedDividerHeight > 0) && divider != null) ? reportedDividerHeight : 0;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;

    View child = null;
    int viewType = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        int newType = adapter.getItemViewType(i);
        if (newType != viewType) {
            child = null;
            viewType = newType;
        }
        child = adapter.getView(i, child, this);

        // Compute child height spec
        int heightMeasureSpec;
        ViewGroup.LayoutParams childLp = child.getLayoutParams();

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

        if (childLp.height > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childLp.height, MeasureSpec.EXACTLY);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(widthMeasureSpec, heightMeasureSpec);

        // Since this view was measured directly aginst the parent measure
        // spec, we must measure it again before reuse.
        child.forceLayout();

        if (i > 0) {
            // Count the divider for all but one child
            returnedHeight += dividerHeight;
        }

        returnedHeight += child.getMeasuredHeight();

        if (returnedHeight >= maxHeight) {
            // We went over, figure out which height to return.  If returnedHeight >
            // maxHeight, then the i'th position did not fit completely.
            return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
                    && (i > disallowPartialChildPosition) // We've past the min pos
                    && (prevHeightWithoutPartialChild > 0) // We have a prev height
                    && (returnedHeight != maxHeight) // i'th child did not fit completely
                            ? prevHeightWithoutPartialChild
                            : maxHeight;
        }

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

    // At this point, we went through the range of children, and they each
    // completely fit, so return the returnedHeight
    return returnedHeight;
}

From source file:com.android.hcframe.DraggableGridViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int childCount = getChildCount();
    mPageCount = (childCount + mPageSize - 1) / mPageSize;
    mGridWidth = (getWidth() - mPaddingLeft - mPaddingRight - (mColCount - 1) * mGridGap) / mColCount;
    mGridHeight = (getHeight() - mPaddingTop - mPaddingButtom - (mRowCount - 1) * mGridGap) / mRowCount;
    //      mGridWidth = mGridHeight = Math.min(mGridWidth, mGridHeight);
    mMaxOverScrollSize = mGridWidth / 2;
    mEdgeSize = mGridWidth / 2;/*from   w ww  . j  av  a2  s.c o  m*/
    newPositions.clear();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final Rect rect = getRectByPosition(i);
        child.measure(MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY));
        child.layout(rect.left, rect.top, rect.right, rect.bottom);
        newPositions.add(-1);
    }
    if (mCurItem > 0 && mCurItem < mPageCount) {
        final int curItem = mCurItem;
        mCurItem = 0;
        setCurrentItem(curItem);
    }
}

From source file:android.support.v7.app.MediaRouteControllerDialog.java

/**
 * Updates the height of views and hide artwork or metadata if space is limited.
 *///from   w ww  . java  2s. co  m
private void updateLayoutHeightInternal(boolean animate) {
    // Measure the size of widgets and get the height of main components.
    int oldHeight = getLayoutHeight(mMediaMainControlLayout);
    setLayoutHeight(mMediaMainControlLayout, ViewGroup.LayoutParams.FILL_PARENT);
    updateMediaControlVisibility(canShowPlaybackControlLayout());
    View decorView = getWindow().getDecorView();
    decorView.measure(MeasureSpec.makeMeasureSpec(getWindow().getAttributes().width, MeasureSpec.EXACTLY),
            MeasureSpec.UNSPECIFIED);
    setLayoutHeight(mMediaMainControlLayout, oldHeight);
    int artViewHeight = 0;
    if (mCustomControlView == null && mArtView.getDrawable() instanceof BitmapDrawable) {
        Bitmap art = ((BitmapDrawable) mArtView.getDrawable()).getBitmap();
        if (art != null) {
            artViewHeight = getDesiredArtHeight(art.getWidth(), art.getHeight());
            mArtView.setScaleType(art.getWidth() >= art.getHeight() ? ImageView.ScaleType.FIT_XY
                    : ImageView.ScaleType.FIT_CENTER);
        }
    }
    int mainControllerHeight = getMainControllerHeight(canShowPlaybackControlLayout());
    int volumeGroupListCount = mGroupMemberRoutes.size();
    // Scale down volume group list items in landscape mode.
    int expandedGroupListHeight = getGroup() == null ? 0
            : mVolumeGroupListItemHeight * getGroup().getRoutes().size();
    if (volumeGroupListCount > 0) {
        expandedGroupListHeight += mVolumeGroupListPaddingTop;
    }
    expandedGroupListHeight = Math.min(expandedGroupListHeight, mVolumeGroupListMaxHeight);
    int visibleGroupListHeight = mIsGroupExpanded ? expandedGroupListHeight : 0;

    int desiredControlLayoutHeight = Math.max(artViewHeight, visibleGroupListHeight) + mainControllerHeight;
    Rect visibleRect = new Rect();
    decorView.getWindowVisibleDisplayFrame(visibleRect);
    // Height of non-control views in decor view.
    // This includes title bar, button bar, and dialog's vertical padding which should be
    // always shown.
    int nonControlViewHeight = mDialogAreaLayout.getMeasuredHeight()
            - mDefaultControlLayout.getMeasuredHeight();
    // Maximum allowed height for controls to fit screen.
    int maximumControlViewHeight = visibleRect.height() - nonControlViewHeight;

    // Show artwork if it fits the screen.
    if (mCustomControlView == null && artViewHeight > 0
            && desiredControlLayoutHeight <= maximumControlViewHeight) {
        mArtView.setVisibility(View.VISIBLE);
        setLayoutHeight(mArtView, artViewHeight);
    } else {
        if (getLayoutHeight(mVolumeGroupList)
                + mMediaMainControlLayout.getMeasuredHeight() >= mDefaultControlLayout.getMeasuredHeight()) {
            mArtView.setVisibility(View.GONE);
        }
        artViewHeight = 0;
        desiredControlLayoutHeight = visibleGroupListHeight + mainControllerHeight;
    }
    // Show the playback control if it fits the screen.
    if (canShowPlaybackControlLayout() && desiredControlLayoutHeight <= maximumControlViewHeight) {
        mPlaybackControlLayout.setVisibility(View.VISIBLE);
    } else {
        mPlaybackControlLayout.setVisibility(View.GONE);
    }
    updateMediaControlVisibility(mPlaybackControlLayout.getVisibility() == View.VISIBLE);
    mainControllerHeight = getMainControllerHeight(mPlaybackControlLayout.getVisibility() == View.VISIBLE);
    desiredControlLayoutHeight = Math.max(artViewHeight, visibleGroupListHeight) + mainControllerHeight;

    // Limit the volume group list height to fit the screen.
    if (desiredControlLayoutHeight > maximumControlViewHeight) {
        visibleGroupListHeight -= (desiredControlLayoutHeight - maximumControlViewHeight);
        desiredControlLayoutHeight = maximumControlViewHeight;
    }
    // Update the layouts with the computed heights.
    mMediaMainControlLayout.clearAnimation();
    mVolumeGroupList.clearAnimation();
    mDefaultControlLayout.clearAnimation();
    if (animate) {
        animateLayoutHeight(mMediaMainControlLayout, mainControllerHeight);
        animateLayoutHeight(mVolumeGroupList, visibleGroupListHeight);
        animateLayoutHeight(mDefaultControlLayout, desiredControlLayoutHeight);
    } else {
        setLayoutHeight(mMediaMainControlLayout, mainControllerHeight);
        setLayoutHeight(mVolumeGroupList, visibleGroupListHeight);
        setLayoutHeight(mDefaultControlLayout, desiredControlLayoutHeight);
    }
    // Maximize the window size with a transparent layout in advance for smooth animation.
    setLayoutHeight(mExpandableAreaLayout, visibleRect.height());
    rebuildVolumeGroupList(animate);
}

From source file:com.ebaonet.lawyer.ui.weight.DraggableGridViewPager.java

private void animateDragged() {
    if (mLastDragged >= 0) {
        final View v = getChildAt(mLastDragged);

        final Rect r = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
        r.inset(-r.width() / 20, -r.height() / 20);
        v.measure(MeasureSpec.makeMeasureSpec(r.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(r.height(), MeasureSpec.EXACTLY));
        v.layout(r.left, r.top, r.right, r.bottom);

        AnimationSet animSet = new AnimationSet(true);
        ScaleAnimation scale = new ScaleAnimation(0.9091f, 1, 0.9091f, 1, v.getWidth() / 2, v.getHeight() / 2);
        scale.setDuration(ANIMATION_DURATION);
        AlphaAnimation alpha = new AlphaAnimation(1, .8f);
        alpha.setDuration(ANIMATION_DURATION);

        animSet.addAnimation(scale);/*  w ww.  ja  va  2  s.  c  o m*/
        animSet.addAnimation(alpha);
        animSet.setFillEnabled(true);
        animSet.setFillAfter(true);

        v.clearAnimation();
        v.startAnimation(animSet);
    }
}

From source file:co.ceryle.segmentedbutton.SegmentedButtonGroup.java

/**
 * Crate a bitmap from the given view//  w  w w .j a  va2 s. c  o  m
 */
private Bitmap getViewBitmap(View view) {
    // setContainerAttrs();

    //Get the dimensions of the view so we can re-layout the view at its current size
    //and create a bitmap of the same size
    int width = view.getMeasuredWidth();
    int height = view.getMeasuredHeight();

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

    //Cause the view to re-layout
    view.measure(measuredWidth, measuredHeight);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    //Create a bitmap backed Canvas to draw the view into
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
    view.draw(c);
    return b;
}

From source file:com.android.hcframe.DraggableGridViewPager.java

private void animateDragged() {
    if (mLastDragged >= 0) {
        final View v = getChildAt(mLastDragged);

        final Rect r = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
        r.inset(-r.width() / 20, -r.height() / 20);
        v.measure(MeasureSpec.makeMeasureSpec(r.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(r.height(), MeasureSpec.EXACTLY));
        v.layout(r.left, r.top, r.right, r.bottom);

        AnimationSet animSet = new AnimationSet(true);
        ScaleAnimation scale = new ScaleAnimation(0.9091f, 1, 0.9091f, 1, v.getWidth() / 2, v.getHeight() / 2);
        scale.setDuration(ANIMATION_DURATION);
        AlphaAnimation alpha = new AlphaAnimation(1, .5f);
        alpha.setDuration(ANIMATION_DURATION);

        animSet.addAnimation(scale);/*from  w ww  . j  a v a 2s.  c om*/
        animSet.addAnimation(alpha);
        animSet.setFillEnabled(true);
        animSet.setFillAfter(true);

        v.clearAnimation();
        v.startAnimation(animSet);
    }
}

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

/**
 * Helper for makeAndAddView to set the position of a view
 * and fill out its layout paramters.//from ww w .j av  a  2  s .  c o  m
 *
 * @param child The view to position
 */
private void setUpChild(View child) {

    // Respect layout params that are already in the view. Otherwise
    // make some up...
    ViewGroup.LayoutParams lp = child.getLayoutParams();
    if (lp == null) {
        lp = generateDefaultLayoutParams();
    }

    addViewInLayout(child, 0, lp);

    child.setSelected(hasFocus());
    if (mDisableChildrenWhenDisabled) {
        child.setEnabled(isEnabled());
    }

    // Get measure specs
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec,
            mSpinnerPadding.top + mSpinnerPadding.bottom, lp.height);
    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
            mSpinnerPadding.left + mSpinnerPadding.right, lp.width);

    // Measure child
    child.measure(childWidthSpec, childHeightSpec);

    int childLeft;
    int childRight;

    // Position vertically based on gravity setting
    int childTop = mSpinnerPadding.top
            + ((getMeasuredHeight() - mSpinnerPadding.bottom - mSpinnerPadding.top - child.getMeasuredHeight())
                    / 2);
    int childBottom = childTop + child.getMeasuredHeight();

    int width = child.getMeasuredWidth();
    childLeft = 0;
    childRight = childLeft + width;

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

From source file:cn.ismartv.tvrecyclerview.widget.GridLayoutManager.java

private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec,
        boolean alreadyMeasured) {
    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
    final boolean measure;
    if (alreadyMeasured) {
        measure = shouldReMeasureChild(child, widthSpec, heightSpec, lp);
    } else {//  ww  w.ja  v a2s.com
        measure = shouldMeasureChild(child, widthSpec, heightSpec, lp);
    }
    if (measure) {
        child.measure(widthSpec, heightSpec);
    }
}