Java Code Examples for android.view.View.MeasureSpec#UNSPECIFIED

The following examples show how to use android.view.View.MeasureSpec#UNSPECIFIED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SquareFrameLayout.java    From CameraV with GNU General Public License v3.0 6 votes vote down vote up
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
 
	if (MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED)
	{
		int inhibitedHeightSpec = MeasureSpec.makeMeasureSpec((int) (MeasureSpec.getSize(widthMeasureSpec)),
				MeasureSpec.getMode(widthMeasureSpec));
		super.onMeasure(widthMeasureSpec, inhibitedHeightSpec);
	}
	else
	{
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = this.getMeasuredWidth();
			int height = (int) (width);
			super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
	}
}
 
Example 2
Source File: DragSortItemView.java    From DongWeather with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 3
Source File: DragSortItemView.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 4
Source File: DragSortItemView.java    From chromadoze with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 5
Source File: DragSortItemView.java    From google-authenticator-android with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    final View child = getChildAt(0);
    if (child == null) {
        setMeasuredDimension(0, width);
        return;
    }

    if (child.isLayoutRequested()) {
        // Always let child be as tall as it wants.
        measureChild(child, widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
        ViewGroup.LayoutParams lp = getLayoutParams();

        if (lp.height > 0) {
            height = lp.height;
        } else {
            height = child.getMeasuredHeight();
        }
    }

    setMeasuredDimension(width, height);
}
 
Example 6
Source File: AwLayoutSizer.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Calculate the size of the view.
 * This is designed to be used to implement the android.view.View#onMeasure() method.
 */
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    int contentHeightPix = (int) (mContentHeightCss * mPageScaleFactor * mDIPScale);
    int contentWidthPix = (int) (mContentWidthCss * mPageScaleFactor * mDIPScale);

    int measuredHeight = contentHeightPix;
    int measuredWidth = contentWidthPix;

    mLastMeasuredPageScaleFactor = mPageScaleFactor;

    // Always use the given size unless unspecified. This matches WebViewClassic behavior.
    mWidthMeasurementIsFixed = (widthMode != MeasureSpec.UNSPECIFIED);
    mHeightMeasurementIsFixed = (heightMode == MeasureSpec.EXACTLY);
    mHeightMeasurementLimited =
        (heightMode == MeasureSpec.AT_MOST) && (contentHeightPix > heightSize);
    mHeightMeasurementLimit = heightSize;

    if (mHeightMeasurementIsFixed || mHeightMeasurementLimited) {
        measuredHeight = heightSize;
    }

    if (mWidthMeasurementIsFixed) {
        measuredWidth = widthSize;
    }

    if (measuredHeight < contentHeightPix) {
        measuredHeight |= View.MEASURED_STATE_TOO_SMALL;
    }

    if (measuredWidth < contentWidthPix) {
        measuredWidth |= View.MEASURED_STATE_TOO_SMALL;
    }

    mDelegate.setMeasuredDimension(measuredWidth, measuredHeight);
}
 
Example 7
Source File: AwLayoutSizer.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Calculate the size of the view.
 * This is designed to be used to implement the android.view.View#onMeasure() method.
 */
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    int contentHeightPix = (int) (mContentHeightCss * mPageScaleFactor * mDIPScale);
    int contentWidthPix = (int) (mContentWidthCss * mPageScaleFactor * mDIPScale);

    int measuredHeight = contentHeightPix;
    int measuredWidth = contentWidthPix;

    mLastMeasuredPageScaleFactor = mPageScaleFactor;

    // Always use the given size unless unspecified. This matches WebViewClassic behavior.
    mWidthMeasurementIsFixed = (widthMode != MeasureSpec.UNSPECIFIED);
    mHeightMeasurementIsFixed = (heightMode == MeasureSpec.EXACTLY);
    mHeightMeasurementLimited =
        (heightMode == MeasureSpec.AT_MOST) && (contentHeightPix > heightSize);
    mHeightMeasurementLimit = heightSize;

    if (mHeightMeasurementIsFixed || mHeightMeasurementLimited) {
        measuredHeight = heightSize;
    }

    if (mWidthMeasurementIsFixed) {
        measuredWidth = widthSize;
    }

    if (measuredHeight < contentHeightPix) {
        measuredHeight |= View.MEASURED_STATE_TOO_SMALL;
    }

    if (measuredWidth < contentWidthPix) {
        measuredWidth |= View.MEASURED_STATE_TOO_SMALL;
    }

    mDelegate.setMeasuredDimension(measuredWidth, measuredHeight);
}
 
