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

The following examples show how to use android.view.View.MeasureSpec#EXACTLY . 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: RootHostDelegate.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the delegate has defined a size and filled the measureOutput array, returns
 * false if not in which case the hosting view should call super.onMeasure.
 */
public boolean onMeasure(int widthMeasureSpec, int heightMeasureSpec, int[] measureOutput) {
  int width = MeasureSpec.getSize(widthMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
  if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY
      && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
    // If the measurements are exact, postpone LayoutState calculation from measure to layout.
    // This is part of the fix for android's double measure bug. Doing this means that if we get
    // remeasured with different exact measurements, we don't compute two layouts.
    mDoMeasureInLayout = true;
    measureOutput[0] = width;
    measureOutput[1] = height;
    return true;
  }

  if (mRenderState != null) {
    mRenderState.measure(widthMeasureSpec, heightMeasureSpec, measureOutput);
    mDoMeasureInLayout = false;
    return true;
  }

  return false;
}
 
Example 2
Source File: ViewUtil.java    From BalloonPerformer with Apache License 2.0 6 votes vote down vote up
public static int measureSize(int measureSpec, int wantSize) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        // We were told how big to be
        result = specSize;
    } else {
        // wrap_content的时候宽度最小
        result = wantSize;
        if (specMode == MeasureSpec.AT_MOST) {
            // Respect AT_MOST value if that was what is called for by
            // measureSpec
            result = Math.min(result, specSize);
        }
    }
    return result;
}
 
Example 3
Source File: MyScrollView.java    From palmsuda with Apache License 2.0 6 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	final int width = MeasureSpec.getSize(widthMeasureSpec);
	final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	if (widthMode != MeasureSpec.EXACTLY) {
		throw new IllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!");
	}
	final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
	if (heightMode != MeasureSpec.EXACTLY) {
		throw new IllegalStateException("ScrollLayout only can run at EXACTLY mode!");
	}
	final int count = getChildCount();
	for (int i = 0; i < count; i++) {
		getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
	}
	scrollTo(mCurScreen * width, 0);
}
 
Example 4
Source File: PiewController.java    From quickmark with MIT License 5 votes vote down vote up
public static int measureWidth(int pWidthMeasureSpec) {
	int result = 0;
	int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// �õ�ģʽ
	int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// �õ��ߴ�

	switch (widthMode) {
	/**
	 * mode�������������ȡֵ�ֱ�ΪMeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY,
	 * MeasureSpec.AT_MOST?
	 * 
	 * 
	 * MeasureSpec.EXACTLY�Ǿ�ȷ�ߴ磬
	 * �����ǽ��ؼ���layout_width��layout_heightָ��Ϊ������ֵʱ��andorid
	 * :layout_width="50dip"������ΪFILL_PARENT�ǣ����ǿؼ���С�Ѿ�ȷ������������Ǿ�ȷ�ߴ�?
	 * 
	 * 
	 * MeasureSpec.AT_MOST�����ߴ磬
	 * ���ؼ���layout_width��layout_heightָ��ΪWRAP_CONTENT�J
	 * ���ؼ���Сһ����������ӿռ�����ݽ��б仯����ʱ�ؼ��ߴ�ֻҪ���������ؼ�����Ėc?�ߴ缴��
	 * ����ˣ���ʱ��mode��AT_MOST��size�����˸��ؼ���������ߴ�?
	 * 
	 * 
	 * MeasureSpec.UNSPECIFIED��δָ���ߴ磬����������࣬د?���Ǹ��ؼ���AdapterView��
	 * ͨ��measure���������ģʽ?
	 */
	case MeasureSpec.AT_MOST:
	case MeasureSpec.EXACTLY:
		result = widthSize;
		break;
	}
	return result;
}
 
Example 5
Source File: PiewController.java    From quickmark with MIT License 5 votes vote down vote up
public static int measureHeight(int pHeightMeasureSpec) {
	int result = 0;

	int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
	int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);

	switch (heightMode) {
	case MeasureSpec.AT_MOST:
	case MeasureSpec.EXACTLY:
		result = heightSize;
		break;
	}
	return result;
}
 
Example 6
Source File: PieView.java    From quickmark with MIT License 5 votes vote down vote up
private int measureWidth(int pWidthMeasureSpec) {
	int result = 0;
	int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// �õ�ģʽ
	int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// �õ��ߴ�

	switch (widthMode) {
	/**
	 * mode�������������ȡֵ�ֱ�ΪMeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY,
	 * MeasureSpec.AT_MOST?
	 * 
	 * 
	 * MeasureSpec.EXACTLY�Ǿ�ȷ�ߴ磬
	 * �����ǽ��ؼ���layout_width��layout_heightָ��Ϊ������ֵʱ��andorid
	 * :layout_width="50dip"������ΪFILL_PARENT�ǣ����ǿؼ���С�Ѿ�ȷ������������Ǿ�ȷ�ߴ�?
	 * 
	 * 
	 * MeasureSpec.AT_MOST�����ߴ磬
	 * ���ؼ���layout_width��layout_heightָ��ΪWRAP_CONTENT�J
	 * ���ؼ���Сһ����������ӿռ�����ݽ��б仯����ʱ�ؼ��ߴ�ֻҪ���������ؼ�����Ėc?�ߴ缴��
	 * ����ˣ���ʱ��mode��AT_MOST��size�����˸��ؼ���������ߴ�?
	 * 
	 * 
	 * MeasureSpec.UNSPECIFIED��δָ���ߴ磬����������࣬د?���Ǹ��ؼ���AdapterView��
	 * ͨ��measure���������ģʽ?
	 */
	case MeasureSpec.AT_MOST:
	case MeasureSpec.EXACTLY:
		result = widthSize;
		break;
	}
	return result;
}
 
