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.aviary.android.feather.sdk.widget.AviaryWorkspace.java

private void setUpChild(View child, int offset, int x, boolean fromLeft) {

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

    addViewInLayout(child, fromLeft ? -1 : 0, lp);

    if (mAllowChildSelection) {
        // final boolean wantfocus = offset == 0;
        // child.setSelected( wantfocus );
        // if( wantfocus ){
        // child.requestFocus();
        // }
    }

    // Get measure specs
    int childHeightSpec = ViewGroup.getChildMeasureSpec(mHeightMeasureSpec, mPaddingTop + mPaddingBottom,
            lp.height);
    int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, mPaddingLeft + mPaddingRight,
            lp.width);

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

    int childLeft;
    int childRight;

    // Position vertically based on gravity setting
    int childTop = calculateTop(child, true);
    int childBottom = childTop + child.getMeasuredHeight();

    int width = child.getMeasuredWidth();
    if (fromLeft) {
        childLeft = x;
        childRight = childLeft + width;
    } else {
        childLeft = x - width;
        childRight = x;
    }

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

}

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (!mFillViewport) {
        return;/*  w  ww  .j a va 2  s.co m*/
    }

    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if (heightMode == MeasureSpec.UNSPECIFIED) {
        return;
    }

    if (getChildCount() > 0) {
        final View child = getChildAt(0);
        int height = getMeasuredHeight();
        if (child.getMeasuredHeight() < height) {
            final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                    getPaddingLeft() + getPaddingRight(), lp.width);
            height -= getPaddingTop();
            height -= getPaddingBottom();
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

From source file:com.cmbb.smartmarket.widget.NestedScrollView.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (!mFillViewport) {
        return;//from   w  w  w .java 2s .  c  o m
    }

    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if (heightMode == MeasureSpec.UNSPECIFIED) {
        return;
    }

    if (getChildCount() > 0) {
        final View child = getChildAt(0);
        int height = getMeasuredHeight();
        if (child.getMeasuredHeight() < height) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                    getPaddingLeft() + getPaddingRight(), lp.width);
            height -= getPaddingTop();
            height -= getPaddingBottom();
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

From source file:com.chenglong.muscle.ui.LazyViewPager.java

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
    if (!checkLayoutParams(params)) {
        params = generateLayoutParams(params);
    }//www .j  a v  a  2 s  .  c om
    final LayoutParams lp = (LayoutParams) params;
    lp.isDecor |= child instanceof Decor;
    if (mInLayout) {
        if (lp != null && lp.isDecor) {
            throw new IllegalStateException("Cannot add pager decor view during layout");
        }
        addViewInLayout(child, index, params);
        child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
    } else {
        super.addView(child, index, params);
    }
    if (USE_CACHE) {
        if (child.getVisibility() != GONE) {
            child.setDrawingCacheEnabled(mScrollingCacheEnabled);
        } else {
            child.setDrawingCacheEnabled(false);
        }
    }
}

From source file:com.example.verticaldrawerlayout.VerticalDrawerLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if
            // specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.AT_MOST) {
                widthMode = MeasureSpec.EXACTLY;
            } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = MeasureSpec.EXACTLY;
                widthSize = 300;//from   www.j a  va 2 s.  c o m
            }
            if (heightMode == MeasureSpec.AT_MOST) {
                heightMode = MeasureSpec.EXACTLY;
            } else if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = MeasureSpec.EXACTLY;
                heightSize = 300;
            }
        } else {
            throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }
    }

    setMeasuredDimension(widthSize, heightSize);

    // Gravity value for each drawer we've seen. Only one of each permitted.
    int foundDrawers = 0;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }

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

        if (isContentView(child)) {
            // Content views get measured at exactly the layout's size.
            final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin,
                    MeasureSpec.EXACTLY);
            final int contentHeightSpec = MeasureSpec
                    .makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
            child.measure(contentWidthSpec, contentHeightSpec);
        } else if (isDrawerView(child)) {
            final int childGravity = getDrawerViewAbsoluteGravity(child) & Gravity.VERTICAL_GRAVITY_MASK;
            if ((foundDrawers & childGravity) != 0) {
                throw new IllegalStateException(
                        "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this "
                                + TAG + " already has a " + "drawer view along that edge");
            }
            final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, lp.leftMargin + lp.rightMargin,
                    lp.width);
            final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                    mMinDrawerMargin + lp.topMargin + lp.bottomMargin, lp.height);
            child.measure(drawerWidthSpec, drawerHeightSpec);
        } else {
            throw new IllegalStateException("Child " + child + " at index " + i
                    + " does not have a valid layout_gravity - must be Gravity.LEFT, "
                    + "Gravity.RIGHT or Gravity.NO_GRAVITY");
        }
    }
}

