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:androidx.mediarouter.app.MediaRouteControllerDialog.java

/**
 * Updates the height of views and hide artwork or metadata if space is limited.
 */// w  ww .  j  a v a2 s .c  o m
void updateLayoutHeightInternal(boolean animate) {
    // Measure the size of widgets and get the height of main components.
    int oldHeight = getLayoutHeight(mMediaMainControlLayout);
    setLayoutHeight(mMediaMainControlLayout, ViewGroup.LayoutParams.MATCH_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:android.support.v71.widget.GridLayoutManager.java

private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec,
        boolean capBothSpecs) {
    calculateItemDecorationsForChild(child, mDecorInsets);
    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
    if (capBothSpecs || mOrientation == VERTICAL) {
        widthSpec = updateSpecWithExtra(widthSpec, lp.leftMargin + mDecorInsets.left,
                lp.rightMargin + mDecorInsets.right);
    }//from   ww w.j av  a  2 s.  c  om
    if (capBothSpecs || mOrientation == HORIZONTAL) {
        heightSpec = updateSpecWithExtra(heightSpec, lp.topMargin + mDecorInsets.top,
                lp.bottomMargin + mDecorInsets.bottom);
    }
    child.measure(widthSpec, heightSpec);
}

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

private void measureScrapChild(View child, int position, int widthMeasureSpec) {
    ListView.LayoutParams p = (ListView.LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                0);/*from   w  ww .  j  av  a2s  .co  m*/
        child.setLayoutParams(p);
    }
    //XXX p.viewType = mAdapter.getItemViewType(position);
    //XXX p.forceAdd = true;

    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
            mDropDownList.getPaddingLeft() + mDropDownList.getPaddingRight(), p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

From source file:com.grottworkshop.gwsmaterialcalendarview.MaterialCalendarView.java

/**
 * {@inheritDoc}/*from   w  w w .ja va  2s .  c o  m*/
 */
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    final int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);
    final int specWidthMode = MeasureSpec.getMode(widthMeasureSpec);
    final int specHeightSize = MeasureSpec.getSize(heightMeasureSpec);
    final int specHeightMode = MeasureSpec.getMode(heightMeasureSpec);

    //We need to disregard padding for a while. This will be added back later
    final int desiredWidth = specWidthSize - getPaddingLeft() - getPaddingRight();
    final int desiredHeight = specHeightSize - getPaddingTop() - getPaddingBottom();

    final int viewTileHieght = getTopbarVisible() ? (MonthView.DEFAULT_MONTH_TILE_HEIGHT + 1)
            : MonthView.DEFAULT_MONTH_TILE_HEIGHT;

    //Calculate independent tile sizes for later
    int desiredTileWidth = desiredWidth / MonthView.DEFAULT_DAYS_IN_WEEK;
    int desiredTileHeight = desiredHeight / viewTileHieght;

    int measureTileSize = -1;

    if (this.tileSize > 0) {
        //We have a tileSize set, we should use that
        measureTileSize = this.tileSize;
    } else if (specWidthMode == MeasureSpec.EXACTLY) {
        if (specHeightMode == MeasureSpec.EXACTLY) {
            //Pick the larger of the two explicit sizes
            measureTileSize = Math.max(desiredTileWidth, desiredTileHeight);
        } else {
            //Be the width size the user wants
            measureTileSize = desiredTileWidth;
        }
    } else if (specHeightMode == MeasureSpec.EXACTLY) {
        //Be the height size the user wants
        measureTileSize = desiredTileHeight;
    }

    //Uh oh! We need to default to something, quick!
    if (measureTileSize <= 0) {
        measureTileSize = dpToPx(DEFAULT_TILE_SIZE_DP);
    }

    //Calculate our size based off our measured tile size
    int measuredWidth = measureTileSize * MonthView.DEFAULT_DAYS_IN_WEEK;
    int measuredHeight = measureTileSize * viewTileHieght;

    //Put padding back in from when we took it away
    measuredWidth += getPaddingLeft() + getPaddingRight();
    measuredHeight += getPaddingTop() + getPaddingBottom();

    //Contract fulfilled, setting out measurements
    setMeasuredDimension(
            //We clamp inline because we want to use un-clamped versions on the children
            clampSize(measuredWidth, widthMeasureSpec), clampSize(measuredHeight, heightMeasureSpec));

    int count = getChildCount();

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);

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

        int childWidthMeasureSpec = MeasureSpec
                .makeMeasureSpec(MonthView.DEFAULT_DAYS_IN_WEEK * measureTileSize, MeasureSpec.EXACTLY);

        int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(p.height * measureTileSize,
                MeasureSpec.EXACTLY);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
}

From source file:com.dm.xz.views.PinnedSectionListView.java

/** Create shadow wrapper with a pinned view for a view at given position */
void createPinnedShadow(int position) {

    // try to recycle shadow
    PinnedSection pinnedShadow = mRecycleSection;
    mRecycleSection = null;// w ww.j a va  2 s .  c  o  m

    // create new shadow, if needed
    if (pinnedShadow == null)
        pinnedShadow = new PinnedSection();
    // request new view using recycled view, if such
    View pinnedView = getAdapter().getView(position, pinnedShadow.view, PinnedSectionListView.this);

    // read layout parameters
    LayoutParams layoutParams = (LayoutParams) pinnedView.getLayoutParams();
    if (layoutParams == null) {
        layoutParams = (LayoutParams) generateDefaultLayoutParams();
        pinnedView.setLayoutParams(layoutParams);
    }

    int heightMode = MeasureSpec.getMode(layoutParams.height);
    int heightSize = MeasureSpec.getSize(layoutParams.height);

    if (heightMode == MeasureSpec.UNSPECIFIED)
        heightMode = MeasureSpec.EXACTLY;

    int maxHeight = getHeight() - getListPaddingTop() - getListPaddingBottom();
    if (heightSize > maxHeight)
        heightSize = maxHeight;

    // measure & layout
    int ws = MeasureSpec.makeMeasureSpec(getWidth() - getListPaddingLeft() - getListPaddingRight(),
            MeasureSpec.EXACTLY);
    int hs = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
    pinnedView.measure(ws, hs);
    pinnedView.layout(0, 0, pinnedView.getMeasuredWidth(), pinnedView.getMeasuredHeight());
    mTranslateY = 0;

    // initialize pinned shadow
    pinnedShadow.view = pinnedView;
    pinnedShadow.position = position;
    pinnedShadow.id = getAdapter().getItemId(position);

    // store pinned shadow
    mPinnedSection = pinnedShadow;
}

From source file:com.ftinc.kit.attributr.ui.widget.StickyRecyclerHeadersElevationDecoration.java

/**
 * Gets the header view for the associated position.  If it doesn't exist yet, it will be
 * created, measured, and laid out.//from  www  .jav a2  s.c  o  m
 * @param parent
 * @param position
 * @return Header view
 */
public View getHeaderView(RecyclerView parent, int position) {
    long headerId = mAdapter.getHeaderId(position);

    RecyclerView.ViewHolder viewHolder = mHeaderViews.get(headerId);
    if (viewHolder == null) {

        viewHolder = mAdapter.onCreateHeaderViewHolder(parent);
        View header = viewHolder.itemView;
        header.setTag(viewHolder);

        if (header.getLayoutParams() == null) {

            header.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));

        }

        int widthSpec;
        int heightSpec;

        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
            heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);
        } else {
            widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.UNSPECIFIED);
            heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.EXACTLY);
        }

        int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
                parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
        int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
                parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);
        header.measure(childWidth, childHeight);
        header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
        mHeaderViews.put(headerId, viewHolder);
    }

    // Rebind content to the view holder
    mAdapter.onBindHeaderViewHolder(viewHolder, position);

    return viewHolder.itemView;
}

From source file:com.android.photos.views.GalleryThumbnailView.java

/**
 * Obtain the view and add it to our list of children. The view can be made
 * fresh, converted from an unused view, or used as is if it was in the
 * recycle bin./*w  ww. j a v a  2 s  .  c  om*/
 *
 * @param startPosition Logical position in the list to start from
 * @param x Left or right edge of the view to add
 * @param forward If true, align left edge to x and increase position.
 *                If false, align right edge to x and decrease position.
 * @return Number of views added
 */
private int makeAndAddColumn(int startPosition, int x, boolean forward) {
    int columnWidth = mLargeColumnWidth;
    int addViews = 0;
    for (int remaining = mLargeColumnUnitCount, i = 0; remaining > 0 && startPosition + i >= 0
            && startPosition + i < mItemCount; i += forward ? 1 : -1, addViews++) {
        if (mAdapter.getIntrinsicAspectRatio(startPosition + i) >= 1f) {
            // landscape
            remaining -= LAND_UNITS;
        } else {
            // portrait
            remaining -= PORT_UNITS;
            if (remaining < 0) {
                remaining += (mSmallColumnUnitCount - mLargeColumnUnitCount);
                columnWidth = mSmallColumnWidth;
            }
        }
    }
    int nextTop = 0;
    for (int i = 0; i < addViews; i++) {
        int position = startPosition + (forward ? i : -i);
        View child = obtainView(position, null);
        if (child.getParent() != this) {
            if (mInLayout) {
                addViewInLayout(child, forward ? -1 : 0, child.getLayoutParams());
            } else {
                addView(child, forward ? -1 : 0);
            }
        }
        int heightSize = (int) (.5f
                + (mAdapter.getIntrinsicAspectRatio(position) >= 1f ? columnWidth / ASPECT_RATIO
                        : columnWidth * ASPECT_RATIO));
        int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
        int widthSpec = MeasureSpec.makeMeasureSpec(columnWidth, MeasureSpec.EXACTLY);
        child.measure(widthSpec, heightSpec);
        int childLeft = forward ? x : x - columnWidth;
        child.layout(childLeft, nextTop, childLeft + columnWidth, nextTop + heightSize);
        nextTop += heightSize;
    }
    return addViews;
}

From source file:cc.echonet.coolmicapp.MainActivity.java

