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.android.yijiang.kzx.widget.tab.PagerSlidingTabStrip.java

/**
 * ????ViewgetMeasuredXXX()????/*from ww  w  .j a v  a2 s.  c om*/
 */
private View measure(View view) {
    ViewGroup.LayoutParams p = view.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    view.measure(childWidthSpec, childHeightSpec);
    return view;
}

From source file:cc.softwarefactory.lokki.android.fragments.MapViewFragment.java

public Bitmap createDrawableFromView(View view) {

    Log.e(TAG, "createDrawableFromView");
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    view.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.buildDrawingCache();//  ww w.j a  v a2 s  .c  om
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

From source file:com.aincc.libtest.activity.flip.FlipViewGroup.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
    childWidthMeasureSpec = MeasureSpec//from   w  w w . j ava2 s. co  m
            .makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), MeasureSpec.EXACTLY);
    childHeightMeasureSpec = MeasureSpec
            .makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(), MeasureSpec.EXACTLY);

    inLayout = true;
    populate();
    inLayout = false;

    final int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
        }
    }
}

From source file:com.lovejjfg.powerrefresh.PowerRefreshLayout.java

public void measureView(View v) {
    if (v == null) {
        return;/*from  ww  w. java  2s . c o  m*/
    }
    int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    v.measure(w, h);
    if (v instanceof HeaderListener && headerHeight == 0) {
        headerHeight = v.getMeasuredHeight();
    }
}

From source file:com.aincc.libtest.activity.flip.FlipViewGroup.java

@Override
public void addView(View child, int index, LayoutParams params) {
    if (inLayout) {
        addViewInLayout(child, index, params);
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    } else {//  w  w  w.ja va 2s  .  c  o  m
        super.addView(child, index, params);
    }
}

From source file:com.actionbarsherlock.internal.view.menu.MenuPopupHelper.java

private int measureContentWidth(ListAdapter adapter) {
    // Menus don't tend to be long, so this is more sane than it looks.
    int width = 0;
    View itemView = null;
    int itemType = 0;
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        final int positionType = adapter.getItemViewType(i);
        if (positionType != itemType) {
            itemType = positionType;//from  w w w .j a v  a2 s . c  o m
            itemView = null;
        }
        if (mMeasureParent == null) {
            mMeasureParent = new FrameLayout(mContext);
        }
        itemView = adapter.getView(i, itemView, mMeasureParent);
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        width = Math.max(width, itemView.getMeasuredWidth());
    }
    return width;
}

From source file:com.gu.swiperefresh.SwipeRefreshPlush.java

private void measureChild(View view) {
    if (view == null)
        return;/* w w w .j  ava  2 s .c om*/
    LayoutParams lp = view.getLayoutParams();
    int width, height;
    width = getMeasureSpec(lp.width, getWidth());
    height = getMeasureSpec(lp.height, mLoadViewController.getMaxHeight());
    view.measure(width, height);
}

From source file:android.support.v7.internal.view.menu.ActionMenuPresenter.java

