Example usage for android.view View layout

List of usage examples for android.view View layout

Introduction

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

Prototype

@SuppressWarnings({ "unchecked" })
public void layout(int l, int t, int r, int b) 

Source Link

Document

Assign a size and position to a view and all of its descendants

This is the second phase of the layout mechanism.

Usage

From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java

/**
 * Place a single View when the layout direction is vertical ({@link #mFlexDirection} is
 * either {@link #FLEX_DIRECTION_COLUMN} or {@link #FLEX_DIRECTION_COLUMN_REVERSE}).
 *
 * @param view       the View to be placed
 * @param flexLine   the {@link FlexLine} where the View belongs to
 * @param isRtl      {@code true} if the layout direction is right to left, {@code false}
 *                   otherwise/* w  ww  .  j a  v  a2 s . com*/
 * @param alignItems the align items attribute of this FlexboxLayout
 * @param left       the left position of the flex line where the View belongs to. The actual
 *                   View's left position is shifted depending on the isRtl and alignItems
 *                   attributes
 * @param top        the top position of the View, which the View's margin is already taken
 *                   into account
 * @param right      the right position of the flex line where the View belongs to. The actual
 *                   View's right position is shifted depending on the isRtl and alignItems
 *                   attributes
 * @param bottom     the bottom position of the View, which the View's margin is already taken
 *                   into account
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutSingleChildVertical(View view, FlexLine flexLine, boolean isRtl, int alignItems, int left,
        int top, int right, int bottom) {
    LayoutParams lp = (LayoutParams) view.getLayoutParams();
    if (lp.alignSelf != LayoutParams.ALIGN_SELF_AUTO) {
        // Expecting the values for alignItems and alignSelf match except for ALIGN_SELF_AUTO.
        // Assigning the alignSelf value as alignItems should work.
        alignItems = lp.alignSelf;
    }
    int crossSize = flexLine.mCrossSize;
    switch (alignItems) {
    case ALIGN_ITEMS_FLEX_START: // Intentional fall through
    case ALIGN_ITEMS_STRETCH: // Intentional fall through
    case ALIGN_ITEMS_BASELINE:
        if (!isRtl) {
            view.layout(left + lp.leftMargin, top, right + lp.leftMargin, bottom);
        } else {
            view.layout(left - lp.rightMargin, top, right - lp.rightMargin, bottom);
        }
        break;
    case ALIGN_ITEMS_FLEX_END:
        if (!isRtl) {
            view.layout(left + crossSize - view.getMeasuredWidth() - lp.rightMargin, top,
                    right + crossSize - view.getMeasuredWidth() - lp.rightMargin, bottom);
        } else {
            // If the flexWrap == FLEX_WRAP_WRAP_REVERSE, the direction of the
            // flexEnd is flipped (from left to right).
            view.layout(left - crossSize + view.getMeasuredWidth() + lp.leftMargin, top,
                    right - crossSize + view.getMeasuredWidth() + lp.leftMargin, bottom);
        }
        break;
    case ALIGN_ITEMS_CENTER:
        int leftFromCrossAxis = (crossSize - view.getMeasuredWidth()
                + MarginLayoutParamsCompat.getMarginStart(lp) - MarginLayoutParamsCompat.getMarginEnd(lp)) / 2;
        if (!isRtl) {
            view.layout(left + leftFromCrossAxis, top, right + leftFromCrossAxis, bottom);
        } else {
            view.layout(left - leftFromCrossAxis, top, right - leftFromCrossAxis, bottom);
        }
        break;
    }
}

