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:com.android.leanlauncher.LauncherTransitionable.java

/**
 * Zoom the camera back into the workspace, hiding 'fromView'.
 * This is the opposite of showAppsCustomizeHelper.
 *
 * @param animated If true, the transition will be animated.
 *//*from ww w .j a v a 2 s . co m*/
private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
        final boolean springLoaded, final Runnable onCompleteRunnable) {

    if (mStateAnimation != null) {
        mStateAnimation.setDuration(0);
        mStateAnimation.cancel();
        mStateAnimation = null;
    }

    boolean material = Utilities.isLmpOrAbove();
    Resources res = getResources();

    final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
    final int itemsAlphaStagger = res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);

    final View fromView = mAppsCustomizeTabHost;
    final View toView = mWorkspace;
    Animator workspaceAnim = null;
    final ArrayList<View> layerViews = new ArrayList<View>();

    if (toState == Workspace.State.NORMAL) {
        workspaceAnim = mWorkspace.getChangeStateAnimation(toState, animated, layerViews);
    } else if (toState == Workspace.State.SPRING_LOADED || toState == Workspace.State.OVERVIEW) {
        workspaceAnim = mWorkspace.getChangeStateAnimation(toState, animated, layerViews);
    }

    // If for some reason our views aren't initialized, don't animate
    boolean initialized = getAllAppsButton() != null;

    if (animated && initialized) {
        mStateAnimation = LauncherAnimUtils.createAnimatorSet();
        if (workspaceAnim != null) {
            mStateAnimation.play(workspaceAnim);
        }

        final AppsCustomizePagedView content = (AppsCustomizePagedView) fromView
                .findViewById(R.id.apps_customize_pane_content);

        final View page = content.getPageAt(content.getNextPage());

        // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
        int count = content.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = content.getChildAt(i);
            if (child != page) {
                child.setVisibility(View.INVISIBLE);
            }
        }
        final View revealView = fromView.findViewById(R.id.fake_page);

        // hideAppsCustomizeHelper is called in some cases when it is already hidden
        // don't perform all these no-op animations. In particularly, this was causing
        // the all-apps button to pop in and out.
        if (fromView.getVisibility() == View.VISIBLE) {
            AppsCustomizePagedView.ContentType contentType = content.getContentType();
            final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;

            revealView.setBackgroundColor(getResources().getColor(R.color.widget_text_panel));

            int width = revealView.getMeasuredWidth();
            int height = revealView.getMeasuredHeight();
            float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);

            // Hide the real page background, and swap in the fake one
            revealView.setVisibility(View.VISIBLE);
            content.setPageBackgroundsVisible(false);

            final View allAppsButton = getAllAppsButton();
            revealView.setTranslationY(0);
            int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView, allAppsButton, null);

            float xDrift = 0;
            float yDrift = 0;
            if (material) {
                yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
                xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
            } else {
                yDrift = 2 * height / 3;
                xDrift = 0;
            }

            revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            TimeInterpolator decelerateInterpolator = material ? new LogDecelerateInterpolator(100, 0)
                    : new DecelerateInterpolator(1f);

            // The vertical motion of the apps panel should be delayed by one frame
            // from the conceal animation in order to give the right feel. We correpsondingly
            // shorten the duration so that the slide and conceal end at the same time.
            ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY", 0, yDrift);
            panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
            panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
            panelDriftY.setInterpolator(decelerateInterpolator);
            mStateAnimation.play(panelDriftY);

            ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX", 0, xDrift);
            panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
            panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
            panelDriftX.setInterpolator(decelerateInterpolator);
            mStateAnimation.play(panelDriftX);

            if (isWidgetTray || !material) {
                float finalAlpha = material ? 0.4f : 0f;
                revealView.setAlpha(1f);
                ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha", 1f, finalAlpha);
                panelAlpha.setDuration(material ? revealDuration : 150);
                panelAlpha.setInterpolator(decelerateInterpolator);
                panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
                mStateAnimation.play(panelAlpha);
            }

            if (page != null) {
                page.setLayerType(View.LAYER_TYPE_HARDWARE, null);

                ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY", 0, yDrift);
                page.setTranslationY(0);
                pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
                pageDrift.setInterpolator(decelerateInterpolator);
                pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
                mStateAnimation.play(pageDrift);

                page.setAlpha(1f);
                ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
                itemsAlpha.setDuration(100);
                itemsAlpha.setInterpolator(decelerateInterpolator);
                mStateAnimation.play(itemsAlpha);
            }

            View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
            pageIndicators.setAlpha(1f);
            ObjectAnimator indicatorsAlpha = LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
            indicatorsAlpha.setDuration(revealDuration);
            indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
            mStateAnimation.play(indicatorsAlpha);

            width = revealView.getMeasuredWidth();

            if (material) {
                if (!isWidgetTray) {
                    allAppsButton.setVisibility(View.INVISIBLE);
                }
                int allAppsButtonSize = LauncherAppState.getInstance().getDynamicGrid()
                        .getDeviceProfile().allAppsButtonVisualSize;
                float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
                Animator reveal = LauncherAnimUtils.createCircularReveal(revealView, width / 2, height / 2,
                        revealRadius, finalRadius);
                reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
                reveal.setDuration(revealDuration);
                reveal.setStartDelay(itemsAlphaStagger);

                reveal.addListener(new AnimatorListenerAdapter() {
                    public void onAnimationEnd(Animator animation) {
                        revealView.setVisibility(View.INVISIBLE);
                        if (!isWidgetTray) {
                            allAppsButton.setVisibility(View.VISIBLE);
                        }
                    }
                });

                mStateAnimation.play(reveal);
            }

            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
            dispatchOnLauncherTransitionPrepare(toView, animated, true);
            mAppsCustomizeContent.stopScrolling();
        }

        mStateAnimation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                fromView.setVisibility(View.GONE);
                dispatchOnLauncherTransitionEnd(fromView, animated, true);
                dispatchOnLauncherTransitionEnd(toView, animated, true);
                if (onCompleteRunnable != null) {
                    onCompleteRunnable.run();
                }

                revealView.setLayerType(View.LAYER_TYPE_NONE, null);
                if (page != null) {
                    page.setLayerType(View.LAYER_TYPE_NONE, null);
                }
                content.setPageBackgroundsVisible(true);
                // Unhide side pages
                int count = content.getChildCount();
                for (int i = 0; i < count; i++) {
                    View child = content.getChildAt(i);
                    child.setVisibility(View.VISIBLE);
                }

                // Reset page transforms
                if (page != null) {
                    page.setTranslationX(0);
                    page.setTranslationY(0);
                    page.setAlpha(1);
                }
                content.setCurrentPage(content.getNextPage());

                mAppsCustomizeContent.updateCurrentPageScroll();

                // This can hold unnecessary references to views.
                mStateAnimation = null;
            }
        });

        final AnimatorSet stateAnimation = mStateAnimation;
        final Runnable startAnimRunnable = new Runnable() {
            public void run() {
                // Check that mStateAnimation hasn't changed while
                // we waited for a layout/draw pass
                if (mStateAnimation != stateAnimation)
                    return;
                dispatchOnLauncherTransitionStart(fromView, animated, false);
                dispatchOnLauncherTransitionStart(toView, animated, false);

                if (Utilities.isLmpOrAbove()) {
                    for (int i = 0; i < layerViews.size(); i++) {
                        View v = layerViews.get(i);
                        if (v != null) {
                            if (Utilities.isViewAttachedToWindow(v))
                                v.buildLayer();
                        }
                    }
                }
                mStateAnimation.start();
            }
        };
        fromView.post(startAnimRunnable);
    } else {
        fromView.setVisibility(View.GONE);
        dispatchOnLauncherTransitionPrepare(fromView, animated, true);
        dispatchOnLauncherTransitionStart(fromView, animated, true);
        dispatchOnLauncherTransitionEnd(fromView, animated, true);
        dispatchOnLauncherTransitionPrepare(toView, animated, true);
        dispatchOnLauncherTransitionStart(toView, animated, true);
        dispatchOnLauncherTransitionEnd(toView, animated, true);
    }
}