From source file:com.hippo.widget.BothScrollView.java

@Override
protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
    ViewGroup.LayoutParams lp = child.getLayoutParams();

    int childWidthMeasureSpec;
    int childHeightMeasureSpec;

    childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

From source file:com.android.widget.SlidingPaneLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (heightMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (heightMode == MeasureSpec.AT_MOST) {
                heightMode = MeasureSpec.EXACTLY;
            } else if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = 300;/*from  ww  w.  j av  a  2 s .c o m*/
            }
        } else {
            throw new IllegalStateException("Height must have an exact value or MATCH_PARENT");
        }
    } else if (widthMode == MeasureSpec.UNSPECIFIED) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Pick a magic number from thin air instead.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = 300;
            }
        } else {
            throw new IllegalStateException("Width must not be UNSPECIFIED");
        }
    }

    int layoutWidth = 0;
    int maxLayoutWidth = -1;
    switch (widthMode) {
    case MeasureSpec.EXACTLY:
        layoutWidth = maxLayoutWidth = widthSize - getPaddingLeft() - getPaddingRight();
        break;
    case MeasureSpec.AT_MOST:
        maxLayoutWidth = widthSize - getPaddingLeft() - getPaddingRight();
        break;
    }

    boolean canSlide = false;
    final int heightAvailable = heightSize - getPaddingTop() - getPaddingBottom();
    final int childCount = getChildCount();

    if (childCount > 2) {
        Log.e(TAG, "onMeasure: More than two child views are not supported.");
    }

    // We'll find the current one below.
    mSlideableView = null;

    // First pass. Measure based on child LayoutParams width/height.
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        if (child.getVisibility() == GONE) {
            continue;
        }

        int childHeightSpec;
        final int verticalMargin = lp.topMargin + lp.bottomMargin;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(heightAvailable - verticalMargin,
                    MeasureSpec.AT_MOST);
        } else if (lp.height == LayoutParams.MATCH_PARENT) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(heightAvailable - verticalMargin,
                    MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }

        int childWidthSpec;
        if (lp.width == LayoutParams.WRAP_CONTENT) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(maxLayoutWidth, MeasureSpec.AT_MOST);
        } else if (lp.width == LayoutParams.MATCH_PARENT) {
            childWidthSpec = MeasureSpec.makeMeasureSpec(maxLayoutWidth, MeasureSpec.EXACTLY);
        } else {
            childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
        }

        child.measure(childWidthSpec, childHeightSpec);
        final int childWidth = child.getMeasuredWidth();

        if (widthMode == MeasureSpec.AT_MOST && childWidth > layoutWidth) {
            layoutWidth = Math.min(childWidth, maxLayoutWidth);
        }

        if (i == childCount - 1) {
            canSlide |= lp.slideable = true;
            mSlideableView = child;
        }
    }

    final int measuredHeight = heightSize;
    final int measuredWidth = layoutWidth + getPaddingLeft() + getPaddingRight();

    setMeasuredDimension(measuredWidth, measuredHeight);
    mCanSlide = canSlide;

    if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && !canSlide) {
        // Cancel scrolling in progress, it's no longer relevant.
        mDragHelper.abort();
    }
}