From source file:cn.bingoogolapple.swipebacklayout.BGASwipeBackLayout.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final boolean isLayoutRtl = isLayoutRtlSupport();
    if (isLayoutRtl) {
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
    } else {// ww  w  . j av a2  s . c o m
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }
    final int width = r - l;
    final int paddingStart = isLayoutRtl ? getPaddingRight() : getPaddingLeft();
    final int paddingEnd = isLayoutRtl ? getPaddingLeft() : getPaddingRight();
    final int paddingTop = getPaddingTop();

    final int childCount = getChildCount();
    int xStart = paddingStart;
    int nextXStart = xStart;

    if (mFirstLayout) {
        mSlideOffset = mCanSlide && mPreservedOpenState ? 1.f : 0.f;
    }

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

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

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

        final int childWidth = child.getMeasuredWidth();
        int offset = 0;

        if (lp.slideable) {
            final int margin = lp.leftMargin + lp.rightMargin;
            final int range = Math.min(nextXStart, width - paddingEnd - mOverhangSize) - xStart - margin;
            mSlideRange = range;
            final int lpMargin = isLayoutRtl ? lp.rightMargin : lp.leftMargin;
            lp.dimWhenOffset = xStart + lpMargin + range + childWidth / 2 > width - paddingEnd;
            final int pos = (int) (range * mSlideOffset);
            xStart += pos + lpMargin;
            mSlideOffset = (float) pos / mSlideRange;
        } else if (mCanSlide && mParallaxBy != 0) {
            offset = (int) ((1 - mSlideOffset) * mParallaxBy);
            xStart = nextXStart;
        } else {
            xStart = nextXStart;
        }

        final int childRight;
        final int childLeft;
        if (isLayoutRtl) {
            childRight = width - xStart + offset;
            childLeft = childRight - childWidth;
        } else {
            childLeft = xStart - offset;
            childRight = childLeft + childWidth;
        }

        final int childTop = paddingTop;
        final int childBottom = childTop + child.getMeasuredHeight();
        child.layout(childLeft, paddingTop, childRight, childBottom);

        nextXStart += child.getWidth();
    }

    if (mFirstLayout) {
        if (mCanSlide) {
            if (mParallaxBy != 0) {
                parallaxOtherViews(mSlideOffset);
            }
            if (((LayoutParams) mSlideableView.getLayoutParams()).dimWhenOffset) {
                dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor);
            }
        } else {
            // Reset the dim level of all children; it's irrelevant when nothing moves.
            for (int i = 0; i < childCount; i++) {
                dimChildView(getChildAt(i), 0, mSliderFadeColor);
            }
        }
        updateObscuredViewsVisibility(mSlideableView);
    }

    mFirstLayout = false;
}

From source file:de.andacaydin.bidirectionalviewpagerlibrary.BiDirectionalViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    Log.i(TAG, "onLayout(" + changed + "l: " + l + " , t:" + t + " r:" + r + " b:" + b);
    mInLayout = true;/*ww  w.j a v a2  s  . com*/
    populate();
    mInLayout = false;

    final int count = getChildCount();
    final int sizeX = r - l;
    final int sizeY = b - t;

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        ItemInfo ii;
        if ((ii = infoForChild(child)) != null) {
            int offX = 0;
            int offY = 0;
            //calculate off according to position:
            switch (ii.position) {
            case 0:
                offX = sizeX * 1;
                offY = sizeY * 1;
                break;
            case 1:
                offX = sizeX * 1;
                offY = sizeY * 0;
                break;
            case 4:
                offX = sizeX * 0;
                offY = sizeY * 1;
                break;
            case 2:
                offX = sizeX * 2;
                offY = sizeY * 1;
                break;
            case 3:
                offX = sizeX * 1;
                offY = sizeY * 2;
                break;
            default:
                throw new UnsupportedOperationException("position range 0-4 !");
            }

            int childLeft = getPaddingLeft();
            int childTop = getPaddingTop();
            childLeft += offX;
            childTop += offY;
            if (DEBUG)
                Log.v(TAG,
                        "onLayout: Positioning #" + i + " pos:" + ii.position + " f="
                                + ii.object.getClass().getName() + ":" + childLeft + "," + childTop + " "
                                + child.getMeasuredWidth() + "x" + child.getMeasuredHeight());
            child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),
                    childTop + child.getMeasuredHeight());

        }
    }
}