From source file:com.example.sky.test.view.ViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollX = getScrollX();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final ViewPager.LayoutParams lp = (ViewPager.LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }//w  w  w.  jav  a 2 s .  co m
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childLeft += scrollX;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    final int childWidth = width - paddingLeft - paddingRight;
    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final ViewPager.LayoutParams lp = (ViewPager.LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (childWidth * ii.offset);
                int childLeft = paddingLeft + loff;
                int childTop = paddingTop;
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidth * lp.widthFactor),
                            MeasureSpec.EXACTLY);
                    final int heightSpec = MeasureSpec
                            .makeMeasureSpec((int) (height - paddingTop - paddingBottom), MeasureSpec.EXACTLY);
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG) {
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                }
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mTopPageBounds = paddingTop;
    mBottomPageBounds = height - paddingBottom;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

From source file:com.example.mvpdemo.widget.NarrowParentViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    Log.i("xx", "================= onLayout =================");

    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollX = getScrollX();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }/*from  w  ww . ja v a  2 s  .c  o m*/
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childLeft += scrollX;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    final int childWidth = width - paddingLeft - paddingRight - 2 * (OFFSET);
    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE && (child == mCurrent && mDragDistance == 0 || child != mCurrent)) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {

                Log.i("Narrow", "INDEX = " + i + "   offset = " + ii.offset);

                int loff = (int) (childWidth * ii.offset);
                int childLeft = paddingLeft + loff;
                int childTop = paddingTop;
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    final int widthSpec = MeasureSpec.makeMeasureSpec((int) (childWidth * lp.widthFactor),
                            MeasureSpec.EXACTLY);
                    final int heightSpec = MeasureSpec
                            .makeMeasureSpec((int) (height - paddingTop - paddingBottom), MeasureSpec.EXACTLY);
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft + OFFSET + PADDING,
                        childTop + (int) ((OFFSET + PADDING) * (float) getHeight() / getWidth()),
                        (int) (childLeft + child.getMeasuredWidth()) + OFFSET + PADDING,
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mTopPageBounds = paddingTop;
    mBottomPageBounds = height - paddingBottom;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

From source file:com.example.view.VerticalViewPager.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.
    int childWidthSize = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();

    /*/*ww w  .  j  a  v  a2  s .  co m*/
     * Make sure all children have been properly measured. Decor views
     * first. Right now we cheat and make this less complicated by assuming
     * decor views won't intersect. We will pin to edges based on gravity.
     */
    int size = getChildCount();
    for (int i = 0; i < size; ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp != null && lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                Log.d(TAG, "gravity: " + lp.gravity + " hgrav: " + hgrav + " vgrav: " + vgrav);
                int widthMode = MeasureSpec.AT_MOST;
                int heightMode = MeasureSpec.AT_MOST;
                boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
                boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;

                if (consumeVertical) {
                    widthMode = MeasureSpec.EXACTLY;
                } else if (consumeHorizontal) {
                    heightMode = MeasureSpec.EXACTLY;
                } /* end of if */

                final int widthSpec = MeasureSpec.makeMeasureSpec(childWidthSize, widthMode);
                final int heightSpec = MeasureSpec.makeMeasureSpec(childHeightSize, heightMode);
                child.measure(widthSpec, heightSpec);

                if (consumeVertical) {
                    childHeightSize -= child.getMeasuredHeight();
                } else if (consumeHorizontal) {
                    childWidthSize -= child.getMeasuredWidth();
                } /* end of if */
            } /* end of if */
        } /* end of if */
    } /* end of for */

    mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
    mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);

    // Make sure we have created all fragments that we need to have shown.
    mInLayout = true;
    populate();
    mInLayout = false;

    // Page views next.
    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);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (lp == null || !lp.isDecor) {
                child.measure(mChildWidthMeasureSpec, mChildHeightMeasureSpec);
            } /* end of if */
        } /* end of if */
    } /* end of for */
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int childCount = getChildCount();
    if (mIsCollapsable) {
        int visibleChildren = 0;
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE && !(child == mMenuView && mMenuView.getChildCount() == 0)) {
                visibleChildren++;//www  . java  2 s  .  c  o m
            }
        }

        if (visibleChildren == 0) {
            // No size for an empty action bar when collapsable.
            setMeasuredDimension(0, 0);
            mIsCollapsed = true;
            return;
        }
    }
    mIsCollapsed = false;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    if (widthMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException(getClass().getSimpleName() + " can only be used "
                + "with android:layout_width=\"MATCH_PARENT\" (or fill_parent)");
    }

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if (heightMode != MeasureSpec.AT_MOST) {
        throw new IllegalStateException(getClass().getSimpleName() + " can only be used "
                + "with android:layout_height=\"wrap_content\"");
    }

    int contentWidth = MeasureSpec.getSize(widthMeasureSpec);

    int maxHeight = mContentHeight > 0 ? mContentHeight : MeasureSpec.getSize(heightMeasureSpec);

    final int verticalPadding = getPaddingTop() + getPaddingBottom();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int height = maxHeight - verticalPadding;
    final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);

    int availableWidth = contentWidth - paddingLeft - paddingRight;
    int leftOfCenter = availableWidth / 2;
    int rightOfCenter = leftOfCenter;

    HomeView homeLayout = mExpandedActionView != null ? mExpandedHomeLayout : mHomeLayout;

    if (homeLayout.getVisibility() != GONE) {
        final ViewGroup.LayoutParams lp = homeLayout.getLayoutParams();
        int homeWidthSpec;
        if (lp.width < 0) {
            homeWidthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
        } else {
            homeWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
        }
        homeLayout.measure(homeWidthSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        final int homeWidth = homeLayout.getMeasuredWidth() + homeLayout.getLeftOffset();
        availableWidth = Math.max(0, availableWidth - homeWidth);
        leftOfCenter = Math.max(0, availableWidth - homeWidth);
    }

    if (mMenuView != null && mMenuView.getParent() == this) {
        availableWidth = measureChildView(mMenuView, availableWidth, childSpecHeight, 0);
        rightOfCenter = Math.max(0, rightOfCenter - mMenuView.getMeasuredWidth());
    }

    if (mIndeterminateProgressView != null && mIndeterminateProgressView.getVisibility() != GONE) {
        availableWidth = measureChildView(mIndeterminateProgressView, availableWidth, childSpecHeight, 0);
        rightOfCenter = Math.max(0, rightOfCenter - mIndeterminateProgressView.getMeasuredWidth());
    }

    final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE
            && (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;

    if (mExpandedActionView == null) {
        switch (mNavigationMode) {
        case ActionBar.NAVIGATION_MODE_LIST:
            if (mListNavLayout != null) {
                final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding;
                availableWidth = Math.max(0, availableWidth - itemPaddingSize);
                leftOfCenter = Math.max(0, leftOfCenter - itemPaddingSize);
                mListNavLayout.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                final int listNavWidth = mListNavLayout.getMeasuredWidth();
                availableWidth = Math.max(0, availableWidth - listNavWidth);
                leftOfCenter = Math.max(0, leftOfCenter - listNavWidth);
            }
            break;
        case ActionBar.NAVIGATION_MODE_TABS:
            if (mTabScrollView != null) {
                final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding;
                availableWidth = Math.max(0, availableWidth - itemPaddingSize);
                leftOfCenter = Math.max(0, leftOfCenter - itemPaddingSize);
                mTabScrollView.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                final int tabWidth = mTabScrollView.getMeasuredWidth();
                availableWidth = Math.max(0, availableWidth - tabWidth);
                leftOfCenter = Math.max(0, leftOfCenter - tabWidth);
            }
            break;
        }
    }

    View customView = null;
    if (mExpandedActionView != null) {
        customView = mExpandedActionView;
    } else if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomNavView != null) {
        customView = mCustomNavView;
    }

    if (customView != null) {
        final ViewGroup.LayoutParams lp = generateLayoutParams(customView.getLayoutParams());
        final ActionBar.LayoutParams ablp = lp instanceof ActionBar.LayoutParams ? (ActionBar.LayoutParams) lp
                : null;

        int horizontalMargin = 0;
        int verticalMargin = 0;
        if (ablp != null) {
            horizontalMargin = ablp.leftMargin + ablp.rightMargin;
            verticalMargin = ablp.topMargin + ablp.bottomMargin;
        }

        // If the action bar is wrapping to its content height, don't allow a custom
        // view to FILL_PARENT.
        int customNavHeightMode;
        if (mContentHeight <= 0) {
            customNavHeightMode = MeasureSpec.AT_MOST;
        } else {
            customNavHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? MeasureSpec.EXACTLY
                    : MeasureSpec.AT_MOST;
        }
        final int customNavHeight = Math.max(0,
                (lp.height >= 0 ? Math.min(lp.height, height) : height) - verticalMargin);

        final int customNavWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? MeasureSpec.EXACTLY
                : MeasureSpec.AT_MOST;
        int customNavWidth = Math.max(0,
                (lp.width >= 0 ? Math.min(lp.width, availableWidth) : availableWidth) - horizontalMargin);
        final int hgrav = (ablp != null ? ablp.gravity : DEFAULT_CUSTOM_GRAVITY)
                & Gravity.HORIZONTAL_GRAVITY_MASK;

        // Centering a custom view is treated specially; we try to center within the whole
        // action bar rather than in the available space.
        if (hgrav == Gravity.CENTER_HORIZONTAL && lp.width == LayoutParams.FILL_PARENT) {
            customNavWidth = Math.min(leftOfCenter, rightOfCenter) * 2;
        }

        customView.measure(MeasureSpec.makeMeasureSpec(customNavWidth, customNavWidthMode),
                MeasureSpec.makeMeasureSpec(customNavHeight, customNavHeightMode));
        availableWidth -= horizontalMargin + customView.getMeasuredWidth();
    }

    if (mExpandedActionView == null && showTitle) {
        availableWidth = measureChildView(mTitleLayout, availableWidth,
                MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.EXACTLY), 0);
        leftOfCenter = Math.max(0, leftOfCenter - mTitleLayout.getMeasuredWidth());
    }

    if (mContentHeight <= 0) {
        int measuredHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View v = getChildAt(i);
            int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
            if (paddedViewHeight > measuredHeight) {
                measuredHeight = paddedViewHeight;
            }
        }
        setMeasuredDimension(contentWidth, measuredHeight);
    } else {
        setMeasuredDimension(contentWidth, maxHeight);
    }

    if (mContextView != null) {
        mContextView.setContentHeight(getMeasuredHeight());
    }

    if (mProgressView != null && mProgressView.getVisibility() != GONE) {
        mProgressView.measure(
                MeasureSpec.makeMeasureSpec(contentWidth - mProgressBarPadding * 2, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST));
    }
}

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollX = getScrollX();
    final int scrollY = getScrollY();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }//  ww  w. j  a v a2  s .com
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                if (isOrientationHorizontal()) {
                    childLeft += scrollX;
                } else {
                    childTop += scrollY;
                }
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    int childSize = 0;
    if (isOrientationHorizontal()) {
        childSize = width - paddingLeft - paddingRight;
    } else {
        childSize = height - paddingTop - paddingBottom;
    }
    // Page views. Do this once we have the right padding offsets from above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (childSize * ii.offset);
                int childLeft = paddingLeft + (isOrientationHorizontal() ? loff : 0);
                int childTop = paddingTop + (isOrientationHorizontal() ? 0 : loff);
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    int widthSpec = 0, heightSpec = 0;
                    if (isOrientationHorizontal()) {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (childSize * lp.sizeFactor),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (height - paddingTop - paddingBottom),
                                MeasureSpec.EXACTLY);
                    } else {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (width - paddingLeft - paddingRight),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (childSize * lp.sizeFactor),
                                MeasureSpec.EXACTLY);
                    }
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mLeftPageBounds = paddingLeft;
    mTopPageBounds = paddingTop;
    mRightPageBounds = width - paddingRight;
    mBottomPageBounds = height - paddingBottom;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

