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.widget.SlidingPaneLayout.java

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

    final int height = b - t;
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    final int paddingLeft = getPaddingLeft();

    final int childCount = getChildCount();
    int yStart = paddingTop;
    int nextYStart = yStart;

    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);

        if (child.getVisibility() == GONE) {
            continue;
        }/*from   w w w. j  a va 2s. c o  m*/

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

        final int childHeight = child.getMeasuredHeight();
        int offset = 0;

        if (lp.slideable) {
            final int margin = lp.topMargin + lp.bottomMargin;
            final int range = Math.min(nextYStart, height - paddingBottom) - yStart - margin;
            mSlideRange = range;

            if (mFirstLayout) {
                switch (mState) {
                case STATE_CLOSED:
                    mSlideOffset = mCanSlide ? 0.f : 1.f;
                    break;
                case STATE_OPENED:
                    mSlideOffset = mCanSlide ? 1 - (float) mOverhangSize / mSlideRange : 1.f;
                    break;
                default: // STATE_OPENED_ENTIRELY
                    mSlideOffset = 1.f;
                    break;
                }
            }

            final int pos = (int) (range * mSlideOffset);
            yStart += pos + lp.topMargin;
            mSlideOffset = (float) pos / mSlideRange;
        } else {
            yStart = nextYStart;
        }

        final int childTop = yStart - offset;
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft;
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(paddingLeft, childTop, childRight, childBottom);

        nextYStart += child.getHeight();
    }

    if (mFirstLayout) {
        updateObscuredViewsVisibility(mSlideableView);
    }

    mFirstLayout = false;
}

From source file:com.chenglong.muscle.ui.LazyViewPager.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.
 *
 * @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.//from  w  ww  .  j  ava 2  s.  c om
 * @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) {
        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);
            }
        }
    }
    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    mCalledSuper = true;
}

From source file:com.devabit.takestock.ui.widget.FlexboxLayout.java