From source file:com.mark.quick.ui.view.swipebacklayout.SwipeBackLayout.java

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

    final boolean isLayoutRtl = isLayoutRtlSupport();
    if (isLayoutRtl) {
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
    } else {/*www  . j av  a2  s  .  com*/
        mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }

    final int width = r - l;
    final int paddingStart = isLayoutRtl ? getPaddingRight() : getPaddingLeft();
    final int paddingEnd = isLayoutRtl ? getPaddingLeft() : getPaddingRight();
    final int paddingTop = getPaddingTop();

    final int childCount = getChildCount();
    int xStart = paddingStart;
    int nextXStart = xStart;

    if (mFirstLayout) {
        mSlideOffset = mCanSlide && mPreservedOpenState ? 1.f : 0.f;
    }

    for (int i = 0; i < childCount; i++) {

        final View child = getChildAt(i);

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

        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int childWidth = child.getMeasuredWidth();
        int offset = 0;

        if (lp.slideable) {
            final int margin = lp.leftMargin + lp.rightMargin;
            final int range = Math.min(nextXStart, width - paddingEnd - mOverhangSize) - xStart - margin;
            mSlideRange = range;
            final int lpMargin = isLayoutRtl ? lp.rightMargin : lp.leftMargin;
            lp.dimWhenOffset = xStart + lpMargin + range + childWidth / 2 > width - paddingEnd;
            final int pos = (int) (range * mSlideOffset);
            xStart += pos + lpMargin;
            mSlideOffset = (float) pos / mSlideRange;
        } else if (mCanSlide && mParallaxBy != 0) {
            offset = (int) ((1 - mSlideOffset) * mParallaxBy);
            xStart = nextXStart;
        } else {
            xStart = nextXStart;
        }

        final int childRight;
        final int childLeft;
        if (isLayoutRtl) {
            childRight = width - xStart + offset;
            childLeft = childRight - childWidth;
        } else {
            childLeft = xStart - offset;
            childRight = childLeft + childWidth;
        }

        final int childTop = paddingTop;
        final int childBottom = childTop + child.getMeasuredHeight();
        LogUtils.e("%s--%s onLayout[%s,%s,%s,%s][pt=%s,mb=%s]",
                mActivity.getClass().getSimpleName() + hashCode(), i, childLeft, paddingTop, childRight,
                childBottom, child.getPaddingTop(), lp.bottomMargin);
        child.layout(childLeft, paddingTop, childRight, childBottom);
        nextXStart += child.getWidth();

    }

    if (mFirstLayout) {
        if (mCanSlide) {
            if (mParallaxBy != 0) {
                parallaxOtherViews(mSlideOffset);
            }
            if (((LayoutParams) mSlideableView.getLayoutParams()).dimWhenOffset) {
                dimChildView(mSlideableView, mSlideOffset, mSliderFadeColor);
            }
        } else {
            // Reset the dim level of all children; it's irrelevant when nothing moves.
            for (int i = 0; i < childCount; i++) {
                dimChildView(getChildAt(i), 0, mSliderFadeColor);
            }
        }
        updateObscuredViewsVisibility(mSlideableView);
    }

    mFirstLayout = false;
}

From source file:cn.ieclipse.af.view.StaggeredGridView.java

/**
 * Measure and layout all currently visible children.
 *
 * @param queryAdapter true to requery the adapter for view data
 *///w  w  w .java 2s  .c  o m