Example 8
Source File: ViewFlow.java    From NewXmPluginSDK with Apache License 2.0 4 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	int widthSize = MeasureSpec.getSize(widthMeasureSpec);
	int heightSize = MeasureSpec.getSize(heightMeasureSpec);
	final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

	int childWidth = 0;
	int childHeight = 0;
	int childState = 0;

	final int widthPadding = getWidthPadding();
	final int heightPadding = getHeightPadding();

	int count = mAdapter == null ? 0 : mAdapter.getCount();
	if (count > 0) {
		final View child = obtainView(0);
		measureChild(child, widthMeasureSpec, heightMeasureSpec);
		childWidth = child.getMeasuredWidth();
		childHeight = child.getMeasuredHeight();
		if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH)
		childState = child.getMeasuredState();
		mRecycledViews.add(child);
	}

	switch (widthMode) {
		case MeasureSpec.UNSPECIFIED:
			widthSize = childWidth + widthPadding;
			break;
		case MeasureSpec.AT_MOST:
			widthSize = (childWidth + widthPadding) | childState;
			break;
		case MeasureSpec.EXACTLY:
			if (widthSize < childWidth + widthPadding)
				widthSize |= MEASURED_STATE_TOO_SMALL;
			break;
	}
	switch (heightMode) {
		case MeasureSpec.UNSPECIFIED:
			heightSize = childHeight + heightPadding;
			break;
		case MeasureSpec.AT_MOST:
			heightSize = (childHeight + heightPadding) | (childState >> MEASURED_HEIGHT_STATE_SHIFT);
			break;
		case MeasureSpec.EXACTLY:
			if (heightSize < childHeight + heightPadding)
				heightSize |= MEASURED_STATE_TOO_SMALL;
			break;
	}

	if (heightMode == MeasureSpec.UNSPECIFIED) {
		heightSize = heightPadding + childHeight;
	} else {
		heightSize |= (childState&MEASURED_STATE_MASK);
	}

	setMeasuredDimension(widthSize, heightSize);
}
 
Example 9
Source File: GridLayout.java    From tns-core-modules-widgets with Apache License 2.0 4 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    CommonLayoutParams.adjustChildrenLayoutParams(this, widthMeasureSpec, heightMeasureSpec);

    int measureWidth = 0;
    int measureHeight = 0;

    int width = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

    int height = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int verticalPadding = this.getPaddingTop() + this.getPaddingBottom();
    int horizontalPadding = this.getPaddingLeft() + this.getPaddingRight();

    boolean infinityWidth = widthMode == MeasureSpec.UNSPECIFIED;
    boolean infinityHeight = heightMode == MeasureSpec.UNSPECIFIED;

    this.helper.width = Math.max(0, width - horizontalPadding);
    this.helper.height = Math.max(0, height - verticalPadding);

    int gravity = LayoutBase.getGravity(this);
    int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int layoutDirection = this.getLayoutDirection();
    final int horizontalGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection) & Gravity.HORIZONTAL_GRAVITY_MASK;

    this.helper.stretchedHorizontally = widthMode == MeasureSpec.EXACTLY || (horizontalGravity == Gravity.FILL_HORIZONTAL && !infinityWidth);
    this.helper.stretchedVertically = heightMode == MeasureSpec.EXACTLY || (verticalGravity == Gravity.FILL_VERTICAL && !infinityHeight);

    this.helper.setInfinityWidth(infinityWidth);
    this.helper.setInfinityHeight(infinityHeight);

    this.helper.clearMeasureSpecs();
    this.helper.init();

    for (int i = 0, count = this.getChildCount(); i < count; i++) {
        View child = this.getChildAt(i);
        if (child.getVisibility() == View.GONE) {
            continue;
        }

        MeasureSpecs measureSpecs = this.map.get(child);
        this.updateMeasureSpecs(child, measureSpecs);
        this.helper.addMeasureSpec(measureSpecs);
    }

    this.helper.measure();

    // Add in our padding
    measureWidth = this.helper.measuredWidth + horizontalPadding;
    measureHeight = this.helper.measuredHeight + verticalPadding;

    // Check against our minimum sizes
    measureWidth = Math.max(measureWidth, this.getSuggestedMinimumWidth());
    measureHeight = Math.max(measureHeight, this.getSuggestedMinimumHeight());

    int widthSizeAndState = resolveSizeAndState(measureWidth, widthMeasureSpec, 0);
    int heightSizeAndState = resolveSizeAndState(measureHeight, heightMeasureSpec, 0);

    this.setMeasuredDimension(widthSizeAndState, heightSizeAndState);
}
 
Example 10
Source File: CommonLayoutParams.java    From tns-core-modules-widgets with Apache License 2.0 4 votes vote down vote up
/**
 * Iterates over children and changes their width and height to one calculated from percentage
 * values.
 *
 * @param viewGroup         The parent ViewGroup.
 * @param widthMeasureSpec  Width MeasureSpec of the parent ViewGroup.
 * @param heightMeasureSpec Height MeasureSpec of the parent ViewGroup.
 */