private void goAbout() {

    Log.d("MainActivity", "goAbout() ");

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popUpView = inflater.inflate(R.layout.popup_about, null, false);

    final PopupWindow popUp = new PopupWindow(this);

    Button close = (Button) popUpView.findViewById(R.id.cmdPopUpDismiss);
    close.setOnClickListener(new View.OnClickListener() {
        public void onClick(View popupView) {
            popUp.dismiss();/*  w  w w  . j a  va2s . c  om*/
        }
    });

    ((TextView) popUpView.findViewById(R.id.txtVersion)).setText(BuildConfig.VERSION_NAME);
    ((TextView) popUpView.findViewById(R.id.txtBuildType)).setText(BuildConfig.BUILD_TYPE);
    ((TextView) popUpView.findViewById(R.id.txtGITBranch)).setText(BuildConfig.GIT_BRANCH);
    ((TextView) popUpView.findViewById(R.id.txtGITRevision)).setText(BuildConfig.GIT_REVISION);
    ((TextView) popUpView.findViewById(R.id.txtGITAuthor)).setText(BuildConfig.GIT_AUTHOR);
    ((TextView) popUpView.findViewById(R.id.txtGITDirty)).setText(BuildConfig.GIT_DIRTY);

    popUpView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    popUp.setContentView(popUpView);

    Log.d("MainActivity", String.format("h: %s w: %s h: %s w: %s", popUp.getHeight(), popUp.getWidth(),
            popUpView.getMeasuredHeight(), popUpView.getMeasuredWidth()));

    popUp.setHeight(popUpView.getMeasuredHeight());
    popUp.setWidth(popUpView.getMeasuredWidth());

    popUp.showAtLocation(popUpView, Gravity.CENTER, 0, 0);

    Log.d("MainActivity", "goAbout() end ");
}

From source file:com.github.shareme.gwsstickyheadersrv.library.caching.HeaderViewCache.java

@SuppressWarnings("unchecked")
@Override//from w w w.j a v  a2 s  . c o  m
public View getHeader(RecyclerView parent, int position) {
    long headerId = mAdapter.getHeaderId(position);

    View header = mHeaderViews.get(headerId);
    if (header == null) {
        //TODO - recycle views
        RecyclerView.ViewHolder viewHolder = mAdapter.onCreateHeaderViewHolder(parent);
        mAdapter.onBindHeaderViewHolder(viewHolder, position);
        header = viewHolder.itemView;
        if (header.getLayoutParams() == null) {
            header.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        int widthSpec;
        int heightSpec;

        if (mOrientationProvider.getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
            heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);
        } else {
            widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.UNSPECIFIED);
            heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.EXACTLY);
        }

        int childWidth = ViewGroup.getChildMeasureSpec(widthSpec,
                parent.getPaddingLeft() + parent.getPaddingRight(), header.getLayoutParams().width);
        int childHeight = ViewGroup.getChildMeasureSpec(heightSpec,
                parent.getPaddingTop() + parent.getPaddingBottom(), header.getLayoutParams().height);
        header.measure(childWidth, childHeight);
        header.layout(0, 0, header.getMeasuredWidth(), header.getMeasuredHeight());
        mHeaderViews.put(headerId, header);
    }
    return header;
}

From source file:com.apptentive.android.sdk.module.messagecenter.view.MessageCenterListView.java

/**
 * Create shadow wrapper with a sticky view  at given position
 *///from   ww w  . java  2 s . co m
void createStickyShadow(int position) {

    // recycle shadow
    StickyWrapper stickyViewShadow = recycledHeaderView;
    recycledHeaderView = null;

    // create new shadow, if needed
    if (stickyViewShadow == null) {
        stickyViewShadow = new StickyWrapper();
    }
    // request new view using recycled view, if such
    View stickyView = getAdapter().getView(position, stickyViewShadow.view, MessageCenterListView.this);

    // read layout parameters
    LayoutParams layoutParams = (LayoutParams) stickyView.getLayoutParams();
    if (layoutParams == null) {
        layoutParams = (LayoutParams) generateDefaultLayoutParams();
        stickyView.setLayoutParams(layoutParams);
    }

    View childLayout = ((ViewGroup) stickyView).getChildAt(0);
    int heightMode = MeasureSpec.getMode(layoutParams.height);
    int heightSize = MeasureSpec.getSize(layoutParams.height);

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        heightMode = MeasureSpec.EXACTLY;
    }

    int maxHeight = getHeight() - getListPaddingTop() - getListPaddingBottom();
    if (heightSize > maxHeight) {
        heightSize = maxHeight;
    }
    // assuming left and right additional paddings are the same
    int ws = MeasureSpec.makeMeasureSpec(getWidth() - getListPaddingLeft() - getListPaddingRight(),
            MeasureSpec.EXACTLY);
    int hs = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
    stickyView.measure(ws, hs);
    stickyView.layout(0, 0, stickyView.getMeasuredWidth(), stickyView.getMeasuredHeight());

    // initialize shadow
    stickyViewShadow.view = stickyView;
    stickyViewShadow.position = position;
    stickyViewShadow.id = getAdapter().getItemId(position);
    stickyViewShadow.additionalIndent = childLayout.getPaddingLeft();

    stickyWrapper = stickyViewShadow;
}