final void layoutChildren(boolean queryAdapter) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    int rebuildLayoutRecordsBefore = -1;
    int rebuildLayoutRecordsAfter = -1;

    Arrays.fill(mItemBottoms, Integer.MIN_VALUE);

    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final int col = lp.column;
        final int position = mFirstPosition + i;
        final boolean needsLayout = queryAdapter || child.isLayoutRequested();

        if (queryAdapter) {
            View newView = obtainView(position, child);
            if (newView != child) {
                removeViewAt(i);
                addView(newView, i);
                child = newView;
            }
            lp = (LayoutParams) child.getLayoutParams(); // Might have changed
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);

        if (needsLayout) {
            final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

            final int heightSpec;
            if (lp.height == LayoutParams.WRAP_CONTENT) {
                heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
            }

            child.measure(widthSpec, heightSpec);
        }

        int childTop = mItemBottoms[col] > Integer.MIN_VALUE ? mItemBottoms[col] + mItemMargin : child.getTop();
        if (span > 1) {
            int lowest = childTop;
            for (int j = col + 1; j < col + span; j++) {
                final int bottom = mItemBottoms[j] + mItemMargin;
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            childTop = lowest;
        }
        final int childHeight = child.getMeasuredHeight();
        final int childBottom = childTop + childHeight;
        final int childLeft = paddingLeft + col * (colWidth + itemMargin);
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        for (int j = col; j < col + span; j++) {
            mItemBottoms[j] = childBottom;
        }

        final LayoutRecord rec = mLayoutRecords.get(position);
        if (rec != null && rec.height != childHeight) {
            // Invalidate our layout records for everything before this.
            rec.height = childHeight;
            rebuildLayoutRecordsBefore = position;
        }

        if (rec != null && rec.span != span) {
            // Invalidate our layout records for everything after this.
            rec.span = span;
            rebuildLayoutRecordsAfter = position;
        }
    }

    // Update mItemBottoms for any empty columns
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] == Integer.MIN_VALUE) {
            mItemBottoms[i] = mItemTops[i];
        }
    }

    if (rebuildLayoutRecordsBefore >= 0 || rebuildLayoutRecordsAfter >= 0) {
        if (rebuildLayoutRecordsBefore >= 0) {
            invalidateLayoutRecordsBeforePosition(rebuildLayoutRecordsBefore);
        }
        if (rebuildLayoutRecordsAfter >= 0) {
            invalidateLayoutRecordsAfterPosition(rebuildLayoutRecordsAfter);
        }
        for (int i = 0; i < childCount; i++) {
            final int position = mFirstPosition + i;
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            LayoutRecord rec = mLayoutRecords.get(position);
            if (rec == null) {
                rec = new LayoutRecord();
                mLayoutRecords.put(position, rec);
            }
            rec.column = lp.column;
            rec.height = child.getHeight();
            rec.id = lp.id;
            rec.span = Math.min(mColCount, lp.span);
        }
    }
}

From source file:org.bangbang.support.v4.widget.HListView.java

/**
     * Layout a child that has been measured, preserving its top position.
     * TODO: unify with setUpChild./*from www.jav  a  2 s  . co  m*/
     * @param child The child.
     */
    private void relayoutMeasuredItem(View child) {
        final int w = child.getMeasuredWidth();
        final int h = child.getMeasuredHeight();
        final int childLeft = child.getLeft();//mListPadding.left;
        final int childRight = childLeft + w;
        final int childTop = mListPadding.top;//child.getTop();
        final int childBottom = childTop + h;
        child.layout(childLeft, childTop, childRight, childBottom);
    }

From source file:net.robotmedia.acv.ui.widget.OcrLayout.java

