diff --git a/src/com/android/launcher3/folder/Folder.java b/src/com/android/launcher3/folder/Folder.java index edec8dc4e2..59052f8135 100644 --- a/src/com/android/launcher3/folder/Folder.java +++ b/src/com/android/launcher3/folder/Folder.java @@ -102,6 +102,7 @@ import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.dagger.LauncherComponentProvider; import com.android.launcher3.dragndrop.DragController.DragListener; import com.android.launcher3.dragndrop.DragOptions; +import com.android.launcher3.graphics.PathWrapper; import com.android.launcher3.graphics.ShapeDelegate; import com.android.launcher3.graphics.ThemeManager; import com.android.launcher3.logger.LauncherAtom.FromState; @@ -243,7 +244,7 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank; - private Path mClipPath; + private PathWrapper mClipPath; @ViewDebug.ExportedProperty(category = "launcher", mapping = { @@ -2015,7 +2016,7 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo * rounded rect. */ @Override - public void setClipPath(Path clipPath) { + public void setClipPath(PathWrapper clipPath) { mClipPath = clipPath; invalidate(); } @@ -2025,7 +2026,7 @@ public class Folder extends AbstractFloatingView implements ClipPathView, DragSo if (mClipPath != null) { // clipPath is non-null while clip reveal animation is playing int count = canvas.save(); - canvas.clipPath(mClipPath); + canvas.clipPath(mClipPath.getPath()); mBackground.draw(canvas); if (!mIsAnimatingClosed) { // If animating open, clip the content to the clip path to keep in bounds. diff --git a/src/com/android/launcher3/folder/FolderPagedView.java b/src/com/android/launcher3/folder/FolderPagedView.java index 8dcf1c0da4..c97b2d6ffc 100644 --- a/src/com/android/launcher3/folder/FolderPagedView.java +++ b/src/com/android/launcher3/folder/FolderPagedView.java @@ -24,7 +24,6 @@ import static com.android.launcher3.folder.FolderGridOrganizer.createFolderGridO import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; -import android.graphics.Path; import android.graphics.Point; import android.util.ArrayMap; import android.util.AttributeSet; @@ -46,6 +45,7 @@ import com.android.launcher3.ShortcutAndWidgetContainer; import com.android.launcher3.Utilities; import com.android.launcher3.apppairs.AppPairIcon; import com.android.launcher3.celllayout.CellLayoutLayoutParams; +import com.android.launcher3.graphics.PathWrapper; import com.android.launcher3.keyboard.ViewGroupFocusHelper; import com.android.launcher3.model.data.AppInfo; import com.android.launcher3.model.data.AppPairInfo; @@ -99,7 +99,7 @@ public class FolderPagedView extends PagedView implements Cli private Folder mFolder; - private Path mClipPath; + private PathWrapper mClipPath; // If the views are attached to the folder or not. A folder should be bound when its // animating or is open. @@ -157,7 +157,7 @@ public class FolderPagedView extends PagedView implements Cli protected void dispatchDraw(Canvas canvas) { if (mClipPath != null) { int count = canvas.save(); - canvas.clipPath(mClipPath); + canvas.clipPath(mClipPath.getPath()); mFocusIndicatorHelper.draw(canvas); super.dispatchDraw(canvas); canvas.restoreToCount(count); @@ -818,7 +818,7 @@ public class FolderPagedView extends PagedView implements Cli } @Override - public void setClipPath(Path clipPath) { + public void setClipPath(PathWrapper clipPath) { mClipPath = clipPath; invalidate(); } diff --git a/src/com/android/launcher3/folder/PreviewBackground.java b/src/com/android/launcher3/folder/PreviewBackground.java index 89ce509fa8..41dac0e167 100644 --- a/src/com/android/launcher3/folder/PreviewBackground.java +++ b/src/com/android/launcher3/folder/PreviewBackground.java @@ -32,7 +32,6 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; -import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RadialGradient; @@ -50,6 +49,7 @@ import com.android.launcher3.DeviceProfile; import com.android.launcher3.Flags; import com.android.launcher3.R; import com.android.launcher3.celllayout.DelegatedCellDrawing; +import com.android.launcher3.graphics.PathWrapper; import com.android.launcher3.graphics.ShapeDelegate; import com.android.launcher3.graphics.ThemeManager; import com.android.launcher3.util.Themes; @@ -85,7 +85,7 @@ public class PreviewBackground extends DelegatedCellDrawing { private RadialGradient mShadowShader = null; private final Matrix mShaderMatrix = new Matrix(); - private final Path mPath = new Path(); + private final PathWrapper mPath = new PathWrapper(); private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); @@ -316,7 +316,7 @@ public class PreviewBackground extends DelegatedCellDrawing { } else { saveCount = canvas.save(); - canvas.clipPath(getClipPath(), Region.Op.DIFFERENCE); + canvas.clipPath(getClipPath().getPath(), Region.Op.DIFFERENCE); } mShaderMatrix.setScale(shadowRadius, shadowRadius); @@ -402,7 +402,7 @@ public class PreviewBackground extends DelegatedCellDrawing { mScale = originalScale; } - public Path getClipPath() { + public PathWrapper getClipPath() { mPath.reset(); float radius = getScaledRadius(); if (!Flags.enableLauncherIconShapes()) { diff --git a/src/com/android/launcher3/folder/PreviewItemManager.java b/src/com/android/launcher3/folder/PreviewItemManager.java index f3b0c1fa8c..ba5cd09225 100644 --- a/src/com/android/launcher3/folder/PreviewItemManager.java +++ b/src/com/android/launcher3/folder/PreviewItemManager.java @@ -182,7 +182,7 @@ public class PreviewItemManager { int saveCount = canvas.getSaveCount(); // The items are drawn in coordinates relative to the preview offset PreviewBackground bg = mIcon.getFolderBackground(); - Path clipPath = bg.getClipPath(); + Path clipPath = bg.getClipPath().getPath(); PointF firstPageOffset = new PointF(bg.basePreviewOffsetX, bg.basePreviewOffsetY); drawParams(canvas, mFirstPageParams, firstPageOffset, /* shouldClipPath */ false, clipPath); canvas.restoreToCount(saveCount); diff --git a/src/com/android/launcher3/graphics/PathWrapper.kt b/src/com/android/launcher3/graphics/PathWrapper.kt new file mode 100644 index 0000000000..5b9f7eda7c --- /dev/null +++ b/src/com/android/launcher3/graphics/PathWrapper.kt @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher3.graphics + +import android.graphics.Path +import android.graphics.RectF + +/** A wrapper class of [Path] that also tracks the [bounds] and corner radii. */ +class PathWrapper { + + val path: Path = Path() + + private val bounds: RectF = RectF() + private var cornerRadius = 0f + + fun setBounds(left: Float, top: Float, right: Float, bottom: Float) { + bounds.set(left, top, right, bottom) + } + + fun setCornerRadius(radius: Float) { + cornerRadius = radius + } + + fun estimateBoundsFromPath() { + path.computeBounds(bounds, true /* exact */) + } + + fun reset() { + path.reset() + } +} diff --git a/src/com/android/launcher3/graphics/ShapeDelegate.kt b/src/com/android/launcher3/graphics/ShapeDelegate.kt index 8e72d8f53b..eac2a4184d 100644 --- a/src/com/android/launcher3/graphics/ShapeDelegate.kt +++ b/src/com/android/launcher3/graphics/ShapeDelegate.kt @@ -70,6 +70,8 @@ interface ShapeDelegate { fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) + fun addToPath(path: PathWrapper, offsetX: Float, offsetY: Float, radius: Float) + fun createRevealAnimator( target: T, startRect: Rect, @@ -90,6 +92,12 @@ interface ShapeDelegate { override fun addToPath(path: Path, offsetX: Float, offsetY: Float, radius: Float) = path.addCircle(radius + offsetX, radius + offsetY, radius, Path.Direction.CW) + + override fun addToPath(path: PathWrapper, offsetX: Float, offsetY: Float, radius: Float) { + addToPath(path.path, offsetX, offsetY, radius) + path.setBounds(offsetX, offsetY, offsetX + radius * 2, offsetY + radius * 2) + path.setCornerRadius(radius) + } } /** Rounded square with [radiusRatio] as a ratio of its half edge size */ @@ -123,6 +131,15 @@ interface ShapeDelegate { ) } + override fun addToPath(path: PathWrapper, offsetX: Float, offsetY: Float, radius: Float) { + addToPath(path.path, offsetX, offsetY, radius) + val cx = radius + offsetX + val cy = radius + offsetY + val cr = radius * radiusRatio + path.setBounds(cx - radius, cy - radius, cx + radius, cy + radius) + path.setCornerRadius(cr) + } + override fun createRevealAnimator( target: T, startRect: Rect, @@ -131,17 +148,15 @@ interface ShapeDelegate { isReversed: Boolean, ): ValueAnimator where T : View, T : ClipPathView { val startRadius = (startRect.width() / 2f) * radiusRatio - val pathProvider = { progress: Float, path: Path -> + val pathProvider = { progress: Float, path: PathWrapper -> val radius = (1 - progress) * startRadius + progress * endRadius - path.addRoundRect( - (1 - progress) * startRect.left + progress * endRect.left, - (1 - progress) * startRect.top + progress * endRect.top, - (1 - progress) * startRect.right + progress * endRect.right, - (1 - progress) * startRect.bottom + progress * endRect.bottom, - radius, - radius, - Path.Direction.CW, - ) + val left = (1 - progress) * startRect.left + progress * endRect.left + val top = (1 - progress) * startRect.top + progress * endRect.top + val right = (1 - progress) * startRect.right + progress * endRect.right + val bottom = (1 - progress) * startRect.bottom + progress * endRect.bottom + path.path.addRoundRect(left, top, right, bottom, radius, radius, Path.Direction.CW) + path.setBounds(left, top, right, bottom) + path.setCornerRadius(radius) } val shouldUseSpringAnimation = Flags.enableLauncherIconShapes() && Flags.enableExpressiveFolderExpansion() @@ -190,6 +205,11 @@ interface ShapeDelegate { addToPath(path, offsetX, offsetY, radius, Matrix()) } + override fun addToPath(path: PathWrapper, offsetX: Float, offsetY: Float, radius: Float) { + addToPath(path.path, offsetX, offsetY, radius, Matrix()) + path.estimateBoundsFromPath() + } + private fun addToPath( path: Path, offsetX: Float, @@ -232,21 +252,28 @@ interface ShapeDelegate { cornerR = endRadius, ), ) + val pathProvider = { progress: Float, path: PathWrapper -> + path.path.set(morph.toPath(progress, path.path)) + path.estimateBoundsFromPath() + } val shouldUseSpringAnimation = Flags.enableLauncherIconShapes() && Flags.enableExpressiveFolderExpansion() return if (shouldUseSpringAnimation) { - ClipSpringAnimBuilder(target, morph::toPath).toAnim(isReversed) + ClipSpringAnimBuilder(target, pathProvider).toAnim(isReversed) } else { - ClipAnimBuilder(target, morph::toPath).toAnim(isReversed) + ClipAnimBuilder(target, pathProvider).toAnim(isReversed) } } } - private class ClipAnimBuilder(val target: T, val pathProvider: (Float, Path) -> Unit) : + private class ClipAnimBuilder( + val target: T, + val pathProvider: (Float, PathWrapper) -> Unit + ) : AnimatorListenerAdapter(), AnimatorUpdateListener where T : View, T : ClipPathView { private var oldOutlineProvider: ViewOutlineProvider? = null - val path = Path() + val path = PathWrapper() override fun onAnimationStart(animation: Animator) { target.apply { @@ -278,11 +305,14 @@ interface ShapeDelegate { } } - private class ClipSpringAnimBuilder(val target: T, val pathProvider: (Float, Path) -> Unit) : + private class ClipSpringAnimBuilder( + val target: T, + val pathProvider: (Float, PathWrapper) -> Unit + ) : AnimatorListenerAdapter() where T : View, T : ClipPathView { private var oldOutlineProvider: ViewOutlineProvider? = null - val path = Path() + val path = PathWrapper() private val animatorBuilder = SpringAnimationBuilder(target.context) private val progressProperty = object : FloatProperty>("progress") { diff --git a/src/com/android/launcher3/views/ClipIconView.java b/src/com/android/launcher3/views/ClipIconView.java index f7fdd613a0..12bbc61b34 100644 --- a/src/com/android/launcher3/views/ClipIconView.java +++ b/src/com/android/launcher3/views/ClipIconView.java @@ -29,7 +29,6 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Outline; -import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.AdaptiveIconDrawable; @@ -48,6 +47,7 @@ import com.android.launcher3.Flags; import com.android.launcher3.R; import com.android.launcher3.Utilities; import com.android.launcher3.dragndrop.FolderAdaptiveIcon; +import com.android.launcher3.graphics.PathWrapper; import com.android.launcher3.graphics.ShapeDelegate; import com.android.launcher3.graphics.ThemeManager; @@ -73,7 +73,7 @@ public class ClipIconView extends View implements ClipPathView { private final Rect mStartRevealRect = new Rect(); private final Rect mEndRevealRect = new Rect(); - private Path mClipPath; + private PathWrapper mClipPath; private float mTaskCornerRadius; private final Rect mOutline = new Rect(); @@ -304,7 +304,7 @@ public class ClipIconView extends View implements ClipPathView { } @Override - public void setClipPath(Path clipPath) { + public void setClipPath(PathWrapper clipPath) { mClipPath = clipPath; invalidate(); } @@ -313,7 +313,7 @@ public class ClipIconView extends View implements ClipPathView { public void draw(Canvas canvas) { int count1 = canvas.save(); if (mClipPath != null) { - canvas.clipPath(mClipPath); + canvas.clipPath(mClipPath.getPath()); } int count2 = canvas.save(); if (mBackground != null) { diff --git a/src/com/android/launcher3/views/ClipPathView.java b/src/com/android/launcher3/views/ClipPathView.java index 2152e1d818..465d694b20 100644 --- a/src/com/android/launcher3/views/ClipPathView.java +++ b/src/com/android/launcher3/views/ClipPathView.java @@ -15,13 +15,14 @@ */ package com.android.launcher3.views; -import android.graphics.Path; import android.view.View; +import com.android.launcher3.graphics.PathWrapper; + /** * Alternative to using {@link View#getClipToOutline()} as it only works with derivatives of * rounded rect. */ public interface ClipPathView { - void setClipPath(Path clipPath); + void setClipPath(PathWrapper clipPath); } diff --git a/tests/multivalentTests/src/com/android/launcher3/graphics/ShapeDelegateTest.kt b/tests/multivalentTests/src/com/android/launcher3/graphics/ShapeDelegateTest.kt index 7e38f0e0ea..6bc60d6d02 100644 --- a/tests/multivalentTests/src/com/android/launcher3/graphics/ShapeDelegateTest.kt +++ b/tests/multivalentTests/src/com/android/launcher3/graphics/ShapeDelegateTest.kt @@ -129,7 +129,7 @@ class ShapeDelegateTest { anim.setCurrentFraction(0f) assertThat( getAreaDiff( - target.currentClip!!, + target.currentClip!!.path, cookiePath(Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)), ) ) @@ -137,7 +137,7 @@ class ShapeDelegateTest { // Verify that end rect is similar to end path anim.setCurrentFraction(1f) - assertThat(getAreaDiff(target.currentClip!!, roundedRectPath(0.5f))) + assertThat(getAreaDiff(target.currentClip!!.path, roundedRectPath(0.5f))) .isLessThan(AREA_CALC_SIZE) // Ensure that when running animation, area increases smoothly. We run the animation over @@ -151,7 +151,7 @@ class ShapeDelegateTest { var lastPath = cookiePath(Rect(0, 0, AREA_CALC_SIZE, AREA_CALC_SIZE)) for (progress in 1..steps) { anim.setCurrentFraction(progress / 1000f) - val currentPath = Path(target.currentClip!!) + val currentPath = Path(target.currentClip!!.path) assertThat(getAreaDiff(lastPath, currentPath)).isLessThan(incrementalDiff) lastPath = currentPath } @@ -197,9 +197,9 @@ class ShapeDelegateTest { class TestClipView : View(context), ClipPathView { - var currentClip: Path? = null + var currentClip: PathWrapper? = null - override fun setClipPath(clipPath: Path?) { + override fun setClipPath(clipPath: PathWrapper?) { currentClip = clipPath } }