/** * Measure according to an aspect ratio an width and height constraints. This version * of measureWithAspectRatio will respect the intrinsic size of the component being measured. * * @param widthSpec A SizeSpec for the width * @param heightSpec A SizeSpec for the height * @param intrinsicWidth A pixel value for the intrinsic width of the measured component * @param intrinsicHeight A pixel value for the intrinsic height of the measured component * @param aspectRatio The aspect ration size against * @param outputSize The output size of this measurement */ public static void measureWithAspectRatio( int widthSpec, int heightSpec, int intrinsicWidth, int intrinsicHeight, float aspectRatio, Size outputSize) { if (SizeSpec.getMode(widthSpec) == AT_MOST && SizeSpec.getSize(widthSpec) > intrinsicWidth) { widthSpec = SizeSpec.makeSizeSpec(intrinsicWidth, AT_MOST); } if (SizeSpec.getMode(heightSpec) == AT_MOST && SizeSpec.getSize(heightSpec) > intrinsicHeight) { heightSpec = SizeSpec.makeSizeSpec(intrinsicHeight, AT_MOST); } measureWithAspectRatio(widthSpec, heightSpec, aspectRatio, outputSize); }
@Override protected void onMeasure( ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) { int width = SizeSpec.getSize(widthSpec); int height = SizeSpec.getSize(heightSpec); onMeasureCalled(); size.width = (measuredWidth != -1) ? SizeSpec.resolveSize(widthSpec, measuredWidth) : width; size.height = (measuredHeight != -1) ? SizeSpec.resolveSize(heightSpec, measuredHeight) : height; }
Size outputSize) { final int widthMode = SizeSpec.getMode(widthSpec); final int widthSize = SizeSpec.getSize(widthSpec); final int heightMode = SizeSpec.getMode(heightSpec); final int heightSize = SizeSpec.getSize(heightSpec); final int widthBasedHeight = (int) Math.ceil(widthSize / aspectRatio); final int heightBasedWidth = (int) Math.ceil(heightSize * aspectRatio); String.format( "Ratio makes height larger than allowed. w:%s h:%s aspectRatio:%f", SizeSpec.toString(widthSpec), SizeSpec.toString(heightSpec), aspectRatio)); String.format( "Ratio makes width larger than allowed. w:%s h:%s aspectRatio:%f", SizeSpec.toString(widthSpec), SizeSpec.toString(heightSpec), aspectRatio));
private static int getResultSizePxWithSpecAndDesiredPx(int spec, int desiredSize) { final int mode = SizeSpec.getMode(spec); switch (mode) { case SizeSpec.UNSPECIFIED: return desiredSize; case SizeSpec.AT_MOST: return Math.min(SizeSpec.getSize(spec), desiredSize); case SizeSpec.EXACTLY: return SizeSpec.getSize(spec); default: throw new IllegalStateException("Unexpected size spec mode"); } }
public static int makeSizeSpecFromCssSpec(float cssSize, YogaMeasureMode cssMode) { switch (cssMode) { case EXACTLY: return makeSizeSpec(FastMath.round(cssSize), SizeSpec.EXACTLY); case UNDEFINED: return makeSizeSpec(0, SizeSpec.UNSPECIFIED); case AT_MOST: return makeSizeSpec(FastMath.round(cssSize), SizeSpec.AT_MOST); default: throw new IllegalArgumentException("Unexpected YogaMeasureMode: " + cssMode); } } }
@Override public int getChildWidthSpec(int widthSpec, RenderInfo renderInfo) { return SizeSpec.makeSizeSpec(SizeSpec.getSize(widthSpec), AT_MOST); } })
@VisibleForTesting public static int resolveWidth( int widthSpec, Layout layout, boolean minimallyWide, int minimallyWideThreshold) { final int fullWidth = SizeSpec.resolveSize(widthSpec, layout.getWidth()); if (minimallyWide && layout.getLineCount() > 1) { final int minimalWidth = SizeSpec.resolveSize(widthSpec, LayoutMeasureUtil.getWidth(layout)); if (fullWidth - minimalWidth > minimallyWideThreshold) { return minimalWidth; } } return fullWidth; }
final boolean isTracing = ComponentsSystrace.isTracing(); widthSpec = SizeSpec.makeSizeSpecFromCssSpec(width, widthMode); heightSpec = SizeSpec.makeSizeSpecFromCssSpec(height, heightMode); .arg("widthSpec", SizeSpec.toString(widthSpec)) .arg("heightSpec", SizeSpec.toString(heightSpec)) .arg("componentId", component.getId()) .flush();
@Override public int getChildWidthSpec(int widthSpec, RenderInfo renderInfo) { switch (mLinearLayoutManager.getOrientation()) { case LinearLayoutManager.HORIZONTAL: return SizeSpec.makeSizeSpec(0, SizeSpec.UNSPECIFIED); default: return widthSpec; } }
/** * @param widthSpec the widthSpec used to measure the parent {@link RecyclerSpec}. * @return widthSpec of a child that is of span size 1 */ @Override public int getChildWidthSpec(int widthSpec, RenderInfo renderInfo) { switch (mGridLayoutManager.getOrientation()) { case GridLayoutManager.HORIZONTAL: return SizeSpec.makeSizeSpec(0, UNSPECIFIED); default: Integer overrideWidth = (Integer) renderInfo.getCustomAttribute(GridLayoutInfo.OVERRIDE_SIZE); if (overrideWidth != null) { return SizeSpec.makeSizeSpec(overrideWidth, EXACTLY); } if (renderInfo.isFullSpan()) { return SizeSpec.makeSizeSpec(SizeSpec.getSize(widthSpec), EXACTLY); } final int spanCount = mGridLayoutManager.getSpanCount(); final int spanSize = renderInfo.getSpanSize(); return SizeSpec.makeSizeSpec( spanSize * ((SizeSpec.getSize(widthSpec)) / spanCount), EXACTLY); } }
public static int getViewMeasureSpec(int sizeSpec) { switch (getMode(sizeSpec)) { case SizeSpec.EXACTLY: return MeasureSpec.makeMeasureSpec(getSize(sizeSpec), MeasureSpec.EXACTLY); case SizeSpec.AT_MOST: return MeasureSpec.makeMeasureSpec(getSize(sizeSpec), MeasureSpec.AT_MOST); case SizeSpec.UNSPECIFIED: return MeasureSpec.makeMeasureSpec(getSize(sizeSpec), MeasureSpec.UNSPECIFIED); default: throw new IllegalStateException("Unexpected size spec mode"); } }
@Override public synchronized void computeLayoutAsync( ComponentContext context, int widthSpec, int heightSpec) { mComponentTree = mock(ComponentTree.class); mTreeValid = true; mLastRequestedWidthSpec = widthSpec; mLastRequestedHeightSpec = heightSpec; mLayoutAsyncCalled = true; mChildWidth = SizeSpec.getSize(widthSpec); mChildHeight = SizeSpec.getSize(heightSpec); }
@OnMeasure static void onMeasure( ComponentContext c, ComponentLayout layout, int widthSpec, int heightSpec, Size size) { if (SizeSpec.getMode(widthSpec) == SizeSpec.UNSPECIFIED && SizeSpec.getMode(heightSpec) == SizeSpec.UNSPECIFIED) { size.width = DEFAULT_SIZE; size.height = DEFAULT_SIZE; } else { MeasureUtils.measureWithEqualDimens(widthSpec, heightSpec, size); } }
size.height = SizeSpec.resolveSize(heightSpec, preferredHeight);
final @SizeSpec.MeasureSpecMode int mode = SizeSpec.getMode(widthSpec); if (mode == SizeSpec.UNSPECIFIED) { return widthSpec; && calculatedScreenWidthPx == SizeSpec.getSize(widthSpec)) { return SizeSpec.makeSizeSpec(screenWidthPx, mode);
@Override public int getChildHeightSpec(int heightSpec, RenderInfo renderInfo) { switch (mLinearLayoutManager.getOrientation()) { case LinearLayoutManager.HORIZONTAL: return heightSpec; default: return SizeSpec.makeSizeSpec(0, SizeSpec.UNSPECIFIED); } }
/** * @param heightSpec the heightSpec used to measure the parent {@link RecyclerSpec}. * @return heightSpec of a child that is of span size 1 */ @Override public int getChildHeightSpec(int heightSpec, RenderInfo renderInfo) { switch (mGridLayoutManager.getOrientation()) { case GridLayoutManager.HORIZONTAL: Integer overrideHeight = (Integer) renderInfo.getCustomAttribute(GridLayoutInfo.OVERRIDE_SIZE); if (overrideHeight != null) { return SizeSpec.makeSizeSpec(overrideHeight, EXACTLY); } if (renderInfo.isFullSpan()) { return SizeSpec.makeSizeSpec(SizeSpec.getSize(heightSpec), EXACTLY); } final int spanCount = mGridLayoutManager.getSpanCount(); final int spanSize = renderInfo.getSpanSize(); return SizeSpec.makeSizeSpec( spanSize * (SizeSpec.getSize(heightSpec) / spanCount), EXACTLY); default: return SizeSpec.makeSizeSpec(0, UNSPECIFIED); } }
/** * Resolve a size spec given a preferred size. * * @param sizeSpec The spec to resolve. * @param preferredSize The preferred size. * @return The resolved size. */ public static int resolveSize(int sizeSpec, int preferredSize) { switch (SizeSpec.getMode(sizeSpec)) { case EXACTLY: return SizeSpec.getSize(sizeSpec); case AT_MOST: return Math.min(SizeSpec.getSize(sizeSpec), preferredSize); case UNSPECIFIED: return preferredSize; default: throw new IllegalStateException("Unexpected size mode: " + SizeSpec.getMode(sizeSpec)); } }