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.jzh.stuapp.view.MyViewPager.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

    mChildWidthMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY);

    mInLayout = true;//  w  w  w  .  j  a  va 2 s.  co m
    populate();
    mInLayout = false;

    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);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

From source file:com.canace.mybaby.view.ImageViewPager.java

@Override
public void addView(View child, int index, LayoutParams params) {
    if (mInLayout) {
        addViewInLayout(child, index, params);
        child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);

    } else {//from  www .jav  a2 s  . c o  m
        super.addView(child, index, params);
    }

    if (USE_CACHE) {
        if (child.getVisibility() != GONE) {
            child.setDrawingCacheEnabled(mScrollingCacheEnabled);
        } else {
            child.setDrawingCacheEnabled(false);
        }
    }
}

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

/**
 * Helper for makeAndAddView to set the position of a view and fill out its layout paramters.
 *
 * @param child    The view to position/*from   w  ww  .ja  v a 2s  . com*/
 * @param addChild true if the child should be added to the Spinner during setup
 */
private void setUpChild(View child, boolean addChild) {

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

    if (addChild) {
        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:bw.com.yunifangstore.view.LazyViewPager.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;//  w  w  w . j  ava  2  s. com
    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);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

From source file:com.example.show.NoPreloadViewPager.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 w  w.  ja 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);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

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

private int compatMeasureContentWidth(SpinnerAdapter adapter, Drawable background) {
    if (adapter == null) {
        return 0;
    }/*from  w  w  w .j  a  v  a 2  s.  c o m*/

    int width = 0;
    View itemView = null;
    int itemType = 0;
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.UNSPECIFIED);

    // Make sure the number of items we'll measure is capped. If it's a huge data set
    // with wildly varying sizes, oh well.
    int start = Math.max(0, getSelectedItemPosition());
    final int end = Math.min(adapter.getCount(), start + MAX_ITEMS_MEASURED);
    final int count = end - start;
    start = Math.max(0, start - (MAX_ITEMS_MEASURED - count));
    for (int i = start; i < end; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;
            itemView = null;
        }
        itemView = adapter.getView(i, itemView, this);
        if (itemView.getLayoutParams() == null) {
            itemView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        width = Math.max(width, itemView.getMeasuredWidth());
    }

    // Add background padding to measured width
    if (background != null) {
        background.getPadding(mTempRect);
        width += mTempRect.left + mTempRect.right;
    }

    return width;
}

From source file:com.google.android.apps.gutenberg.widget.TabLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // If we have a MeasureSpec which allows us to decide our height, try and use the default
    // height//from  w w  w  . j a v a2  s .  com
    switch (MeasureSpec.getMode(heightMeasureSpec)) {
    case MeasureSpec.AT_MOST:
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                Math.min(dpToPx(DEFAULT_HEIGHT), MeasureSpec.getSize(heightMeasureSpec)), MeasureSpec.EXACTLY);
        break;
    case MeasureSpec.UNSPECIFIED:
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(dpToPx(DEFAULT_HEIGHT), MeasureSpec.EXACTLY);
        break;
    }

    // Now super measure itself using the (possibly) modified height spec
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (mMode == MODE_FIXED && getChildCount() == 1) {
        // If we're in fixed mode then we need to make the tab strip is the same width as us
        // so we don't scroll
        final View child = getChildAt(0);
        final int width = getMeasuredWidth();

        if (child.getMeasuredWidth() > width) {
            // If the child is wider than us, re-measure it with a widthSpec set to exact our
            // measure width
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                    getPaddingTop() + getPaddingBottom(), child.getLayoutParams().height);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }

    // Now update the tab max width. We do it here as the default tab min width is
    // layout width - 56dp
    int maxTabWidth = mRequestedTabMaxWidth;
    final int defaultTabMaxWidth = getMeasuredWidth() - dpToPx(TAB_MIN_WIDTH_MARGIN);
    if (maxTabWidth == 0 || maxTabWidth > defaultTabMaxWidth) {
        // If the request tab max width is 0, or larger than our default, use the default
        maxTabWidth = defaultTabMaxWidth;
    }
    mTabMaxWidth = maxTabWidth;
}

From source file:com.cnpeng.cnpeng_mydemosfrom2016_12.a_12_GetLocalFiles_VP_FM.CustomNoPreLoadViewPager.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 ww  w  .  j  a  v  a  2 s  .  c om
    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);
            }
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

From source file:com.lin.yuantiku.ui.customview.CustomViewPager.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 item_task_unfinish 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 item_task_unfinish 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   ww  w .  java  2s . com*/
    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);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}

From source file:com.bm.wjsj.View.LazyViewPager.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  w  w. j  a va  2  s .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);
            child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
        }
    }
}