Example usage for android.view View getMeasuredWidth

List of usage examples for android.view View getMeasuredWidth

Introduction

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

Prototype

public final int getMeasuredWidth() 

Source Link

Document

Like #getMeasuredWidthAndState() , but only returns the raw width component (that is the result is masked by #MEASURED_SIZE_MASK ).

Usage

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

/**
 * Crate a bitmap from the given view//ww  w .  j  ava  2s.  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.mail.browse.ConversationContainer.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    LogUtils.d(TAG, "*** IN header container onLayout");

    for (View nonScrollingChild : mNonScrollingChildren) {
        if (nonScrollingChild.getVisibility() != GONE) {
            final int w = nonScrollingChild.getMeasuredWidth();
            final int h = nonScrollingChild.getMeasuredHeight();

            final MarginLayoutParams lp = (MarginLayoutParams) nonScrollingChild.getLayoutParams();

            final int childLeft = lp.leftMargin;
            final int childTop = lp.topMargin;
            nonScrollingChild.layout(childLeft, childTop, childLeft + w, childTop + h);
        }/*from   ww w  .java2  s.co m*/
    }

    if (mOverlayAdapter != null) {
        // being in a layout pass means overlay children may require measurement,
        // so invalidate them
        for (int i = 0, len = mOverlayAdapter.getCount(); i < len; i++) {
            mOverlayAdapter.getItem(i).invalidateMeasurement();
        }
    }

    positionOverlays(mOffsetY, false /* postAddView */);
}

From source file:com.adherence.adherence.SwipeRevealLayout.java

/**
 * {@inheritDoc}//from   w ww. jav a 2s  .  c  om
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (getChildCount() < 2) {
        throw new RuntimeException("Layout must have two children");
    }

    final LayoutParams params = getLayoutParams();

    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int desiredWidth = 0;
    int desiredHeight = 0;

    // first find the largest child
    for (int i = 0; i < getChildCount(); i++) {
        final View child = getChildAt(i);
        measureChild(child, widthMeasureSpec, heightMeasureSpec);
        desiredWidth = Math.max(child.getMeasuredWidth(), desiredWidth);
        desiredHeight = Math.max(child.getMeasuredHeight(), desiredHeight);
    }
    // create new measure spec using the largest child width
    widthMeasureSpec = MeasureSpec.makeMeasureSpec(desiredWidth, widthMode);
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(desiredHeight, heightMode);

    final int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
    final int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);

    for (int i = 0; i < getChildCount(); i++) {
        final View child = getChildAt(i);
        final LayoutParams childParams = child.getLayoutParams();

        if (childParams != null) {
            if (childParams.height == LayoutParams.MATCH_PARENT) {
                child.setMinimumHeight(measuredHeight);
            }

            if (childParams.width == LayoutParams.MATCH_PARENT) {
                child.setMinimumWidth(measuredWidth);
            }
        }

        measureChild(child, widthMeasureSpec, heightMeasureSpec);
        desiredWidth = Math.max(child.getMeasuredWidth(), desiredWidth);
        desiredHeight = Math.max(child.getMeasuredHeight(), desiredHeight);
    }

    // taking accounts of padding
    desiredWidth += getPaddingLeft() + getPaddingRight();
    desiredHeight += getPaddingTop() + getPaddingBottom();

    // adjust desired width
    if (widthMode == MeasureSpec.EXACTLY) {
        desiredWidth = measuredWidth;
    } else {
        if (params.width == LayoutParams.MATCH_PARENT) {
            desiredWidth = measuredWidth;
        }

        if (widthMode == MeasureSpec.AT_MOST) {
            desiredWidth = (desiredWidth > measuredWidth) ? measuredWidth : desiredWidth;
        }
    }

    // adjust desired height
    if (heightMode == MeasureSpec.EXACTLY) {
        desiredHeight = measuredHeight;
    } else {
        if (params.height == LayoutParams.MATCH_PARENT) {
            desiredHeight = measuredHeight;
        }

        if (heightMode == MeasureSpec.AT_MOST) {
            desiredHeight = (desiredHeight > measuredHeight) ? measuredHeight : desiredHeight;
        }
    }

    setMeasuredDimension(desiredWidth, desiredHeight);
}

From source file:com.android.launcher3.folder.FolderIcon.java

public void performDestroyAnimation(final View finalView, Runnable onCompleteRunnable) {
    Drawable animateDrawable = getTopDrawable((TextView) finalView);
    computePreviewDrawingParams(animateDrawable.getIntrinsicWidth(), finalView.getMeasuredWidth());

    // This will animate the first item from it's position as an icon into its
    // position as the first item in the preview
    animateFirstItem(animateDrawable, FINAL_ITEM_ANIMATION_DURATION, true, onCompleteRunnable);
}

From source file:com.gome.haoyuangong.views.SwipeRefreshLayout.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int width = getMeasuredWidth();
    final int height = getMeasuredHeight();
    if (getChildCount() == 0) {
        return;//  www.  ja v a 2  s.  c  o m
    }
    final View child0 = getChildAt(0);
    final View child = getChildAt(1);
    final int childLeft = getPaddingLeft();
    final int childTop = mCurrentTargetOffsetTop + getPaddingTop();
    final int childWidth = width - getPaddingLeft() - getPaddingRight();
    final int childHeight = height - getPaddingTop() - getPaddingBottom();
    child0.layout(childLeft, getPaddingTop(), childLeft + child0.getMeasuredWidth(), childTop + mHeaderHeight);
    child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
}

From source file:com.aibasis.parent.widget.PagerSlidingTabStrip.java

/**
 * views?ViewView?parentViewWidth//  ww w.j a v a 2 s.  c  o  m
 * 
 * @param views
 *            ?View?
 * @param parentViewWidth
 *            Vie
 * @param parentWidthMeasureSpec
 *            View
 * @param parentHeightMeasureSpec
 *            View
 */