/**
 * Sub method for {@link #onLayout(boolean, int, int, int, int)} when the
 * {@link #mFlexDirection} is either {@link #FLEX_DIRECTION_COLUMN} or
 * {@link #FLEX_DIRECTION_COLUMN_REVERSE}.
 *
 * @param isRtl           {@code true} if the horizontal layout direction is right to left,
 *                        {@code false}/*w w  w  .j  a v a  2 s .c o m*/
 *                        otherwise
 * @param fromBottomToTop {@code true} if the layout direction is bottom to top, {@code false}
 *                        otherwise
 * @param left            the left position of this View
 * @param top             the top position of this View
 * @param right           the right position of this View
 * @param bottom          the bottom position of this View
 * @see #getFlexWrap()
 * @see #setFlexWrap(int)
 * @see #getJustifyContent()
 * @see #setJustifyContent(int)
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, int top, int right, int bottom) {
    int paddingTop = getPaddingTop();
    int paddingBottom = getPaddingBottom();

    int paddingRight = getPaddingRight();
    int childLeft = getPaddingLeft();
    int currentViewIndex = 0;

    int width = right - left;
    int height = bottom - top;
    // childRight is used if the mFlexWrap is FLEX_WRAP_WRAP_REVERSE otherwise
    // childLeft is used to align the horizontal position of the children views.
    int childRight = width - paddingRight;

    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childTop;

    // Used only for if the direction is from bottom to top
    float childBottom;
    for (FlexLine flexLine : mFlexLines) {
        float spaceBetweenItem = 0f;
        switch (mJustifyContent) {
        case JUSTIFY_CONTENT_FLEX_START:
            childTop = paddingTop;
            childBottom = height - paddingBottom;
            break;
        case JUSTIFY_CONTENT_FLEX_END:
            childTop = height - flexLine.mainSize + paddingBottom;
            childBottom = flexLine.mainSize - paddingTop;
            break;
        case JUSTIFY_CONTENT_CENTER:
            childTop = paddingTop + (height - flexLine.mainSize) / 2f;
            childBottom = height - paddingBottom - (height - flexLine.mainSize) / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_AROUND:
            if (flexLine.itemCount != 0) {
                spaceBetweenItem = (height - flexLine.mainSize) / (float) flexLine.itemCount;
            }
            childTop = paddingTop + spaceBetweenItem / 2f;
            childBottom = height - paddingBottom - spaceBetweenItem / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_BETWEEN:
            childTop = paddingTop;
            float denominator = flexLine.itemCount != 1 ? flexLine.itemCount - 1 : 1f;
            spaceBetweenItem = (height - flexLine.mainSize) / denominator;
            childBottom = height - paddingBottom;
            break;
        default:
            throw new IllegalStateException("Invalid justifyContent is set: " + mJustifyContent);
        }
        spaceBetweenItem = Math.max(spaceBetweenItem, 0);

        for (int i = 0; i < flexLine.itemCount; i++) {
            View child = getReorderedChildAt(currentViewIndex);
            if (child == null) {
                continue;
            } else if (child.getVisibility() == View.GONE) {
                currentViewIndex++;
                continue;
            }
            LayoutParams lp = ((LayoutParams) child.getLayoutParams());
            childTop += lp.topMargin;
            childBottom -= lp.bottomMargin;
            if (isRtl) {
                if (fromBottomToTop) {
                    layoutSingleChildVertical(child, flexLine, true, mAlignItems,
                            childRight - child.getMeasuredWidth(),
                            Math.round(childBottom) - child.getMeasuredHeight(), childRight,
                            Math.round(childBottom));
                } else {
                    layoutSingleChildVertical(child, flexLine, true, mAlignItems,
                            childRight - child.getMeasuredWidth(), Math.round(childTop), childRight,
                            Math.round(childTop) + child.getMeasuredHeight());
                }
            } else {
                if (fromBottomToTop) {
                    layoutSingleChildVertical(child, flexLine, false, mAlignItems, childLeft,
                            Math.round(childBottom) - child.getMeasuredHeight(),
                            childLeft + child.getMeasuredWidth(), Math.round(childBottom));
                } else {
                    layoutSingleChildVertical(child, flexLine, false, mAlignItems, childLeft,
                            Math.round(childTop), childLeft + child.getMeasuredWidth(),
                            Math.round(childTop) + child.getMeasuredHeight());
                }
            }
            childTop += child.getMeasuredHeight() + spaceBetweenItem + lp.bottomMargin;
            childBottom -= child.getMeasuredHeight() + spaceBetweenItem + lp.topMargin;
            currentViewIndex++;
        }
        childLeft += flexLine.crossSize;
        childRight -= flexLine.crossSize;
    }
}

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 www .  jav a  2s  .  c  o  m*/

    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.android.tv.settings.dialog.DialogFragment.java

private void performEntryTransition() {
    final View dialogView = getView();
    final View contentView = (View) dialogView.getTag(R.id.content_fragment);
    final View actionContainerView = dialogView.findViewById(R.id.action_fragment);

    mIntroAnimationInProgress = true;// w  w  w.  ja v a2 s .c om

    // Fade out the old activity.
    getActivity().overridePendingTransition(0, R.anim.lb_dialog_fade_out);

    int bgColor = contentView.getContext().getResources().getColor(R.color.lb_dialog_activity_background);
    final ColorDrawable bgDrawable = new ColorDrawable();
    bgDrawable.setColor(bgColor);
    bgDrawable.setAlpha(0);
    dialogView.setBackground(bgDrawable);
    dialogView.setVisibility(View.INVISIBLE);

    runDelayedAnim(new Runnable() {
        @Override
        public void run() {
            if (!isAdded()) {
                // We have been detached before this could run,
                // so just bail
                return;
            }

            dialogView.setVisibility(View.VISIBLE);

            // Fade in the activity background protection
            ObjectAnimator oa = ObjectAnimator.ofInt(bgDrawable, "alpha", 255);
            oa.setDuration(ANIMATE_IN_DURATION);
            oa.setStartDelay(SECONDARY_ANIMATE_DELAY);
            oa.setInterpolator(new DecelerateInterpolator(1.0f));
            oa.start();

            boolean isRtl = ViewCompat.getLayoutDirection(contentView) == View.LAYOUT_DIRECTION_RTL;
            int startDist = isRtl ? SLIDE_IN_DISTANCE : -SLIDE_IN_DISTANCE;
            int endDist = isRtl ? -actionContainerView.getMeasuredWidth()
                    : actionContainerView.getMeasuredWidth();

            // Fade in and slide in the ContentFragment
            // TextViews from the start.
            prepareAndAnimateView((View) contentView.getTag(R.id.title), startDist, false);
            prepareAndAnimateView((View) contentView.getTag(R.id.breadcrumb), startDist, false);
            prepareAndAnimateView((View) contentView.getTag(R.id.description), startDist, false);

            // Fade in and slide in the ActionFragment from the
            // end.
            prepareAndAnimateView(actionContainerView, endDist, false);
            prepareAndAnimateView((View) contentView.getTag(R.id.icon), startDist, true);

            // Fade in the selector.
            if (mSelectorAnimator != null) {
                mSelectorAnimator.fadeIn();
            }
        }
    });
}