public boolean createTriggerCaptureBox(Point pt) {
    final int IDEAL_CAP_WIDTH = 100;
    final int IDEAL_CAP_HALF_WIDTH = IDEAL_CAP_WIDTH / 2;
    final int IDEAL_CAP_HEIGHT = 450;
    final int IDEAL_CAP_ABOVE_PT = 35;
    final int IDEAL_LOOKAHEAD = 30;
    final int FALLBACK_WIDTH = 70;
    final int FALLBACK_HEIGHT = 200;
    final int FALLBACK_ABOVE_PT = 10;

    View v = this.comicView;

    if (v != null) {
        try {/*from www  . j  av  a 2s  .  c  o m*/
            // Only create the bitmap that contains the entire screen once
            if (this.lastScreenshot == null) {
                this.lastScreenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
                this.captureCanvas = new Canvas(this.lastScreenshot);
            }

            v.layout(0, 0, v.getWidth(), v.getHeight());

            // Draw the comic view to the lastScreenshot bitmap
            v.draw(this.captureCanvas);

            Rect cropRect = new Rect();

            if (this.textOrientation == TEXT_ORIENTATION_HORIZONTAL) {
                cropRect.left = Math.max(0, pt.x - IDEAL_CAP_ABOVE_PT);
                cropRect.top = Math.max(0, pt.y - IDEAL_CAP_HALF_WIDTH);
                cropRect.right = Math.min(v.getWidth(), pt.x + IDEAL_CAP_HEIGHT);
                cropRect.bottom = Math.min(v.getHeight(), pt.y + IDEAL_CAP_HALF_WIDTH);
            } else // Vertical or Auto
            {
                cropRect.left = Math.max(0, pt.x - IDEAL_CAP_HALF_WIDTH);
                cropRect.top = Math.max(0, pt.y - IDEAL_CAP_ABOVE_PT);
                cropRect.right = Math.min(v.getWidth(), pt.x + IDEAL_CAP_HALF_WIDTH);
                cropRect.bottom = Math.min(v.getHeight(), pt.y + IDEAL_CAP_HEIGHT);
            }

            // Get bitmap that contains the area to crop
            Bitmap cropBmp = Bitmap.createBitmap(this.lastScreenshot, cropRect.left, cropRect.top,
                    cropRect.width(), cropRect.height());

            // Get the click point relative to the cropped area
            Point ptInCropRect = new Point(pt.x - cropRect.left, pt.y - cropRect.top);

            // Crop
            Pix pixs = ReadFile.readBitmap(cropBmp);

            if (pixs == null) {
                return false;
            }

            cropBmp.recycle();

            // Convert to grayscale
            pixs = Convert.convertTo8(pixs);

            if (pixs == null) {
                return false;
            }

            Pix tempOtsu = Binarize.otsuAdaptiveThreshold(pixs, 2000, 2000, 0, 0, 0.0f);

            if (tempOtsu == null) {
                return false;
            }

            float ave = 0.0f;

            // Get the average intensity of the pixels around the click point
            if (this.textOrientation == TEXT_ORIENTATION_HORIZONTAL) {
                ave = Pix.averageInRect(tempOtsu, (int) (ptInCropRect.x * 0.9),
                        (int) (ptInCropRect.y - (tempOtsu.getHeight() * 0.95) / 2.0),
                        (int) (tempOtsu.getWidth() * 0.25), (int) (tempOtsu.getHeight() * 0.95));
            } else // Vertical or Auto
            {
                ave = Pix.averageInRect(tempOtsu, (int) (ptInCropRect.x - (tempOtsu.getWidth() * 0.95) / 2.0),
                        (int) (ptInCropRect.y * 0.9), (int) (tempOtsu.getWidth() * 0.95),
                        (int) (tempOtsu.getHeight() * 0.25));
            }

            tempOtsu.recycle();

            // If background is dark
            if (ave >= 0.51f) {
                // Negate image
                boolean invertStatus = pixs.invert();

                if (!invertStatus) {
                    return false;
                }
            }

            // Blur to reduce noise
            pixs = Convolve.blockconvGray(pixs, 1, 1);

            if (pixs == null) {
                return false;
            }

            // Apply unsharp mask
            pixs = Enhance.unsharpMasking(pixs, 5, 2.5f);

            if (pixs == null) {
                return false;
            }

            // Binarize
            pixs = Binarize.otsuAdaptiveThreshold(pixs, 2000, 2000, 0, 0, 0.0f);

            if (pixs == null) {
                return false;
            }

            // Remove black pixels connected to the border.
            // This eliminates annoying things like text bubbles.
            pixs = Seedfill.removeBorderConnComps(pixs, 8);

            if (pixs == null) {
                return false;
            }

            // Find the black pixel closest to the click point
            Point nearestPixel = LeptUtils.findNearestBlackPixel(pixs, ptInCropRect.x, ptInCropRect.y, 40);

            // Get a bounding box surrounding the clicked text
            Rect boundingBox = BoundingTextRect.getBoundingRect(pixs, nearestPixel.x, nearestPixel.y,
                    (this.textOrientation != TEXT_ORIENTATION_HORIZONTAL), IDEAL_LOOKAHEAD);

            // Form the capture box size and position based on click point and bounding box
            this.captureBox = new Rect();
            this.captureBox.left = pt.x - ptInCropRect.x + boundingBox.left;
            this.captureBox.top = pt.y - ptInCropRect.y + boundingBox.top;
            this.captureBox.right = this.captureBox.left + boundingBox.width();
            this.captureBox.bottom = this.captureBox.top + boundingBox.height();

            // If could not find adequate bounding rectangle, fallback to a default size
            if (this.captureBox.width() <= 2 || this.captureBox.height() <= 2) {
                if (this.textOrientation == TEXT_ORIENTATION_HORIZONTAL) {
                    this.captureBox = new Rect();
                    this.captureBox.left = Math.max(0, pt.x - FALLBACK_ABOVE_PT);
                    this.captureBox.top = Math.max(0, pt.y - FALLBACK_WIDTH / 2);
                    this.captureBox.right = Math.min(v.getWidth(), pt.x + FALLBACK_HEIGHT);
                    this.captureBox.bottom = Math.min(v.getHeight(), pt.y + FALLBACK_WIDTH / 2);
                } else // Vertical or Auto
                {
                    this.captureBox = new Rect();
                    this.captureBox.left = Math.max(0, pt.x - FALLBACK_WIDTH / 2);
                    this.captureBox.top = Math.max(0, pt.y - FALLBACK_ABOVE_PT);
                    this.captureBox.right = Math.min(v.getWidth(), pt.x + FALLBACK_WIDTH / 2);
                    this.captureBox.bottom = Math.min(v.getHeight(), pt.y + FALLBACK_HEIGHT);
                }
            }

            pixs.recycle();
        } catch (Exception e) {
            // If we're here, it's probably an out-of-memory exception
            Log.e(LOG_TAG, "Exception in processTriggerCapture()! " + e);
            return false;
        }
    }

    this.isTriggerCapture = true;

    return true;
}

From source file:cc.flydev.launcher.Page.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (!mIsDataReady || getChildCount() == 0) {
        return;// ww w  .  j ava 2  s. c  om
    }

    if (DEBUG)
        Log.d(TAG, "PagedView.onLayout()");
    final int childCount = getChildCount();

    int screenWidth = getViewportWidth();

    int offsetX = getViewportOffsetX();
    int offsetY = getViewportOffsetY();

    // Update the viewport offsets
    mViewport.offset(offsetX, offsetY);

    final boolean isRtl = isLayoutRtl();

    final int startIndex = isRtl ? childCount - 1 : 0;
    final int endIndex = isRtl ? -1 : childCount;
    final int delta = isRtl ? -1 : 1;

    int verticalPadding = getPaddingTop() + getPaddingBottom();

    int childLeft = mFirstChildLeft = offsetX + (screenWidth - getChildWidth(startIndex)) / 2;
    if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
        mPageScrolls = new int[getChildCount()];
    }

    for (int i = startIndex; i != endIndex; i += delta) {
        final View child = getPageAt(i);
        if (child.getVisibility() != View.GONE) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childTop;
            if (lp.isFullScreenPage) {
                childTop = offsetY;
            } else {
                childTop = offsetY + getPaddingTop() + mInsets.top;
                if (mCenterPagesVertically) {
                    childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding
                            - child.getMeasuredHeight()) / 2;
                }
            }

            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();

            if (DEBUG)
                Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
            child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + childHeight);

            // We assume the left and right padding are equal, and hence center the pages
            // horizontally
            int scrollOffset = (getViewportWidth() - childWidth) / 2;
            mPageScrolls[i] = childLeft - scrollOffset - offsetX;

            if (i != endIndex - delta) {
                childLeft += childWidth + scrollOffset;
                int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
                childLeft += nextScrollOffset;
            }
        }
    }

    if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
        setHorizontalScrollBarEnabled(false);
        updateCurrentPageScroll();
        setHorizontalScrollBarEnabled(true);
        mFirstLayout = false;
    }

    if (childCount > 0) {
        final int index = isLayoutRtl() ? 0 : childCount - 1;
        mMaxScrollX = getScrollForPage(index);
    } else {
        mMaxScrollX = 0;
    }

    if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() && !mDeferringForDelete) {
        if (mRestorePage != INVALID_RESTORE_PAGE) {
            setCurrentPage(mRestorePage);
            mRestorePage = INVALID_RESTORE_PAGE;
        } else {
            setCurrentPage(getNextPage());
        }
    }
    mChildCountOnLastLayout = getChildCount();

    if (isReordering(true)) {
        updateDragViewTranslationDuringDrag();
    }
}