protected static void adjustChildrenLayoutParams(ViewGroup viewGroup, int widthMeasureSpec, int heightMeasureSpec) {

    int availableWidth = MeasureSpec.getSize(widthMeasureSpec);
    int widthSpec = MeasureSpec.getMode(widthMeasureSpec);

    int availableHeight = MeasureSpec.getSize(heightMeasureSpec);
    int heightSpec = MeasureSpec.getMode(heightMeasureSpec);

    for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
        View child = viewGroup.getChildAt(i);
        LayoutParams params = child.getLayoutParams();

        if (params instanceof CommonLayoutParams) {
            CommonLayoutParams lp = (CommonLayoutParams) child.getLayoutParams();
            if (widthSpec != MeasureSpec.UNSPECIFIED) {
                if (lp.widthPercent > 0) {
                    // If we get measured twice we will override the original value with the one calculated from percentValue from the first measure.
                    // So we set originalValue only the first time.
                    if (lp.widthOriginal == NOT_SET) {
                        lp.widthOriginal = lp.width;
                    }
                    lp.width = (int) (availableWidth * lp.widthPercent);
                }
                else {
                    lp.widthOriginal = NOT_SET;
                }

                if (lp.leftMarginPercent > 0) {
                    if (lp.leftMarginOriginal == NOT_SET) {
                        lp.leftMarginOriginal = lp.leftMargin;
                    }
                    lp.leftMargin = (int) (availableWidth * lp.leftMarginPercent);
                }
                else {
                    lp.leftMarginOriginal = NOT_SET;
                }

                if (lp.rightMarginPercent > 0) {
                    if (lp.rightMarginOriginal == NOT_SET) {
                        lp.rightMarginOriginal = lp.rightMargin;
                    }
                    lp.rightMargin = (int) (availableWidth * lp.rightMarginPercent);
                }
                else {
                    lp.rightMarginOriginal = NOT_SET;
                }
            }

            if (heightSpec != MeasureSpec.UNSPECIFIED) {
                if (lp.heightPercent > 0) {
                    if (lp.heightOriginal == NOT_SET) {
                        lp.heightOriginal = lp.height;
                    }
                    lp.height = (int) (availableHeight * lp.heightPercent);
                }
                else {
                    lp.heightOriginal = NOT_SET;
                }

                if (lp.topMarginPercent > 0) {
                    if (lp.topMarginOriginal == NOT_SET) {
                        lp.topMarginOriginal = lp.topMargin;
                    }
                    lp.topMargin = (int) (availableHeight * lp.topMarginPercent);
                }
                else {
                    lp.topMarginOriginal = NOT_SET;
                }

                if (lp.bottomMarginPercent > 0) {
                    if (lp.bottomMarginOriginal == NOT_SET) {
                        lp.bottomMarginOriginal = lp.bottomMargin;
                    }
                    lp.bottomMargin = (int) (availableHeight * lp.bottomMarginPercent);
                }
                else {
                    lp.bottomMarginOriginal = NOT_SET;
                }
            }
        }
    }
}
 
Example 11
Source File: CommonLayoutParams.java    From tns-core-modules-widgets with Apache License 2.0 4 votes vote down vote up
private static int getMeasureSpec(View view, int parentMeasureSpec, boolean horizontal) {

        int parentLength = MeasureSpec.getSize(parentMeasureSpec);
        int parentSpecMode = MeasureSpec.getMode(parentMeasureSpec);

        CommonLayoutParams lp = (CommonLayoutParams) view.getLayoutParams();
        final int margins = horizontal ? lp.leftMargin + lp.rightMargin : lp.topMargin + lp.bottomMargin;

        int resultSize = 0;
        int resultMode = MeasureSpec.UNSPECIFIED;

        int measureLength = Math.max(0, parentLength - margins);
        int childLength = horizontal ? lp.width : lp.height;

        // We want a specific size... let be it.
        if (childLength >= 0) {
            if (parentSpecMode != MeasureSpec.UNSPECIFIED) {
                resultSize = Math.min(parentLength, childLength);
            } else {
                resultSize = childLength;
            }

            resultMode = MeasureSpec.EXACTLY;
        } else {
            switch (parentSpecMode) {
                // Parent has imposed an exact size on us
                case MeasureSpec.EXACTLY:
                    resultSize = measureLength;
                    int gravity = LayoutBase.getGravity(view);
                    boolean stretched;
                    if (horizontal) {
                        final int horizontalGravity = Gravity.getAbsoluteGravity(gravity, view.getLayoutDirection()) & Gravity.HORIZONTAL_GRAVITY_MASK;
                        stretched = horizontalGravity == Gravity.FILL_HORIZONTAL;
                    } else {
                        final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
                        stretched = verticalGravity == Gravity.FILL_VERTICAL;
                    }

                    // if stretched - view wants to be our size. So be it.
                    // else - view wants to determine its own size. It can't be bigger than us.
                    resultMode = stretched ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
                    break;

                // Parent has imposed a maximum size on us
                case MeasureSpec.AT_MOST:
                    resultSize = measureLength;
                    resultMode = MeasureSpec.AT_MOST;
                    break;

                case MeasureSpec.UNSPECIFIED:
                    resultSize = 0;
                    resultMode = MeasureSpec.UNSPECIFIED;
                    break;
            }
        }

        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }