From 49fafc6c99c3e82ec5f0f329f71f2e54b2cf9efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20Emirhan=20=C5=9Eahin?= Date: Wed, 4 Feb 2026 15:22:55 +0300 Subject: [PATCH] Fix SurfaceView overflow in AspectRatioFrameLayout RESIZE_MODE_ZOOM When using RESIZE_MODE_ZOOM, AspectRatioFrameLayout previously expanded its own measured dimensions beyond parent bounds. This caused SurfaceView's separate hardware surface to extend past the frame, making videos overlap neighboring items in RecyclerView/LazyColumn. This fix keeps the frame at parent-provided size while still measuring children at the zoomed (expanded) size: - Save original dimensions in onMeasure and reset via setMeasuredDimension after children are measured at zoomed size - Override onLayout to center oversized children within the frame for a symmetric center-crop effect - Override dispatchDraw to clip the canvas for canvas-drawn children (TextureView, ImageView), which combined with FrameLayout's default clipChildren=true also enables RenderNode clipping for SurfaceView on API 29+ Other resize modes and wrap_content children (buffering spinner, error text) are unaffected as all new logic is guarded by RESIZE_MODE_ZOOM. Fixes: androidx/media#1107 --- .../media3/ui/AspectRatioFrameLayout.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/libraries/ui/src/main/java/androidx/media3/ui/AspectRatioFrameLayout.java b/libraries/ui/src/main/java/androidx/media3/ui/AspectRatioFrameLayout.java index c4757139ba4..a57c1d5cca7 100644 --- a/libraries/ui/src/main/java/androidx/media3/ui/AspectRatioFrameLayout.java +++ b/libraries/ui/src/main/java/androidx/media3/ui/AspectRatioFrameLayout.java @@ -19,7 +19,9 @@ import android.content.Context; import android.content.res.TypedArray; +import android.graphics.Canvas; import android.util.AttributeSet; +import android.view.View; import android.widget.FrameLayout; import androidx.annotation.IntDef; import androidx.annotation.Nullable; @@ -176,6 +178,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getMeasuredWidth(); int height = getMeasuredHeight(); + int originalWidth = width; + int originalHeight = height; float viewAspectRatio = (float) width / height; float aspectDeformation = videoAspectRatio / viewAspectRatio - 1; if (Math.abs(aspectDeformation) <= MAX_ASPECT_RATIO_DEFORMATION_FRACTION) { @@ -214,6 +218,42 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); + if (resizeMode == RESIZE_MODE_ZOOM) { + // Reset the frame's measured dimensions to the original parent-constrained size so the frame + // itself does not overflow its parent. Children are already measured at the zoomed size. + setMeasuredDimension(originalWidth, originalHeight); + } + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + if (resizeMode == RESIZE_MODE_ZOOM && videoAspectRatio > 0) { + int frameWidth = right - left; + int frameHeight = bottom - top; + for (int i = 0; i < getChildCount(); i++) { + View child = getChildAt(i); + int childWidth = child.getMeasuredWidth(); + int childHeight = child.getMeasuredHeight(); + if (childWidth > frameWidth || childHeight > frameHeight) { + int childLeft = (frameWidth - childWidth) / 2; + int childTop = (frameHeight - childHeight) / 2; + child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); + } + } + } + } + + @Override + protected void dispatchDraw(Canvas canvas) { + if (resizeMode == RESIZE_MODE_ZOOM && videoAspectRatio > 0) { + canvas.save(); + canvas.clipRect(0, 0, getWidth(), getHeight()); + super.dispatchDraw(canvas); + canvas.restore(); + } else { + super.dispatchDraw(canvas); + } } /** Dispatches updates to {@link AspectRatioListener}. */