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.tandong.sa.drag.DragGridViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int childCount = getChildCount();
    mPageCount = (childCount + mPageSize - 1) / mPageSize;
    mGridWidth = (getWidth() - mPaddingLeft - mPaddingRight - (mColCount - 1) * mGridGap) / mColCount;
    mGridHeight = (getHeight() - mPaddingTop - mPaddingButtom - (mRowCount - 1) * mGridGap) / mRowCount;
    mGridWidth = mGridHeight = Math.min(mGridWidth, mGridHeight);
    mMaxOverScrollSize = mGridWidth / 2;
    mEdgeSize = mGridWidth / 2;//from www .  ja  v a2  s.c o  m
    newPositions.clear();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final Rect rect = getRectByPosition(i);
        child.measure(MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY));
        DEBUG_LOG("child.layout position=" + i + ", rect=" + rect);
        child.layout(rect.left, rect.top, rect.right, rect.bottom);
        newPositions.add(-1);
    }
}

From source file:com.learn.mobile.customview.DSwipeRefreshLayout.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int width = getMeasuredWidth();
    final int height = getMeasuredHeight();
    if (getChildCount() == 0) {
        return;//  w  ww . jav a 2 s .c o m
    }
    if (mTarget == null) {
        ensureTarget();
    }
    if (mTarget == null) {
        return;
    }
    final View child = mTarget;
    final int childLeft = getPaddingLeft();
    final int childTop = getPaddingTop();
    final int childWidth = width - getPaddingLeft() - getPaddingRight();
    final int childHeight = height - getPaddingTop() - getPaddingBottom();
    child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
    int circleWidth = mCircleView.getMeasuredWidth();
    int circleHeight = mCircleView.getMeasuredHeight();
    mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop, (width / 2 + circleWidth / 2),
            mCurrentTargetOffsetTop + circleHeight);

    // TODO Display Custom Swipe Refresh Layout
    updateLoadMorePosition(width, height);
}

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

/**
 * Crate a bitmap from the given view//w  w  w  .j a  v  a2  s. c o  m
 */
private Bitmap getViewBitmap(View view) {
    // setContainerAttrs();

    //Get the dimensions of the view so we can re-layout the view at its current size
    //and create a bitmap of the same size
    int width = view.getMeasuredWidth();
    int height = view.getMeasuredHeight();

    int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

    //Cause the view to re-layout
    view.measure(measuredWidth, measuredHeight);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    //Create a bitmap backed Canvas to draw the view into
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    //Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
    view.draw(c);
    return b;
}

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

private static void applyBoundsToMountContent(Object content, int left, int top, int right, int bottom,
        boolean force) {
    assertMainThread();// www .j  a  v a 2  s . c o m

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

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

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

From source file:jp.shuri.yamanetoshi.verticalviewpager.VerticalViewPager.java

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

    final int count = getChildCount();
    final int height = b - t;

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        ItemInfo ii;
        if (child.getVisibility() != GONE && (ii = infoForChild(child)) != null) {
            int loff = height * ii.position;
            int childLeft = getPaddingLeft();
            int childTop = getPaddingTop() + loff;
            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());
        }
    }
}

From source file:android.support.wear.widget.drawer.WearableDrawerLayout.java

@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
        int oldRight, int oldBottom) {
    if (v == mTopDrawerView) {
        // Layout the top drawer base on the openedPercent. It is initially hidden.
        final float openedPercent = mTopDrawerView.getOpenedPercent();
        final int height = v.getHeight();
        final int childTop = -height + (int) (height * openedPercent);
        v.layout(v.getLeft(), childTop, v.getRight(), childTop + height);
    } else if (v == mBottomDrawerView) {
        // Layout the bottom drawer base on the openedPercent. It is initially hidden.
        final float openedPercent = mBottomDrawerView.getOpenedPercent();
        final int height = v.getHeight();
        final int childTop = (int) (getHeight() - height * openedPercent);
        v.layout(v.getLeft(), childTop, v.getRight(), childTop + height);
    }//  ww  w .  j  a v  a  2 s.c o m
}

