List of usage examples for android.view View getMeasuredWidth
public final int getMeasuredWidth()
From source file:com.example.newviewsplayground.WrappingSlidingPaneLayout.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int widthMode = MeasureSpec.getMode(widthMeasureSpec); final int widthSize = MeasureSpec.getSize(widthMeasureSpec); final int heightMode = MeasureSpec.getMode(heightMeasureSpec); final int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException("Width must have an exact value or MATCH_PARENT"); } else if (heightMode == MeasureSpec.UNSPECIFIED) { throw new IllegalStateException("Height must not be UNSPECIFIED"); }// w ww . ja v a 2 s.c o m int layoutHeight = 0; int maxLayoutHeight = -1; switch (heightMode) { case MeasureSpec.EXACTLY: layoutHeight = maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom(); break; case MeasureSpec.AT_MOST: maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom(); break; } float weightSum = 0; boolean canSlide = false; int widthRemaining = widthSize - getPaddingLeft() - getPaddingRight(); final int childCount = getChildCount(); if (childCount > 2) { Log.e(TAG, "onMeasure: More than two child views are not supported."); } // We'll find the current one below. mSlideableView = null; // First pass. Measure based on child LayoutParams width/height. // Weight will incur a second pass. for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (child.getVisibility() == GONE) { lp.dimWhenOffset = false; continue; } if (lp.weight > 0) { weightSum += lp.weight; // If we have no width, weight is the only contributor to the final size. // Measure this view on the weight pass only. if (lp.width == 0) continue; } int childWidthSpec; final int horizontalMargin = lp.leftMargin + lp.rightMargin; if (lp.width == LayoutParams.WRAP_CONTENT) { childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, MeasureSpec.AT_MOST); } else if (lp.width == LayoutParams.FILL_PARENT) { childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, MeasureSpec.EXACTLY); } else { childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); } int childHeightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); } else if (lp.height == LayoutParams.FILL_PARENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(childWidthSpec, childHeightSpec); final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); if (heightMode == MeasureSpec.AT_MOST && childHeight > layoutHeight) { layoutHeight = Math.min(childHeight, maxLayoutHeight); } widthRemaining -= childWidth; canSlide |= lp.slideable = widthRemaining < 0; if (lp.slideable) { mSlideableView = child; } } // Resolve weight and make sure non-sliding panels are smaller than the full screen. if (canSlide || weightSum > 0) { final int fixedPanelWidthLimit = widthSize - mOverhangSize; for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final boolean skippedFirstPass = lp.width == 0 && lp.weight > 0; final int measuredWidth = skippedFirstPass ? 0 : child.getMeasuredWidth(); if (canSlide && child != mSlideableView) { if (lp.width < 0 && (measuredWidth > fixedPanelWidthLimit || lp.weight > 0)) { // Fixed panels in a sliding configuration should // be clamped to the fixed panel limit. final int childHeightSpec; if (skippedFirstPass) { // Do initial height measurement if we skipped measuring this view // the first time around. if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); } else if (lp.height == LayoutParams.FILL_PARENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } } else { childHeightSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY); } final int childWidthSpec = MeasureSpec.makeMeasureSpec(fixedPanelWidthLimit, MeasureSpec.EXACTLY); child.measure(childWidthSpec, childHeightSpec); } } else if (lp.weight > 0) { int childHeightSpec; if (lp.width == 0) { // This was skipped the first time; figure out a real height spec. if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); } else if (lp.height == LayoutParams.FILL_PARENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } } else { childHeightSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY); } if (canSlide) { // Consume available space final int horizontalMargin = lp.leftMargin + lp.rightMargin; final int newWidth = widthSize - horizontalMargin; final int childWidthSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY); if (measuredWidth != newWidth) { child.measure(childWidthSpec, childHeightSpec); } } else { // Distribute the extra width proportionally similar to LinearLayout final int widthToDistribute = Math.max(0, widthRemaining); final int addedWidth = (int) (lp.weight * widthToDistribute / weightSum); final int childWidthSpec = MeasureSpec.makeMeasureSpec(measuredWidth + addedWidth, MeasureSpec.EXACTLY); child.measure(childWidthSpec, childHeightSpec); } } } } setMeasuredDimension(widthSize, layoutHeight); mCanSlide = canSlide; if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && !canSlide) { // Cancel scrolling in progress, it's no longer relevant. mDragHelper.abort(); } }
From source file:com.google.android.flexbox.FlexboxLayout.java
/** * Expand the flex items along the main axis based on the individual flexGrow attribute. * * @param flexLine the flex line to which flex items belong * @param flexDirection the flexDirection value for this FlexboxLayout * @param maxMainSize the maximum main size. Expanded main size will be this size * @param paddingAlongMainAxis the padding value along the main axis * @param startIndex the start index of the children views to be expanded. This index * needs to/*from w w w . j a v a2 s . co m*/ * be an absolute index in the flex container (FlexboxLayout), * not the relative index in the flex line. * @return the next index, the next flex line's first flex item starts from the returned index * @see #getFlexDirection() * @see #setFlexDirection(int) * @see LayoutParams#flexGrow */ private int expandFlexItems(FlexLine flexLine, @FlexDirection int flexDirection, int maxMainSize, int paddingAlongMainAxis, int startIndex) { int childIndex = startIndex; if (flexLine.mTotalFlexGrow <= 0 || maxMainSize < flexLine.mMainSize) { childIndex += flexLine.mItemCount; return childIndex; } int sizeBeforeExpand = flexLine.mMainSize; boolean needsReexpand = false; float unitSpace = (maxMainSize - flexLine.mMainSize) / flexLine.mTotalFlexGrow; flexLine.mMainSize = paddingAlongMainAxis + flexLine.mDividerLengthInMainSize; float accumulatedRoundError = 0; for (int i = 0; i < flexLine.mItemCount; i++) { View child = getReorderedChildAt(childIndex); if (child == null) { continue; } else if (child.getVisibility() == View.GONE) { childIndex++; continue; } LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (isMainAxisDirectionHorizontal(flexDirection)) { // The direction of the main axis is horizontal if (!mChildrenFrozen[childIndex]) { float rawCalculatedWidth = child.getMeasuredWidth() + unitSpace * lp.flexGrow; if (i == flexLine.mItemCount - 1) { rawCalculatedWidth += accumulatedRoundError; accumulatedRoundError = 0; } int newWidth = Math.round(rawCalculatedWidth); if (newWidth > lp.maxWidth) { // This means the child can't expand beyond the value of the maxWidth attribute. // To adjust the flex line length to the size of maxMainSize, remaining // positive free space needs to be re-distributed to other flex items // (children views). In that case, invoke this method again with the same // startIndex. needsReexpand = true; newWidth = lp.maxWidth; mChildrenFrozen[childIndex] = true; flexLine.mTotalFlexGrow -= lp.flexGrow; } else { accumulatedRoundError += (rawCalculatedWidth - newWidth); if (accumulatedRoundError > 1.0) { newWidth += 1; accumulatedRoundError -= 1.0; } else if (accumulatedRoundError < -1.0) { newWidth -= 1; accumulatedRoundError += 1.0; } } child.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY)); } flexLine.mMainSize += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } else { // The direction of the main axis is vertical if (!mChildrenFrozen[childIndex]) { float rawCalculatedHeight = child.getMeasuredHeight() + unitSpace * lp.flexGrow; if (i == flexLine.mItemCount - 1) { rawCalculatedHeight += accumulatedRoundError; accumulatedRoundError = 0; } int newHeight = Math.round(rawCalculatedHeight); if (newHeight > lp.maxHeight) { // This means the child can't expand beyond the value of the maxHeight // attribute. // To adjust the flex line length to the size of maxMainSize, remaining // positive free space needs to be re-distributed to other flex items // (children views). In that case, invoke this method again with the same // startIndex. needsReexpand = true; newHeight = lp.maxHeight; mChildrenFrozen[childIndex] = true; flexLine.mTotalFlexGrow -= lp.flexGrow; } else { accumulatedRoundError += (rawCalculatedHeight - newHeight); if (accumulatedRoundError > 1.0) { newHeight += 1; accumulatedRoundError -= 1.0; } else if (accumulatedRoundError < -1.0) { newHeight -= 1; accumulatedRoundError += 1.0; } } child.measure(MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); } flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; } childIndex++; } if (needsReexpand && sizeBeforeExpand != flexLine.mMainSize) { // Re-invoke the method with the same startIndex to distribute the positive free space // that wasn't fully distributed (because of maximum length constraint) expandFlexItems(flexLine, flexDirection, maxMainSize, paddingAlongMainAxis, startIndex); } return childIndex; }
From source file:cc.flydev.launcher.Page.java
protected void getVisiblePages(int[] range) { final int pageCount = getChildCount(); mTmpIntPoint[0] = mTmpIntPoint[1] = 0; range[0] = -1;/*from w w w . j a v a2 s . c om*/ range[1] = -1; if (pageCount > 0) { int viewportWidth = getViewportWidth(); int curScreen = 0; int count = getChildCount(); for (int i = 0; i < count; i++) { View currPage = getPageAt(i); mTmpIntPoint[0] = 0; Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false); if (mTmpIntPoint[0] > viewportWidth) { if (range[0] == -1) { continue; } else { break; } } mTmpIntPoint[0] = currPage.getMeasuredWidth(); Utilities.getDescendantCoordRelativeToParent(currPage, this, mTmpIntPoint, false); if (mTmpIntPoint[0] < 0) { if (range[0] == -1) { continue; } else { break; } } curScreen = i; if (range[0] < 0) { range[0] = curScreen; } } range[1] = curScreen; } else { range[0] = -1; range[1] = -1; } }
From source file:com.android.tv.settings.dialog.SettingsLayoutFragment.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;//ww w. j av a 2s.c o m // 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); // We need to defer the remainder of the animation preparation until the first layout has // occurred, as we don't yet know the final location of the icon. contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { contentView.getViewTreeObserver().removeOnGlobalLayoutListener(this); // if we buildLayer() at this time, the texture is // actually not created delay a little so we can make // sure all hardware layer is created before animation, // in that way we can avoid the jittering of start // animation contentView.postOnAnimationDelayed(mEntryAnimationRunnable, mAnimateDelay); } Runnable mEntryAnimationRunnable = 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(mAnimateInDuration); oa.setStartDelay(mSecondaryAnimateDelay); oa.setInterpolator(new DecelerateInterpolator(1.0f)); oa.start(); boolean isRtl = ViewCompat.getLayoutDirection(contentView) == View.LAYOUT_DIRECTION_RTL; int startDist = isRtl ? mSlideInDistance : -mSlideInDistance; 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); } }; }); }
From source file:com.example.view.VerticalViewPager.java
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mInLayout = true;/*from w w w.java 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 scrollY = getScrollY(); 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) { // XXX isDecoralse?? 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; } /* end of switch */ 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; } /* end of switch */ // XXX ???? childTop += scrollY; decorCount++; child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight()); } else if ((ii = infoForChild(child)) != null) { // XXX ???iewPager?? int toff = (height + mPageMargin) * ii.position; childLeft = paddingLeft; childTop = paddingTop + toff; 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()); } /* end of if */ } /* end of if */ } /* end of for */ // XXX ????? mLeftPageBounds = paddingLeft; mRightPageBounds = width - paddingRight; mDecorChildCount = decorCount; mFirstLayout = false; }
From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java
/** * Shrink the flex items along the main axis based on the individual flexShrink attribute. * * @param widthMeasureSpec the horizontal space requirements as imposed by the parent * @param heightMeasureSpec the vertical space requirements as imposed by the parent * @param flexLine the flex line to which flex items belong * @param flexDirection the flexDirection value for this FlexboxLayout * @param maxMainSize the maximum main size. Shrank main size will be this size * @param paddingAlongMainAxis the padding value along the main axis * @param startIndex the start index of the children views to be shrank. This index * needs to//from w w w. j av a 2s. c om * be an absolute index in the flex container (FlexboxLayout), * not the relative index in the flex line. * @param calledRecursively true if this method is called recursively, false otherwise * @return the next index, the next flex line's first flex item starts from the returned index * @see #getFlexDirection() * @see #setFlexDirection(int) * @see LayoutParams#flexShrink */ private int shrinkFlexItems(int widthMeasureSpec, int heightMeasureSpec, FlexLine flexLine, @FlexDirection int flexDirection, int maxMainSize, int paddingAlongMainAxis, int startIndex, boolean calledRecursively) { int childIndex = startIndex; int sizeBeforeShrink = flexLine.mMainSize; if (flexLine.mTotalFlexShrink <= 0 || maxMainSize > flexLine.mMainSize) { childIndex += flexLine.mItemCount; return childIndex; } boolean needsReshrink = false; float unitShrink = (flexLine.mMainSize - maxMainSize) / flexLine.mTotalFlexShrink; float accumulatedRoundError = 0; flexLine.mMainSize = paddingAlongMainAxis + flexLine.mDividerLengthInMainSize; // Setting the cross size of the flex line as the temporal value since the cross size of // each flex item may be changed from the initial calculation // (in the measureHorizontal/measureVertical method) even this method is part of the main // size determination. // E.g. If a TextView's layout_width is set to 0dp, layout_height is set to wrap_content, // and layout_flexGrow is set to 1, the TextView is trying to expand to the vertical // direction to enclose its content (in the measureHorizontal method), but // the width will be expanded in this method. In that case, the height needs to be measured // again with the expanded width. int largestCrossSize = 0; if (!calledRecursively) { flexLine.mCrossSize = Integer.MIN_VALUE; } for (int i = 0; i < flexLine.mItemCount; i++) { View child = getReorderedChildAt(childIndex); if (child == null) { continue; } else if (child.getVisibility() == View.GONE) { childIndex++; continue; } LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (isMainAxisDirectionHorizontal(flexDirection)) { // The direction of main axis is horizontal if (!mChildrenFrozen[childIndex]) { float rawCalculatedWidth = child.getMeasuredWidth() - unitShrink * lp.flexShrink; if (i == flexLine.mItemCount - 1) { rawCalculatedWidth += accumulatedRoundError; accumulatedRoundError = 0; } int newWidth = Math.round(rawCalculatedWidth); if (newWidth < lp.minWidth) { // This means the child doesn't have enough space to distribute the negative // free space. To adjust the flex line length down to the maxMainSize, remaining // negative free space needs to be re-distributed to other flex items // (children views). In that case, invoke this method again with the same // startIndex. needsReshrink = true; newWidth = lp.minWidth; mChildrenFrozen[childIndex] = true; flexLine.mTotalFlexShrink -= lp.flexShrink; } else { accumulatedRoundError += (rawCalculatedWidth - newWidth); if (accumulatedRoundError > 1.0) { newWidth += 1; accumulatedRoundError -= 1; } else if (accumulatedRoundError < -1.0) { newWidth -= 1; accumulatedRoundError += 1; } } int childHeightMeasureSpec = getChildHeightMeasureSpec(heightMeasureSpec, lp); child.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), childHeightMeasureSpec); largestCrossSize = Math.max(largestCrossSize, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); } flexLine.mMainSize += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } else { // The direction of main axis is vertical if (!mChildrenFrozen[childIndex]) { float rawCalculatedHeight = child.getMeasuredHeight() - unitShrink * lp.flexShrink; if (i == flexLine.mItemCount - 1) { rawCalculatedHeight += accumulatedRoundError; accumulatedRoundError = 0; } int newHeight = Math.round(rawCalculatedHeight); if (newHeight < lp.minHeight) { // Need to invoke this method again like the case flex direction is vertical needsReshrink = true; newHeight = lp.minHeight; mChildrenFrozen[childIndex] = true; flexLine.mTotalFlexShrink -= lp.flexShrink; } else { accumulatedRoundError += (rawCalculatedHeight - newHeight); if (accumulatedRoundError > 1.0) { newHeight += 1; accumulatedRoundError -= 1; } else if (accumulatedRoundError < -1.0) { newHeight -= 1; accumulatedRoundError += 1; } } int childWidthMeasureSpec = getChildWidthMeasureSpec(widthMeasureSpec, lp); child.measure(childWidthMeasureSpec, MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); largestCrossSize = Math.max(largestCrossSize, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; } flexLine.mCrossSize = Math.max(flexLine.mCrossSize, largestCrossSize); childIndex++; } if (needsReshrink && sizeBeforeShrink != flexLine.mMainSize) { // Re-invoke the method with the same startIndex to distribute the negative free space // that wasn't fully distributed (because some views length were not enough) shrinkFlexItems(widthMeasureSpec, heightMeasureSpec, flexLine, flexDirection, maxMainSize, paddingAlongMainAxis, startIndex, true); } return childIndex; }
From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java
/** * Expand the flex items along the main axis based on the individual flexGrow attribute. * * @param widthMeasureSpec the horizontal space requirements as imposed by the parent * @param heightMeasureSpec the vertical space requirements as imposed by the parent * @param flexLine the flex line to which flex items belong * @param flexDirection the flexDirection value for this FlexboxLayout * @param maxMainSize the maximum main size. Expanded main size will be this size * @param paddingAlongMainAxis the padding value along the main axis * @param startIndex the start index of the children views to be expanded. This index * needs to/*from w ww . ja v a 2 s.co m*/ * be an absolute index in the flex container (FlexboxLayout), * not the relative index in the flex line. * @param calledRecursively true if this method is called recursively, false otherwise * @return the next index, the next flex line's first flex item starts from the returned index * @see #getFlexDirection() * @see #setFlexDirection(int) * @see LayoutParams#flexGrow */ private int expandFlexItems(int widthMeasureSpec, int heightMeasureSpec, FlexLine flexLine, @FlexDirection int flexDirection, int maxMainSize, int paddingAlongMainAxis, int startIndex, boolean calledRecursively) { int childIndex = startIndex; if (flexLine.mTotalFlexGrow <= 0 || maxMainSize < flexLine.mMainSize) { childIndex += flexLine.mItemCount; return childIndex; } int sizeBeforeExpand = flexLine.mMainSize; boolean needsReexpand = false; float unitSpace = (maxMainSize - flexLine.mMainSize) / flexLine.mTotalFlexGrow; flexLine.mMainSize = paddingAlongMainAxis + flexLine.mDividerLengthInMainSize; // Setting the cross size of the flex line as the temporal value since the cross size of // each flex item may be changed from the initial calculation // (in the measureHorizontal/measureVertical method) even this method is part of the main // size determination. // E.g. If a TextView's layout_width is set to 0dp, layout_height is set to wrap_content, // and layout_flexGrow is set to 1, the TextView is trying to expand to the vertical // direction to enclose its content (in the measureHorizontal method), but // the width will be expanded in this method. In that case, the height needs to be measured // again with the expanded width. if (!calledRecursively) { flexLine.mCrossSize = Integer.MIN_VALUE; } int largestCrossSize = 0; float accumulatedRoundError = 0; for (int i = 0; i < flexLine.mItemCount; i++) { View child = getReorderedChildAt(childIndex); if (child == null) { continue; } else if (child.getVisibility() == View.GONE) { childIndex++; continue; } LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (isMainAxisDirectionHorizontal(flexDirection)) { // The direction of the main axis is horizontal if (!mChildrenFrozen[childIndex]) { float rawCalculatedWidth = child.getMeasuredWidth() + unitSpace * lp.flexGrow; if (i == flexLine.mItemCount - 1) { rawCalculatedWidth += accumulatedRoundError; accumulatedRoundError = 0; } int newWidth = Math.round(rawCalculatedWidth); if (newWidth > lp.maxWidth) { // This means the child can't expand beyond the value of the maxWidth attribute. // To adjust the flex line length to the size of maxMainSize, remaining // positive free space needs to be re-distributed to other flex items // (children views). In that case, invoke this method again with the same // startIndex. needsReexpand = true; newWidth = lp.maxWidth; mChildrenFrozen[childIndex] = true; flexLine.mTotalFlexGrow -= lp.flexGrow; } else { accumulatedRoundError += (rawCalculatedWidth - newWidth); if (accumulatedRoundError > 1.0) { newWidth += 1; accumulatedRoundError -= 1.0; } else if (accumulatedRoundError < -1.0) { newWidth -= 1; accumulatedRoundError += 1.0; } } int childHeightMeasureSpec = getChildHeightMeasureSpec(heightMeasureSpec, lp); child.measure(MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY), childHeightMeasureSpec); largestCrossSize = Math.max(largestCrossSize, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); } flexLine.mMainSize += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin; } else { // The direction of the main axis is vertical if (!mChildrenFrozen[childIndex]) { float rawCalculatedHeight = child.getMeasuredHeight() + unitSpace * lp.flexGrow; if (i == flexLine.mItemCount - 1) { rawCalculatedHeight += accumulatedRoundError; accumulatedRoundError = 0; } int newHeight = Math.round(rawCalculatedHeight); if (newHeight > lp.maxHeight) { // This means the child can't expand beyond the value of the maxHeight // attribute. // To adjust the flex line length to the size of maxMainSize, remaining // positive free space needs to be re-distributed to other flex items // (children views). In that case, invoke this method again with the same // startIndex. needsReexpand = true; newHeight = lp.maxHeight; mChildrenFrozen[childIndex] = true; flexLine.mTotalFlexGrow -= lp.flexGrow; } else { accumulatedRoundError += (rawCalculatedHeight - newHeight); if (accumulatedRoundError > 1.0) { newHeight += 1; accumulatedRoundError -= 1.0; } else if (accumulatedRoundError < -1.0) { newHeight -= 1; accumulatedRoundError += 1.0; } } int childWidthMeasureSpec = getChildWidthMeasureSpec(widthMeasureSpec, lp); child.measure(childWidthMeasureSpec, MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY)); largestCrossSize = Math.max(largestCrossSize, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } flexLine.mMainSize += child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; } flexLine.mCrossSize = Math.max(flexLine.mCrossSize, largestCrossSize); childIndex++; } if (needsReexpand && sizeBeforeExpand != flexLine.mMainSize) { // Re-invoke the method with the same startIndex to distribute the positive free space // that wasn't fully distributed (because of maximum length constraint) expandFlexItems(widthMeasureSpec, heightMeasureSpec, flexLine, flexDirection, maxMainSize, paddingAlongMainAxis, startIndex, true); } return childIndex; }
From source file:cn.emagsoftware.ui.BugFixedSlidingPaneLayout.java
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { if (isInEditMode()) { // Don't crash the layout editor. Consume all of the space if specified // or pick a magic number from thin air otherwise. // TODO Better communication with tools of this bogus state. // It will crash on a real device. if (widthMode == MeasureSpec.AT_MOST) { widthMode = MeasureSpec.EXACTLY; } else if (widthMode == MeasureSpec.UNSPECIFIED) { widthMode = MeasureSpec.EXACTLY; widthSize = 300;//from w ww . j a va 2 s . co m } } else { throw new IllegalStateException("Width must have an exact value or MATCH_PARENT"); } } else if (heightMode == MeasureSpec.UNSPECIFIED) { if (isInEditMode()) { // Don't crash the layout editor. Pick a magic number from thin air instead. // TODO Better communication with tools of this bogus state. // It will crash on a real device. if (heightMode == MeasureSpec.UNSPECIFIED) { heightMode = MeasureSpec.AT_MOST; heightSize = 300; } } else { throw new IllegalStateException("Height must not be UNSPECIFIED"); } } int layoutHeight = 0; int maxLayoutHeight = -1; switch (heightMode) { case MeasureSpec.EXACTLY: layoutHeight = maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom(); break; case MeasureSpec.AT_MOST: maxLayoutHeight = heightSize - getPaddingTop() - getPaddingBottom(); break; } float weightSum = 0; boolean canSlide = false; int widthRemaining = widthSize - getPaddingLeft() - getPaddingRight(); final int childCount = getChildCount(); if (childCount > 2) { Log.e(TAG, "onMeasure: More than two child views are not supported."); } // We'll find the current one below. mSlideableView = null; // First pass. Measure based on child LayoutParams width/height. // Weight will incur a second pass. for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (child.getVisibility() == GONE) { lp.dimWhenOffset = false; continue; } if (lp.weight > 0) { weightSum += lp.weight; // If we have no width, weight is the only contributor to the final size. // Measure this view on the weight pass only. if (lp.width == 0) continue; } int childWidthSpec; final int horizontalMargin = lp.leftMargin + lp.rightMargin; if (lp.width == LayoutParams.WRAP_CONTENT) { childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, MeasureSpec.AT_MOST); } else if (lp.width == LayoutParams.FILL_PARENT) { childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - horizontalMargin, MeasureSpec.EXACTLY); } else { childWidthSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); } int childHeightSpec; if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); } else if (lp.height == LayoutParams.FILL_PARENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } child.measure(childWidthSpec, childHeightSpec); final int childWidth = child.getMeasuredWidth(); final int childHeight = child.getMeasuredHeight(); if (heightMode == MeasureSpec.AT_MOST && childHeight > layoutHeight) { layoutHeight = Math.min(childHeight, maxLayoutHeight); } widthRemaining -= childWidth; canSlide |= lp.slideable = widthRemaining < 0; if (lp.slideable) { mSlideableView = child; } } // Resolve weight and make sure non-sliding panels are smaller than the full screen. if (canSlide || weightSum > 0) { final int fixedPanelWidthLimit = widthSize - mOverhangSize; for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (child.getVisibility() == GONE) { continue; } final boolean skippedFirstPass = lp.width == 0 && lp.weight > 0; final int measuredWidth = skippedFirstPass ? 0 : child.getMeasuredWidth(); if (canSlide && child != mSlideableView) { if (lp.width < 0 && (measuredWidth > fixedPanelWidthLimit || lp.weight > 0)) { // Fixed panels in a sliding configuration should // be clamped to the fixed panel limit. final int childHeightSpec; if (skippedFirstPass) { // Do initial height measurement if we skipped measuring this view // the first time around. if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); } else if (lp.height == LayoutParams.FILL_PARENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } } else { childHeightSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY); } final int childWidthSpec = MeasureSpec.makeMeasureSpec(fixedPanelWidthLimit, MeasureSpec.EXACTLY); child.measure(childWidthSpec, childHeightSpec); } } else if (lp.weight > 0) { int childHeightSpec; if (lp.width == 0) { // This was skipped the first time; figure out a real height spec. if (lp.height == LayoutParams.WRAP_CONTENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.AT_MOST); } else if (lp.height == LayoutParams.FILL_PARENT) { childHeightSpec = MeasureSpec.makeMeasureSpec(maxLayoutHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); } } else { childHeightSpec = MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), MeasureSpec.EXACTLY); } if (canSlide) { // Consume available space final int horizontalMargin = lp.leftMargin + lp.rightMargin; final int newWidth = widthSize - horizontalMargin; final int childWidthSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY); if (measuredWidth != newWidth) { child.measure(childWidthSpec, childHeightSpec); } } else { // Distribute the extra width proportionally similar to LinearLayout final int widthToDistribute = Math.max(0, widthRemaining); final int addedWidth = (int) (lp.weight * widthToDistribute / weightSum); final int childWidthSpec = MeasureSpec.makeMeasureSpec(measuredWidth + addedWidth, MeasureSpec.EXACTLY); child.measure(childWidthSpec, childHeightSpec); } } } } setMeasuredDimension(widthSize, layoutHeight); mCanSlide = canSlide; if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && !canSlide) { // Cancel scrolling in progress, it's no longer relevant. mDragHelper.abort(); } }
From source file:com.example.fengxiaozheng.mynews.widget.splash.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.// w w w . j av a 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) { 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 (mSeenPositionMin < 0 || position < mSeenPositionMin) { mSeenPositionMin = position; } if (mSeenPositionMax < 0 || Math.ceil(position + offset) > mSeenPositionMax) { mSeenPositionMax = position + 1; } if (mOnPageChangeListener != null) { mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels); } if (mInternalPageChangeListener != null) { mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels); } if (mPageTransformer != null) { final int scrollX = getScrollX(); 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 float transformPos = (float) (child.getLeft() - scrollX) / getWidth(); mPageTransformer.transformPage(child, transformPos); } } mCalledSuper = true; }
From source file:com.emoiluj.doubleviewpager.HorizontalViewPager.java
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { mInLayout = true;/*from w w w .j a va2s .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; // 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; } 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++; } } } // 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) (width * 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) ((width - paddingLeft - paddingRight) * 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; mFirstLayout = false; }