From source file:com.dk.view.FolderDrawerLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
        if (isInEditMode()) {
            // Don't crash the layout editor. Consume all of the space if
            // specified
            // or pick a magic number from thin air otherwise.
            // TODO Better communication with tools of this bogus state.
            // It will crash on a real device.
            if (widthMode == MeasureSpec.AT_MOST) {
                widthMode = MeasureSpec.EXACTLY;
            } else if (widthMode == MeasureSpec.UNSPECIFIED) {
                widthMode = MeasureSpec.EXACTLY;
                widthSize = 300;//ww  w . j a va 2  s.  c om
            }
            if (heightMode == MeasureSpec.AT_MOST) {
                heightMode = MeasureSpec.EXACTLY;
            } else if (heightMode == MeasureSpec.UNSPECIFIED) {
                heightMode = MeasureSpec.EXACTLY;
                heightSize = 300;
            }
        } else {
            throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
        }
    }

    setMeasuredDimension(widthSize, heightSize);

    // Gravity value for each drawer we've seen. Only one of each permitted.
    int foundDrawers = 0;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }

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

        if (isContentView(child)) {
            // Content views get measured at exactly the layout's size.
            final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin,
                    MeasureSpec.EXACTLY);
            final int contentHeightSpec = MeasureSpec
                    .makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
            child.measure(contentWidthSpec, contentHeightSpec);
        } else if (isDrawerView(child)) {
            final int childGravity = getDrawerViewAbsoluteGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
            if ((foundDrawers & childGravity) != 0) {
                throw new IllegalStateException(
                        "Child drawer has absolute gravity " + gravityToString(childGravity) + " but this "
                                + TAG + " already has a " + "drawer view along that edge");
            }
            final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                    mMinDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
            final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin,
                    lp.height);
            child.measure(drawerWidthSpec, drawerHeightSpec);
        } else {
            throw new IllegalStateException("Child " + child + " at index " + i
                    + " does not have a valid layout_gravity - must be Gravity.LEFT, "
                    + "Gravity.RIGHT or Gravity.NO_GRAVITY");
        }
    }
}

From source file:com.comcast.freeflow.core.FreeFlowContainer.java

/**
 * Adds a view based on the current viewport. If we can get a view from the
 * ViewPool, we dont need to construct a new instance, else we will based on
 * the View class returned by the <code>Adapter</code>
 * //from  w  ww.  j  a v  a 2s  .  c o m
 * @param freeflowItem
 *            <code>FreeFlowItem</code> instance that determines the View
 *            being positioned
 */
protected void addAndMeasureViewIfNeeded(FreeFlowItem freeflowItem) {
    View view;
    if (freeflowItem.view == null) {

        View convertView = viewpool.getViewFromPool(itemAdapter.getViewType(freeflowItem));

        if (freeflowItem.isHeader) {
            view = itemAdapter.getHeaderViewForSection(freeflowItem.itemSection, convertView, this);
        } else {
            view = itemAdapter.getItemView(freeflowItem.itemSection, freeflowItem.itemIndex, convertView, this);
        }

        if (view instanceof FreeFlowContainer)
            throw new IllegalStateException("A container cannot be a direct child view to a container");

        freeflowItem.view = view;
        prepareViewForAddition(view, freeflowItem);
        addView(view, getChildCount(), params);
    }

    view = freeflowItem.view;

    int widthSpec = MeasureSpec.makeMeasureSpec(freeflowItem.frame.width(), MeasureSpec.EXACTLY);
    int heightSpec = MeasureSpec.makeMeasureSpec(freeflowItem.frame.height(), MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
}

From source file:com.hippo.widget.BothScrollView.java

@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
        int parentHeightMeasureSpec, int heightUsed) {
    final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

    final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.topMargin + lp.bottomMargin,
            MeasureSpec.UNSPECIFIED);/*ww  w.  jav  a  2s  .c om*/
    final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin,
            MeasureSpec.UNSPECIFIED);

    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}