From source file:com.devabit.takestock.ui.widget.FlexboxLayout.java

/**
 * Sub method for {@link #onLayout(boolean, int, int, int, int)} when the
 * {@link #mFlexDirection} is either {@link #FLEX_DIRECTION_ROW} or
 * {@link #FLEX_DIRECTION_ROW_REVERSE}./*from   w  w w.  j a  v  a2 s . co  m*/
 *
 * @param isRtl  {@code true} if the horizontal layout direction is right to left, {@code
 *               false} otherwise.
 * @param left   the left position of this View
 * @param top    the top position of this View
 * @param right  the right position of this View
 * @param bottom the bottom position of this View
 * @see #getFlexWrap()
 * @see #setFlexWrap(int)
 * @see #getJustifyContent()
 * @see #setJustifyContent(int)
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutHorizontal(boolean isRtl, int left, int top, int right, int bottom) {
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childLeft;
    int currentViewIndex = 0;

    int height = bottom - top;
    int width = right - left;
    // childBottom is used if the mFlexWrap is FLEX_WRAP_WRAP_REVERSE otherwise
    // childTop is used to align the vertical position of the children views.
    int childBottom = height - getPaddingBottom();
    int childTop = getPaddingTop();

    // Used only for RTL layout
    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childRight;
    for (FlexLine flexLine : mFlexLines) {
        float spaceBetweenItem = 0f;
        switch (mJustifyContent) {
        case JUSTIFY_CONTENT_FLEX_START:
            childLeft = paddingLeft;
            childRight = width - paddingRight;
            break;
        case JUSTIFY_CONTENT_FLEX_END:
            childLeft = width - flexLine.mainSize + paddingRight;
            childRight = flexLine.mainSize - paddingLeft;
            break;
        case JUSTIFY_CONTENT_CENTER:
            childLeft = paddingLeft + (width - flexLine.mainSize) / 2f;
            childRight = width - paddingRight - (width - flexLine.mainSize) / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_AROUND:
            if (flexLine.itemCount != 0) {
                spaceBetweenItem = (width - flexLine.mainSize) / (float) flexLine.itemCount;
            }
            childLeft = paddingLeft + spaceBetweenItem / 2f;
            childRight = width - paddingRight - spaceBetweenItem / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_BETWEEN:
            childLeft = paddingLeft;
            float denominator = flexLine.itemCount != 1 ? flexLine.itemCount - 1 : 1f;
            spaceBetweenItem = (width - flexLine.mainSize) / denominator;
            childRight = width - paddingRight;
            break;
        default:
            throw new IllegalStateException("Invalid justifyContent is set: " + mJustifyContent);
        }
        spaceBetweenItem = Math.max(spaceBetweenItem, 0);

        for (int i = 0; i < flexLine.itemCount; i++) {
            View child = getReorderedChildAt(currentViewIndex);
            if (child == null) {
                continue;
            } else if (child.getVisibility() == View.GONE) {
                currentViewIndex++;
                continue;
            }
            LayoutParams lp = ((LayoutParams) child.getLayoutParams());
            childLeft += lp.leftMargin;
            childRight -= lp.rightMargin;
            if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
                if (isRtl) {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems,
                            Math.round(childRight) - child.getMeasuredWidth(),
                            childBottom - child.getMeasuredHeight(), Math.round(childRight), childBottom);
                } else {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems, Math.round(childLeft),
                            childBottom - child.getMeasuredHeight(),
                            Math.round(childLeft) + child.getMeasuredWidth(), childBottom);
                }
            } else {
                if (isRtl) {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems,
                            Math.round(childRight) - child.getMeasuredWidth(), childTop, Math.round(childRight),
                            childTop + child.getMeasuredHeight());
                } else {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems, Math.round(childLeft),
                            childTop, Math.round(childLeft) + child.getMeasuredWidth(),
                            childTop + child.getMeasuredHeight());
                }
            }
            childLeft += child.getMeasuredWidth() + spaceBetweenItem + lp.rightMargin;
            childRight -= child.getMeasuredWidth() + spaceBetweenItem + lp.leftMargin;
            currentViewIndex++;
        }
        childTop += flexLine.crossSize;
        childBottom -= flexLine.crossSize;
    }
}

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

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mInLayout = true;//from  ww  w . ja v a  2 s .  com
    populate();
    mInLayout = false;
    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;
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            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;
                }
                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;
                decorCount++;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            } else if ((ii = infoForChild(child)) != null) {
                int loff = (width + mPageMargin) * ii.position;
                childLeft = paddingLeft + loff;
                childTop = paddingTop;
                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;
    mFirstLayout = false;
}

