From d24bc7c69008f75d8ba55263ad01b088c9b78b69 Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Thu, 10 Jul 2025 11:01:36 -0700 Subject: [PATCH] Centering preview view using gravity Center We didn't bother to center the view because it just one view that matches the parent view, the problem is that the peview view is bigger than it's parent, this is by design so that the preview matches the full screen and looks the same as the real Launcher. We then scale the preview view to match the visualization space. The problem is that the logic that position a view in FrameLayot doesn't account for children views bigger than itself so in RTL the view is not "centered" or it doesn't start where we expect it to start. By using gravity = CENTER it does account correclty for bigger views. Fix: 429081966 Test: GridPreviewTest Flag: EXEMPT bug fix (cherry picked from commit 6624691c50b29dabb706e52d5bd09d88b257015b) (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:e5e32f0ef771417b9bf29a71d6d7fd7b47067dd1) Merged-In: I1e75087ca68dd3180b0a9f11d9403eafb892391a Change-Id: I1e75087ca68dd3180b0a9f11d9403eafb892391a --- .../android/launcher3/preview/PreviewSurfaceRenderer.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/android/launcher3/preview/PreviewSurfaceRenderer.java b/src/com/android/launcher3/preview/PreviewSurfaceRenderer.java index f0427eeab9..9acac237a4 100644 --- a/src/com/android/launcher3/preview/PreviewSurfaceRenderer.java +++ b/src/com/android/launcher3/preview/PreviewSurfaceRenderer.java @@ -40,6 +40,7 @@ import android.text.TextUtils; import android.util.SparseIntArray; import android.view.ContextThemeWrapper; import android.view.Display; +import android.view.Gravity; import android.view.SurfaceControlViewHost; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; @@ -344,15 +345,14 @@ public class PreviewSurfaceRenderer { } private void setContentRoot(View view) { - view.setPivotX(0); - view.setPivotY(0); // This aspect scales the view to fit in the surface and centers it final float scale = Math.min(mWidth / (float) view.getMeasuredWidth(), mHeight / (float) view.getMeasuredHeight()); view.setScaleX(scale); view.setScaleY(scale); - view.setTranslationX((mWidth - scale * view.getWidth()) / 2); - view.setTranslationY((mHeight - scale * view.getHeight()) / 2); + LayoutParams lp = new LayoutParams(view.getMeasuredWidth(), view.getMeasuredHeight()); + lp.gravity = Gravity.CENTER; + view.setLayoutParams(lp); if (!Flags.newCustomizationPickerUi()) { view.setAlpha(mSkipAnimations ? 1 : 0);