Example 7
Source File: PieView.java    From quickmark with MIT License 5 votes vote down vote up
private int measureHeight(int pHeightMeasureSpec) {
	int result = 0;

	int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
	int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);

	switch (heightMode) {
	case MeasureSpec.AT_MOST:
	case MeasureSpec.EXACTLY:
		result = heightSize;
		break;
	}
	return result;
}
 
Example 8
Source File: BaseResolutionPolicy.java    From tilt-game-android with MIT License 5 votes vote down vote up
protected static void throwOnNotMeasureSpecEXACTLY(final int pWidthMeasureSpec, final int pHeightMeasureSpec) {
	final int specWidthMode = MeasureSpec.getMode(pWidthMeasureSpec);
	final int specHeightMode = MeasureSpec.getMode(pHeightMeasureSpec);

	if (specWidthMode != MeasureSpec.EXACTLY || specHeightMode != MeasureSpec.EXACTLY) {
		throw new IllegalStateException("This IResolutionPolicy requires MeasureSpec.EXACTLY ! That means ");
	}
}
 
Example 9
Source File: BaseResolutionPolicy.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
protected static void throwOnNotMeasureSpecEXACTLY(final int pWidthMeasureSpec, final int pHeightMeasureSpec) {
	final int specWidthMode = MeasureSpec.getMode(pWidthMeasureSpec);
	final int specHeightMode = MeasureSpec.getMode(pHeightMeasureSpec);

	if (specWidthMode != MeasureSpec.EXACTLY || specHeightMode != MeasureSpec.EXACTLY) {
		throw new IllegalStateException("This IResolutionPolicy requires MeasureSpec.EXACTLY ! That means ");
	}
}
 
Example 10
Source File: BaseCollectionViewItem.java    From TiCollectionView with MIT License 5 votes vote down vote up
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	int h = MeasureSpec.getSize(heightMeasureSpec);
	int hMode = MeasureSpec.getMode(heightMeasureSpec);
	if (h < minHeight && hMode == MeasureSpec.EXACTLY) {
		h = minHeight;
	}
	super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, hMode));
}
 
Example 11
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 12
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 13
Source File: CircleMenuLayout.java    From accountBook with Apache License 2.0 4 votes vote down vote up
/**
 * 设置布局的宽高,并策略menu item宽高
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int resWidth = 0;
    int resHeight = 0;

    /**
     * 根据传入的参数,分别获取测量模式和测量值
     */
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);

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

    /**
     * 如果宽或者高的测量模式非精确值
     */
    if (widthMode != MeasureSpec.EXACTLY
            || heightMode != MeasureSpec.EXACTLY)
    {
        // 主要设置为背景图的高度
        resWidth = getSuggestedMinimumWidth();
        // 如果未设置背景图片,则设置为屏幕宽高的默认值
        resWidth = resWidth == 0 ? getDefaultWidth() : resWidth;

        resHeight = getSuggestedMinimumHeight();
        // 如果未设置背景图片,则设置为屏幕宽高的默认值
        resHeight = resHeight == 0 ? getDefaultWidth() : resHeight;
    } else
    {
        // 如果都设置为精确值,则直接取小值;
        resWidth = resHeight = Math.min(width, height);
    }

    setMeasuredDimension(resWidth, resHeight);

    // 获得半径
    mRadius = Math.max(getMeasuredWidth(), getMeasuredHeight());

    // menu item数量
    final int count = getChildCount();
    // menu item尺寸
    int childSize = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION);
    // menu item测量模式
    int childMode = MeasureSpec.EXACTLY;

    // 迭代测量
    for (int i = 0; i < count; i++)
    {
        final View child = getChildAt(i);

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

        // 计算menu item的尺寸;以及和设置好的模式,去对item进行测量
        int makeMeasureSpec = -1;

        if (child.getId() == R.id.id_circle_menu_item_center)
        {
            makeMeasureSpec = MeasureSpec.makeMeasureSpec(
                    (int) (mRadius * RADIO_DEFAULT_CENTERITEM_DIMENSION),
                    childMode);
        } else
        {
            makeMeasureSpec = MeasureSpec.makeMeasureSpec(childSize,
                    childMode);
        }
        child.measure(makeMeasureSpec, makeMeasureSpec);
    }

    mPadding = RADIO_PADDING_LAYOUT * mRadius;

}
 
Example 14
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 15
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 16
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);
    }