From source file:com.coco.draggablegridviewpager.DraggableGridViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int childCount = getChildCount();
    mPageCount = (childCount + mPageSize - 1) / mPageSize;
    mGridWidth = (getWidth() - mPaddingLeft - mPaddingRight - (mColCount - 1) * mGridGap) / mColCount;
    mGridHeight = (getHeight() - mPaddingTop - mPaddingButtom - (mRowCount - 1) * mGridGap) / mRowCount;
    mGridWidth = mGridHeight = Math.min(mGridWidth, mGridHeight);
    mMaxOverScrollSize = mGridWidth / 2;
    mEdgeSize = mGridWidth / 2;/*from  w ww .  j ava2 s.c  om*/
    newPositions.clear();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final Rect rect = getRectByPosition(i);
        child.measure(MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY));
        DEBUG_LOG("child.layout position=" + i + ", rect=" + rect);
        child.layout(rect.left, rect.top, rect.right, rect.bottom);
        newPositions.add(-1);
    }
    if (mCurItem > 0 && mCurItem < mPageCount) {
        final int curItem = mCurItem;
        mCurItem = 0;
        setCurrentItem(curItem);
    }
}

From source file:com.viettel.image.zoom.ZoomViewPaper.java

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

    final int count = getChildCount();
    final int width = r - l;

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        ItemInfo ii;
        if (child.getVisibility() != GONE && (ii = infoForChild(child)) != null) {
            int loff = width * ii.position;
            int childLeft = getPaddingLeft() + loff;
            int childTop = getPaddingTop();
            if (DEBUG)
                VTLog.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());
        }
    }
}