public boolean flagActionItems() {
    final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
    final int itemsSize = visibleItems.size();
    int maxActions = mMaxItems;
    int widthLimit = mActionItemWidthLimit;
    final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final ViewGroup parent = (ViewGroup) mMenuView;

    int requiredItems = 0;
    int requestedItems = 0;
    int firstActionWidth = 0;
    boolean hasOverflow = false;
    for (int i = 0; i < itemsSize; i++) {
        MenuItemImpl item = visibleItems.get(i);
        if (item.requiresActionButton()) {
            requiredItems++;/*from w w  w.  j a v  a2 s  .  c om*/
        } else if (item.requestsActionButton()) {
            requestedItems++;
        } else {
            hasOverflow = true;
        }
        if (mExpandedActionViewsExclusive && item.isActionViewExpanded()) {
            // Overflow everything if we have an expanded action view and we're
            // space constrained.
            maxActions = 0;
        }
    }

    // Reserve a spot for the overflow item if needed.
    if (mReserveOverflow && (hasOverflow || requiredItems + requestedItems > maxActions)) {
        maxActions--;
    }
    maxActions -= requiredItems;

    final SparseBooleanArray seenGroups = mActionButtonGroups;
    seenGroups.clear();

    int cellSize = 0;
    int cellsRemaining = 0;
    if (mStrictWidthLimit) {
        cellsRemaining = widthLimit / mMinCellSize;
        final int cellSizeRemaining = widthLimit % mMinCellSize;
        cellSize = mMinCellSize + cellSizeRemaining / cellsRemaining;
    }

    // Flag as many more requested items as will fit.
    for (int i = 0; i < itemsSize; i++) {
        MenuItemImpl item = visibleItems.get(i);

        if (item.requiresActionButton()) {
            View v = getItemView(item, mScrapActionButtonView, parent);
            if (mScrapActionButtonView == null) {
                mScrapActionButtonView = v;
            }
            if (mStrictWidthLimit) {
                cellsRemaining -= ActionMenuView.measureChildForCells(v, cellSize, cellsRemaining, querySpec,
                        0);
            } else {
                v.measure(querySpec, querySpec);
            }
            final int measuredWidth = v.getMeasuredWidth();
            widthLimit -= measuredWidth;
            if (firstActionWidth == 0) {
                firstActionWidth = measuredWidth;
            }
            final int groupId = item.getGroupId();
            if (groupId != 0) {
                seenGroups.put(groupId, true);
            }
            item.setIsActionButton(true);
        } else if (item.requestsActionButton()) {
            // Items in a group with other items that already have an action slot
            // can break the max actions rule, but not the width limit.
            final int groupId = item.getGroupId();
            final boolean inGroup = seenGroups.get(groupId);
            boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0
                    && (!mStrictWidthLimit || cellsRemaining > 0);

            if (isAction) {
                View v = getItemView(item, mScrapActionButtonView, parent);
                if (mScrapActionButtonView == null) {
                    mScrapActionButtonView = v;
                }
                if (mStrictWidthLimit) {
                    final int cells = ActionMenuView.measureChildForCells(v, cellSize, cellsRemaining,
                            querySpec, 0);
                    cellsRemaining -= cells;
                    if (cells == 0) {
                        isAction = false;
                    }
                } else {
                    v.measure(querySpec, querySpec);
                }
                final int measuredWidth = v.getMeasuredWidth();
                widthLimit -= measuredWidth;
                if (firstActionWidth == 0) {
                    firstActionWidth = measuredWidth;
                }

                if (mStrictWidthLimit) {
                    isAction &= widthLimit >= 0;
                } else {
                    // Did this push the entire first item past the limit?
                    isAction &= widthLimit + firstActionWidth > 0;
                }
            }

            if (isAction && groupId != 0) {
                seenGroups.put(groupId, true);
            } else if (inGroup) {
                // We broke the width limit. Demote the whole group, they all overflow now.
                seenGroups.put(groupId, false);
                for (int j = 0; j < i; j++) {
                    MenuItemImpl areYouMyGroupie = visibleItems.get(j);
                    if (areYouMyGroupie.getGroupId() == groupId) {
                        // Give back the action slot
                        if (areYouMyGroupie.isActionButton()) {
                            maxActions++;
                        }
                        areYouMyGroupie.setIsActionButton(false);
                    }
                }
            }

            if (isAction) {
                maxActions--;
            }

            item.setIsActionButton(isAction);
        }
    }
    return true;
}

From source file:com.facebook.litho.MountState.java

private static void applyBoundsToMountContent(Object content, int left, int top, int right, int bottom,
        boolean force) {
    assertMainThread();//from   ww  w  .j a v  a2  s . co  m

    if (content instanceof View) {
        View view = (View) content;
        int width = right - left;
        int height = bottom - top;

        if (force || view.getMeasuredHeight() != height || view.getMeasuredWidth() != width) {
            view.measure(makeMeasureSpec(right - left, MeasureSpec.EXACTLY),
                    makeMeasureSpec(bottom - top, MeasureSpec.EXACTLY));
        }

        if (force || view.getLeft() != left || view.getTop() != top || view.getRight() != right
                || view.getBottom() != bottom) {
            view.layout(left, top, right, bottom);
        }
    } else if (content instanceof Drawable) {
        ((Drawable) content).setBounds(left, top, right, bottom);
    } else {
        throw new IllegalStateException("Unsupported mounted content " + content);
    }
}

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

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
    if (mInLayout) {
        addViewInLayout(child, index, params);
        child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
    } else {//  w  w  w . j  a  va  2  s .  c  om
        super.addView(child, index, params);
    }

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