From source file:com.google.android.flexbox.FlexboxHelper.java

/**
 * Expand the view vertically to the size of the crossSize (considering the view margins)
 *
 * @param view      the View to be stretched
 * @param crossSize the cross size/*ww  w  .jav  a2s. com*/
 * @param index     the index of the view
 */
private void stretchViewVertically(View view, int crossSize, int index) {
    FlexItem flexItem = (FlexItem) view.getLayoutParams();
    int newHeight = crossSize - flexItem.getMarginTop() - flexItem.getMarginBottom()
            - mFlexContainer.getDecorationLengthCrossAxis(view);
    newHeight = Math.max(newHeight, flexItem.getMinHeight());
    newHeight = Math.min(newHeight, flexItem.getMaxHeight());
    int childWidthSpec;
    int measuredWidth;
    if (mMeasuredSizeCache != null) {
        // Retrieve the measured height from the cache because there
        // are some cases that the view is re-created from the last measure, thus
        // View#getMeasuredHeight returns 0.
        // E.g. if the flex container is FlexboxLayoutManager, that case happens
        // frequently
        measuredWidth = extractLowerInt(mMeasuredSizeCache[index]);
    } else {
        measuredWidth = view.getMeasuredWidth();
    }
    childWidthSpec = View.MeasureSpec.makeMeasureSpec(measuredWidth, View.MeasureSpec.EXACTLY);

    int childHeightSpec = View.MeasureSpec.makeMeasureSpec(newHeight, View.MeasureSpec.EXACTLY);
    view.measure(childWidthSpec, childHeightSpec);

    updateMeasureCache(index, childWidthSpec, childHeightSpec, view);
    mFlexContainer.updateViewCache(index, view);
}

From source file:beichen.douban.ui.view.LazyViewPager.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.
 * //w w  w.  j  a  va  2  s . 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) {
        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);
            }
        }
    }

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

From source file:com.ferg.awfulapp.widget.AwfulViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mInLayout = true;//  w w w. j a  va 2 s  . c  o  m
    populate();
    mInLayout = false;

    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;

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            ItemInfo ii;
            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;
                }
                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;
                decorCount++;
                child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());
            } else if ((ii = infoForChild(child)) != null) {
                int loff = (width + mPageMargin) * ii.position;
                childLeft = paddingLeft + loff;
                childTop = paddingTop;
                //                       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;
    mFirstLayout = false;
}