private void adjustChildWidthWithParent(List<View> views, int parentViewWidth, int parentWidthMeasureSpec,
        int parentHeightMeasureSpec) {
    // ?View?
    for (View view : views) {
        if (view.getLayoutParams() instanceof MarginLayoutParams) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
            parentViewWidth -= lp.leftMargin + lp.rightMargin;
        }
    }

    // ?View???
    int averageWidth = parentViewWidth / views.size();
    int bigTabCount = views.size();
    while (true) {
        Iterator<View> iterator = views.iterator();
        while (iterator.hasNext()) {
            View view = iterator.next();
            if (view.getMeasuredWidth() > averageWidth) {
                parentViewWidth -= view.getMeasuredWidth();
                bigTabCount--;
                iterator.remove();
            }
        }
        averageWidth = parentViewWidth / bigTabCount;
        boolean end = true;
        for (View view : views) {
            if (view.getMeasuredWidth() > averageWidth) {
                end = false;
            }
        }
        if (end) {
            break;
        }
    }

    // ??View
    for (View view : views) {
        if (view.getMeasuredWidth() < averageWidth) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.width = averageWidth;
            view.setLayoutParams(layoutParams);
            // ??
            if (layoutParams instanceof MarginLayoutParams) {
                measureChildWithMargins(view, parentWidthMeasureSpec, 0, parentHeightMeasureSpec, 0);
            } else {
                measureChild(view, parentWidthMeasureSpec, parentHeightMeasureSpec);
            }
        }
    }
}

From source file:com.cicada.startup.common.ui.view.indicator.ViewPagerIndicator.java

/**
 * views?ViewView?parentViewWidth/*from   w  ww  .  jav  a  2  s .com*/
 *
 * @param views                   ?View?
 * @param parentViewWidth         Vie
 * @param parentWidthMeasureSpec  View
 * @param parentHeightMeasureSpec View
 */
private void adjustChildWidthWithParent(List<View> views, int parentViewWidth, int parentWidthMeasureSpec,
        int parentHeightMeasureSpec) {
    // ?View?
    for (View view : views) {
        if (view.getLayoutParams() instanceof MarginLayoutParams) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
            parentViewWidth -= lp.leftMargin + lp.rightMargin;
        }
    }

    // ?View???
    int tabCount = views.size();
    int averageWidth = parentViewWidth / tabCount;
    while (true) {
        Iterator<View> iterator = views.iterator();
        while (iterator.hasNext()) {
            View view = iterator.next();
            if (view.getMeasuredWidth() > averageWidth) {
                parentViewWidth -= view.getMeasuredWidth();
                tabCount--;
                iterator.remove();
            }
        }
        averageWidth = parentViewWidth / tabCount;
        boolean end = true;
        for (View view : views) {
            if (view.getMeasuredWidth() > averageWidth) {
                end = false;
            }
        }
        if (end) {
            break;
        }
    }

    // ??View
    for (View view : views) {
        if (view.getMeasuredWidth() < averageWidth) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
            layoutParams.width = averageWidth;
            view.setLayoutParams(layoutParams);
            // ??
            if (layoutParams instanceof MarginLayoutParams) {
                measureChildWithMargins(view, parentWidthMeasureSpec, 0, parentHeightMeasureSpec, 0);
            } else {
                measureChild(view, parentWidthMeasureSpec, parentHeightMeasureSpec);
            }
        }
    }
}

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;//  w w w .jav a  2  s.  co 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:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int width = getWidth();

    int ypos = mTopOffset;
    int leftEdge = getPaddingLeft();
    int rightEdge = width - getPaddingRight();

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        if (child.getVisibility() == GONE) {
            continue;
        }// www  .ja  v a 2s .c  o m

        int top = ypos + lp.topMargin;
        if (lp.ignoreOffset) {
            top -= mCollapseOffset;
        }
        final int bottom = top + child.getMeasuredHeight();

        final int childWidth = child.getMeasuredWidth();
        final int widthAvailable = rightEdge - leftEdge;
        final int left = leftEdge + (widthAvailable - childWidth) / 2;
        final int right = left + childWidth;

        child.layout(left, top, right, bottom);

        ypos = bottom + lp.bottomMargin;
    }

    mIsLaidOut = true;
}

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

/**
 * {@inheritDoc}/*w w  w .jav a  2  s . c om*/
 */
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int count = getChildCount();

    final int parentLeft = getPaddingLeft();
    final int parentWidth = right - left - parentLeft - getPaddingRight();

    int childTop = getPaddingTop();

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == View.GONE) {
            continue;
        }

        final int width = child.getMeasuredWidth();
        final int height = child.getMeasuredHeight();

        int delta = (parentWidth - width) / 2;
        int childLeft = parentLeft + delta;

        child.layout(childLeft, childTop, childLeft + width, childTop + height);

        childTop += height;
    }
}