From source file:android.support.designox.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 ww  w .j a  va 2  s.c o  m
 * @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 = mTempRect1;
    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 = mTempRect2;
    GravityCompat.apply(resolveGravity(lp.gravity), child.getMeasuredWidth(), child.getMeasuredHeight(), parent,
            out, layoutDirection);
    child.layout(out.left, out.top, out.right, out.bottom);
}

From source file:com.n2hsu.launcher.Page.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (!mIsDataReady || getChildCount() == 0) {
        return;/*www.java 2 s.co m*/
    }

    if (DEBUG)
        Log.d(TAG, "PagedView.onLayout()");
    final int childCount = getChildCount();

    int screenWidth = getViewportWidth();

    int offsetX = getViewportOffsetX();
    int offsetY = getViewportOffsetY();

    // Update the viewport offsets
    mViewport.offset(offsetX, offsetY);

    final boolean isRtl = isLayoutRtl();

    final int startIndex = isRtl ? childCount - 1 : 0;
    final int endIndex = isRtl ? -1 : childCount;
    final int delta = isRtl ? -1 : 1;

    int verticalPadding = getPaddingTop() + getPaddingBottom();

    int childLeft = mFirstChildLeft = offsetX + (screenWidth - getChildWidth(startIndex)) / 2;
    if (mPageScrolls == null || getChildCount() != mChildCountOnLastLayout) {
        mPageScrolls = new int[getChildCount()];
    }

    for (int i = startIndex; i != endIndex; i += delta) {
        final View child = getPageAt(i);
        if (child.getVisibility() != View.GONE) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childTop;
            if (lp.isFullScreenPage) {
                childTop = offsetY;
            } else {
                childTop = offsetY + getPaddingTop() + mInsets.top;
                if (mCenterPagesVertically) {
                    childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding
                            - child.getMeasuredHeight()) / 2;
                }
            }

            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();

            if (DEBUG)
                Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
            child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + childHeight);

            // We assume the left and right padding are equal, and hence
            // center the pages
            // horizontally
            int scrollOffset = (getViewportWidth() - childWidth) / 2;
            mPageScrolls[i] = childLeft - scrollOffset - offsetX;

            if (i != endIndex - delta) {
                childLeft += childWidth + scrollOffset;
                int nextScrollOffset = (getViewportWidth() - getChildWidth(i + delta)) / 2;
                childLeft += nextScrollOffset;
            }
        }
    }

    if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
        setHorizontalScrollBarEnabled(false);
        updateCurrentPageScroll();
        setHorizontalScrollBarEnabled(true);
        mFirstLayout = false;
    }

    if (childCount > 0) {
        final int index = isLayoutRtl() ? 0 : childCount - 1;
        mMaxScrollX = getScrollForPage(index);
    } else {
        mMaxScrollX = 0;
    }

    if (mScroller.isFinished() && mChildCountOnLastLayout != getChildCount() && !mDeferringForDelete) {
        if (mRestorePage != INVALID_RESTORE_PAGE) {
            setCurrentPage(mRestorePage);
            mRestorePage = INVALID_RESTORE_PAGE;
        } else {
            setCurrentPage(getNextPage());
        }
    }
    mChildCountOnLastLayout = getChildCount();

    if (isReordering(true)) {
        updateDragViewTranslationDuringDrag();
    }
}