From source file:com.coco.draggablegridviewpager.DraggableGridViewPager.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) {
        // Don't handle edge touches immediately -- they may actually belong to one of our
        // descendants.
        return false;
    }/*from   w ww.  java 2  s  . c om*/

    if (mPageCount <= 0) {
        // Nothing to present or scroll; nothing to touch.
        return false;
    }

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();
    boolean needsInvalidate = false;

    switch (action & MotionEventCompat.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        mScroller.abortAnimation();
        // Remember where the motion event started
        mLastMotionX = mInitialMotionX = ev.getX();
        mLastMotionY = mInitialMotionY = ev.getY();
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);

        DEBUG_LOG("Down at " + mLastMotionX + "," + mLastMotionY + " mIsBeingDragged=" + mIsBeingDragged
                + " mIsUnableToDrag=" + mIsUnableToDrag);

        if (!mIsBeingDragged && mScrollState == SCROLL_STATE_IDLE) {
            mLastPosition = getPositionByXY((int) mLastMotionX, (int) mLastMotionY);
        } else {
            mLastPosition = -1;
        }
        if (mLastPosition >= 0) {
            mLastDownTime = System.currentTimeMillis();
        } else {
            mLastDownTime = Long.MAX_VALUE;
        }
        DEBUG_LOG("Down at mLastPosition=" + mLastPosition);
        mLastDragged = -1;
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        if (mLastDragged >= 0) {
            // change draw location of dragged visual
            final View v = getChildAt(mLastDragged);
            final int l = getScrollX() + (int) x - v.getWidth() / 2;
            final int t = getScrollY() + (int) y - v.getHeight() / 2;
            v.layout(l, t, l + v.getWidth(), t + v.getHeight());

            // check for new target hover
            if (mScrollState == SCROLL_STATE_IDLE) {
                final int target = getTargetByXY((int) x, (int) y);
                if (target != -1 && mLastTarget != target) {
                    animateGap(target);
                    mLastTarget = target;
                    DEBUG_LOG("Moved to mLastTarget=" + mLastTarget);
                }
                // edge holding
                final int edge = getEdgeByXY((int) x, (int) y);
                if (mLastEdge == -1) {
                    if (edge != mLastEdge) {
                        mLastEdge = edge;
                        mLastEdgeTime = System.currentTimeMillis();
                    }
                } else {
                    if (edge != mLastEdge) {
                        mLastEdge = -1;
                    } else {
                        if ((System.currentTimeMillis() - mLastEdgeTime) >= EDGE_HOLD_DURATION) {
                            performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                            triggerSwipe(edge);
                            mLastEdge = -1;
                        }
                    }
                }
            }
        } else if (!mIsBeingDragged) {
            final float xDiff = Math.abs(x - mLastMotionX);
            final float yDiff = Math.abs(y - mLastMotionY);
            DEBUG_LOG("Moved to " + x + "," + y + " diff=" + xDiff + "," + yDiff);

            if (xDiff > mTouchSlop && xDiff > yDiff) {
                DEBUG_LOG("Starting drag!");
                mIsBeingDragged = true;
                requestParentDisallowInterceptTouchEvent(true);
                mLastMotionX = x - mInitialMotionX > 0 ? mInitialMotionX + mTouchSlop
                        : mInitialMotionX - mTouchSlop;
                mLastMotionY = y;
                setScrollState(SCROLL_STATE_DRAGGING);
                setScrollingCacheEnabled(true);
            }
        }
        // Not else! Note that mIsBeingDragged can be set above.
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            needsInvalidate |= performDrag(x);
        } else if (mLastPosition >= 0) {
            final int currentPosition = getPositionByXY((int) x, (int) y);
            DEBUG_LOG("Moved to currentPosition=" + currentPosition);
            if (currentPosition == mLastPosition) {
                if ((System.currentTimeMillis() - mLastDownTime) >= LONG_CLICK_DURATION) {
                    if (onItemLongClick(currentPosition)) {
                        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
                        mLastDragged = mLastPosition;
                        requestParentDisallowInterceptTouchEvent(true);
                        mLastTarget = -1;
                        animateDragged();
                        mLastPosition = -1;
                    }
                    mLastDownTime = Long.MAX_VALUE;
                }
            } else {
                mLastPosition = -1;
            }
        }
        break;
    }
    case MotionEvent.ACTION_UP: {
        DEBUG_LOG("Touch up!!!");
        final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, pointerIndex);
        final float y = MotionEventCompat.getY(ev, pointerIndex);

        if (mLastDragged >= 0) {
            rearrange();
        } else if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
            final int width = getWidth();
            final int scrollX = getScrollX();
            final int currentPage = scrollX / width;
            final int offsetPixels = scrollX - currentPage * width;
            final float pageOffset = (float) offsetPixels / (float) width;
            final int totalDelta = (int) (x - mInitialMotionX);
            int nextPage = determineTargetPage(currentPage, pageOffset, initialVelocity, totalDelta);
            setCurrentItemInternal(nextPage, true, true, initialVelocity);

            mActivePointerId = INVALID_POINTER;
            endDrag();
        } else if (mLastPosition >= 0) {
            final int currentPosition = getPositionByXY((int) x, (int) y);
            DEBUG_LOG("Touch up!!! currentPosition=" + currentPosition);
            if (currentPosition == mLastPosition) {
                onItemClick(currentPosition);
            }
        }
        break;
    }
    case MotionEvent.ACTION_CANCEL:
        DEBUG_LOG("Touch cancel!!!");
        if (mLastDragged >= 0) {
            rearrange();
        } else if (mIsBeingDragged) {
            scrollToItem(mCurItem, true, 0, false);
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        final float x = MotionEventCompat.getX(ev, index);
        mLastMotionX = x;
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }
    if (needsInvalidate) {
        ViewCompat.postInvalidateOnAnimation(this);
    }
    return true;
}

From source file:com.android.hcframe.DraggableGridViewPager.java

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int childCount = getChildCount();
    mPageCount = (childCount + mPageSize - 1) / mPageSize;
    mGridWidth = (getWidth() - mPaddingLeft - mPaddingRight - (mColCount - 1) * mGridGap) / mColCount;
    mGridHeight = (getHeight() - mPaddingTop - mPaddingButtom - (mRowCount - 1) * mGridGap) / mRowCount;
    //      mGridWidth = mGridHeight = Math.min(mGridWidth, mGridHeight);
    mMaxOverScrollSize = mGridWidth / 2;
    mEdgeSize = mGridWidth / 2;//from w ww. j  ava2  s .c o  m
    newPositions.clear();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final Rect rect = getRectByPosition(i);
        child.measure(MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY));
        child.layout(rect.left, rect.top, rect.right, rect.bottom);
        newPositions.add(-1);
    }
    if (mCurItem > 0 && mCurItem < mPageCount) {
        final int curItem = mCurItem;
        mCurItem = 0;
        setCurrentItem(curItem);
    }
}