/**
 * This method will be invoked when the current page is scrolled, either as part
 * of a programmatically initiated smooth scroll or a user initiated touch scroll.
 * If you override this method you must call through to the superclass implementation
 * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
 * returns./*from   w w  w.  ja va  2s  .c o m*/
 *
 * @param position Position index of the first page currently being displayed.
 *                 Page position+1 will be visible if positionOffset is nonzero.
 * @param offset Value from [0, 1) indicating the offset from the page at position.
 * @param offsetPixels Value in pixels indicating the offset from position.
 */
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    // Offset any decor views if needed - keep them on-screen at all times.
    if (mDecorChildCount > 0) {
        // TODO This is where I start getting tired. Refactor this better later.
        if (isOrientationHorizontal()) {
            final int scrollX = getScrollX();
            int paddingLeft = getPaddingLeft();
            int paddingRight = getPaddingRight();
            final int width = getWidth();
            final int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (!lp.isDecor)
                    continue;

                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                int childLeft = 0;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }
                childLeft += scrollX;

                final int childOffset = childLeft - child.getLeft();
                if (childOffset != 0) {
                    child.offsetLeftAndRight(childOffset);
                }
            }
        } else {
            final int scrollY = getScrollY();
            int paddingTop = getPaddingTop();
            int paddingBottom = getPaddingBottom();
            final int height = getHeight();
            final int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (!lp.isDecor)
                    continue;

                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int childTop = 0;
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childTop += scrollY;

                final int childOffset = childTop - child.getTop();
                if (childOffset != 0) {
                    child.offsetTopAndBottom(childOffset);
                }
            }
        }
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }

    if (mPageTransformer != null) {
        final boolean horizontal = isOrientationHorizontal();
        final int scroll = horizontal ? getScrollX() : getScrollY();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (lp.isDecor)
                continue;

            float transformPos;
            if (horizontal) {
                transformPos = (float) (child.getLeft() - scroll) / getClientWidth();
            } else {
                transformPos = (float) (child.getTop() - scroll) / getClientHeight();
            }
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:android.support.design.widget.CoordinatorLayout.java

/**
 * Lay out a child view with no special handling. This will position the child as
 * if it were within a FrameLayout or similar simple frame.
 *
 * @param child child view to lay out//from  w  w  w  . ja  v  a2 s.c om
 * @param layoutDirection ViewCompat constant for the desired layout direction
 */
private void layoutChild(View child, int layoutDirection) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final Rect parent = acquireTempRect();
    parent.set(getPaddingLeft() + lp.leftMargin, getPaddingTop() + lp.topMargin,
            getWidth() - getPaddingRight() - lp.rightMargin,
            getHeight() - getPaddingBottom() - lp.bottomMargin);

    if (mLastInsets != null && ViewCompat.getFitsSystemWindows(this)
            && !ViewCompat.getFitsSystemWindows(child)) {
        // If we're set to handle insets but this child isn't, then it has been measured as
        // if there are no insets. We need to lay it out to match.
        parent.left += mLastInsets.getSystemWindowInsetLeft();
        parent.top += mLastInsets.getSystemWindowInsetTop();
        parent.right -= mLastInsets.getSystemWindowInsetRight();
        parent.bottom -= mLastInsets.getSystemWindowInsetBottom();
    }

    final Rect out = acquireTempRect();
    GravityCompat.apply(resolveGravity(lp.gravity), child.getMeasuredWidth(), child.getMeasuredHeight(), parent,
            out, layoutDirection);
    child.layout(out.left, out.top, out.right, out.bottom);

    releaseTempRect(parent);
    releaseTempRect(out);
}

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int childCount = getChildCount();
    if (mIsCollapsable) {
        int visibleChildren = 0;
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE && !(child == mMenuView && mMenuView.getChildCount() == 0)) {
                visibleChildren++;/*  ww w  .jav a2s  . c  om*/
            }
        }

        if (visibleChildren == 0) {
            // No size for an empty action bar when collapsable.
            setMeasuredDimension(0, 0);
            mIsCollapsed = true;
            return;
        }
    }
    mIsCollapsed = false;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    if (widthMode != MeasureSpec.EXACTLY) {
        throw new IllegalStateException(getClass().getSimpleName() + " can only be used "
                + "with android:layout_width=\"match_parent\" (or fill_parent)");
    }

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if (heightMode != MeasureSpec.AT_MOST) {
        throw new IllegalStateException(getClass().getSimpleName() + " can only be used "
                + "with android:layout_height=\"wrap_content\"");
    }

    int contentWidth = MeasureSpec.getSize(widthMeasureSpec);

    int maxHeight = mContentHeight > 0 ? mContentHeight : MeasureSpec.getSize(heightMeasureSpec);

    final int verticalPadding = getPaddingTop() + getPaddingBottom();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int height = maxHeight - verticalPadding;
    final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);

    int availableWidth = contentWidth - paddingLeft - paddingRight;
    int leftOfCenter = availableWidth / 2;
    int rightOfCenter = leftOfCenter;

    HomeView homeLayout = mExpandedActionView != null ? mExpandedHomeLayout : mHomeLayout;

    if (homeLayout.getVisibility() != GONE) {
        final ViewGroup.LayoutParams lp = homeLayout.getLayoutParams();
        int homeWidthSpec;
        if (lp.width < 0) {
            homeWidthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
        } else {
            homeWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
        }
        homeLayout.measure(homeWidthSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        final int homeWidth = homeLayout.getMeasuredWidth() + homeLayout.getLeftOffset();
        availableWidth = Math.max(0, availableWidth - homeWidth);
        leftOfCenter = Math.max(0, availableWidth - homeWidth);
    }

    if (mMenuView != null && mMenuView.getParent() == this) {
        availableWidth = measureChildView(mMenuView, availableWidth, childSpecHeight, 0);
        rightOfCenter = Math.max(0, rightOfCenter - mMenuView.getMeasuredWidth());
    }

    if (mIndeterminateProgressView != null && mIndeterminateProgressView.getVisibility() != GONE) {
        availableWidth = measureChildView(mIndeterminateProgressView, availableWidth, childSpecHeight, 0);
        rightOfCenter = Math.max(0, rightOfCenter - mIndeterminateProgressView.getMeasuredWidth());
    }

    final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE
            && (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;

    if (mExpandedActionView == null) {
        switch (mNavigationMode) {
        case ActionBar.NAVIGATION_MODE_LIST:
            if (mListNavLayout != null) {
                final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding;
                availableWidth = Math.max(0, availableWidth - itemPaddingSize);
                leftOfCenter = Math.max(0, leftOfCenter - itemPaddingSize);
                mListNavLayout.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                final int listNavWidth = mListNavLayout.getMeasuredWidth();
                availableWidth = Math.max(0, availableWidth - listNavWidth);
                leftOfCenter = Math.max(0, leftOfCenter - listNavWidth);
            }
            break;
        case ActionBar.NAVIGATION_MODE_TABS:
            if (mTabScrollView != null) {
                final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding;
                availableWidth = Math.max(0, availableWidth - itemPaddingSize);
                leftOfCenter = Math.max(0, leftOfCenter - itemPaddingSize);
                mTabScrollView.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                final int tabWidth = mTabScrollView.getMeasuredWidth();
                availableWidth = Math.max(0, availableWidth - tabWidth);
                leftOfCenter = Math.max(0, leftOfCenter - tabWidth);
            }
            break;
        }
    }

    View customView = null;
    if (mExpandedActionView != null) {
        customView = mExpandedActionView;
    } else if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomNavView != null) {
        customView = mCustomNavView;
    }

    if (customView != null) {
        final ViewGroup.LayoutParams lp = generateLayoutParams(customView.getLayoutParams());
        final ActionBar.LayoutParams ablp = lp instanceof ActionBar.LayoutParams ? (ActionBar.LayoutParams) lp
                : null;

        int horizontalMargin = 0;
        int verticalMargin = 0;
        if (ablp != null) {
            horizontalMargin = ablp.leftMargin + ablp.rightMargin;
            verticalMargin = ablp.topMargin + ablp.bottomMargin;
        }

        // If the action bar is wrapping to its content height, don't allow a custom
        // view to MATCH_PARENT.
        int customNavHeightMode;
        if (mContentHeight <= 0) {
            customNavHeightMode = MeasureSpec.AT_MOST;
        } else {
            customNavHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? MeasureSpec.EXACTLY
                    : MeasureSpec.AT_MOST;
        }
        final int customNavHeight = Math.max(0,
                (lp.height >= 0 ? Math.min(lp.height, height) : height) - verticalMargin);

        final int customNavWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? MeasureSpec.EXACTLY
                : MeasureSpec.AT_MOST;
        int customNavWidth = Math.max(0,
                (lp.width >= 0 ? Math.min(lp.width, availableWidth) : availableWidth) - horizontalMargin);
        final int hgrav = (ablp != null ? ablp.gravity : DEFAULT_CUSTOM_GRAVITY)
                & Gravity.HORIZONTAL_GRAVITY_MASK;

        // Centering a custom view is treated specially; we try to center within the whole
        // action bar rather than in the available space.
        if (hgrav == Gravity.CENTER_HORIZONTAL && lp.width == LayoutParams.MATCH_PARENT) {
            customNavWidth = Math.min(leftOfCenter, rightOfCenter) * 2;
        }

        customView.measure(MeasureSpec.makeMeasureSpec(customNavWidth, customNavWidthMode),
                MeasureSpec.makeMeasureSpec(customNavHeight, customNavHeightMode));
        availableWidth -= horizontalMargin + customView.getMeasuredWidth();
    }

    if (mExpandedActionView == null && showTitle) {
        availableWidth = measureChildView(mTitleLayout, availableWidth,
                MeasureSpec.makeMeasureSpec(mContentHeight, MeasureSpec.EXACTLY), 0);
        leftOfCenter = Math.max(0, leftOfCenter - mTitleLayout.getMeasuredWidth());
    }

    if (mContentHeight <= 0) {
        int measuredHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View v = getChildAt(i);
            int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
            if (paddedViewHeight > measuredHeight) {
                measuredHeight = paddedViewHeight;
            }
        }
        setMeasuredDimension(contentWidth, measuredHeight);
    } else {
        setMeasuredDimension(contentWidth, maxHeight);
    }

    if (mContextView != null) {
        mContextView.setContentHeight(getMeasuredHeight());
    }

    if (mProgressView != null && mProgressView.getVisibility() != GONE) {
        mProgressView.measure(
                MeasureSpec.makeMeasureSpec(contentWidth - mProgressBarPadding * 2, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST));
    }
}

From source file:com.guide.ViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    int width = r - l;
    int height = b - t;
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();
    final int scrollX = getScrollX();
    final int scrollY = getScrollY();

    int decorCount = 0;

    // First pass - decor views. We need to do this in two passes so that
    // we have the proper offsets for non-decor views later.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = 0;
            int childTop = 0;
            if (lp.isDecor) {
                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getMeasuredWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }/*from   w w w.ja  v a  2s.co m*/
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getMeasuredHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                if (isOrientationHorizontal()) {
                    childLeft += scrollX;
                } else {
                    childTop += scrollY;
                }
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
                decorCount++;
            }
        }
    }

    int childSize = 0;
    if (isOrientationHorizontal()) {
        childSize = width - paddingLeft - paddingRight;
    } else {
        childSize = height - paddingTop - paddingBottom;
    }
    // Page views. Do this once we have the right padding offsets from
    // above.
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            if (!lp.isDecor && (ii = infoForChild(child)) != null) {
                int loff = (int) (childSize * ii.offset);
                int childLeft = paddingLeft + (isOrientationHorizontal() ? loff : 0);
                int childTop = paddingTop + (isOrientationHorizontal() ? 0 : loff);
                if (lp.needsMeasure) {
                    // This was added during layout and needs measurement.
                    // Do it now that we know what we're working with.
                    lp.needsMeasure = false;
                    int widthSpec = 0, heightSpec = 0;
                    if (isOrientationHorizontal()) {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (childSize * lp.sizeFactor),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (height - paddingTop - paddingBottom),
                                MeasureSpec.EXACTLY);
                    } else {
                        widthSpec = MeasureSpec.makeMeasureSpec((int) (width - paddingLeft - paddingRight),
                                MeasureSpec.EXACTLY);
                        heightSpec = MeasureSpec.makeMeasureSpec((int) (childSize * lp.sizeFactor),
                                MeasureSpec.EXACTLY);
                    }
                    child.measure(widthSpec, heightSpec);
                }
                if (DEBUG)
                    Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object + ":" + childLeft + ","
                            + childTop + " " + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            }
        }
    }
    mLeftPageBounds = paddingLeft;
    mTopPageBounds = paddingTop;
    mRightPageBounds = width - paddingRight;
    mBottomPageBounds = height - paddingBottom;
    mDecorChildCount = decorCount;

    if (mFirstLayout) {
        scrollToItem(mCurItem, false, 0, false);
    }
    mFirstLayout = false;
}