Skip to content

Commit c8b9b1c

Browse files
dsn5ftpekingme
authored andcommittedJan 5, 2024
[Predictive Back][Navigation Drawer] Animate corners during predictive back when no drawerLayoutCornerSize is set
PiperOrigin-RevId: 596056382
1 parent c218b3c commit c8b9b1c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
 

‎lib/java/com/google/android/material/motion/MaterialBackAnimationHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public MaterialBackAnimationHelper(@NonNull V view) {
7171
context, R.attr.motionDurationShort2, CANCEL_DURATION_DEFAULT);
7272
}
7373

74-
protected float interpolateProgress(float progress) {
74+
public float interpolateProgress(float progress) {
7575
return progressInterpolator.getInterpolation(progress);
7676
}
7777

‎lib/java/com/google/android/material/navigation/NavigationView.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import androidx.drawerlayout.widget.DrawerLayout;
7676
import androidx.drawerlayout.widget.DrawerLayout.DrawerListener;
7777
import androidx.drawerlayout.widget.DrawerLayout.SimpleDrawerListener;
78+
import com.google.android.material.animation.AnimationUtils;
7879
import com.google.android.material.drawable.DrawableUtils;
7980
import com.google.android.material.internal.ContextUtils;
8081
import com.google.android.material.internal.NavigationMenu;
@@ -146,6 +147,8 @@ public class NavigationView extends ScrimInsetsFrameLayout implements MaterialBa
146147
private boolean bottomInsetScrimEnabled = true;
147148

148149
@Px private int drawerLayoutCornerSize = 0;
150+
private final boolean drawerLayoutCornerSizeBackAnimationEnabled;
151+
@Px private final int drawerLayoutCornerSizeBackAnimationMax;
149152
private final ShapeableDelegate shapeableDelegate = ShapeableDelegate.create(this);
150153

151154
private final MaterialSideContainerBackHelper sideContainerBackHelper =
@@ -166,6 +169,7 @@ public void onDrawerOpened(@NonNull View drawerView) {
166169
public void onDrawerClosed(@NonNull View drawerView) {
167170
if (drawerView == NavigationView.this) {
168171
backOrchestrator.stopListeningForBackCallbacks();
172+
maybeClearCornerSizeAnimationForDrawerLayout();
169173
}
170174
}
171175
};
@@ -199,6 +203,9 @@ public NavigationView(@NonNull Context context, @Nullable AttributeSet attrs, in
199203
// placed inside a drawer layout.
200204
drawerLayoutCornerSize =
201205
a.getDimensionPixelSize(R.styleable.NavigationView_drawerLayoutCornerSize, 0);
206+
drawerLayoutCornerSizeBackAnimationEnabled = drawerLayoutCornerSize == 0;
207+
drawerLayoutCornerSizeBackAnimationMax =
208+
getResources().getDimensionPixelSize(R.dimen.m3_navigation_drawer_layout_corner_size);
202209

203210
// Set the background to a MaterialShapeDrawable if it hasn't been set or if it can be converted
204211
// to a MaterialShapeDrawable.
@@ -400,7 +407,7 @@ public void setForceCompatClippingEnabled(boolean enabled) {
400407
private void maybeUpdateCornerSizeForDrawerLayout(@Px int width, @Px int height) {
401408
if (getParent() instanceof DrawerLayout
402409
&& getLayoutParams() instanceof DrawerLayout.LayoutParams
403-
&& drawerLayoutCornerSize > 0
410+
&& (drawerLayoutCornerSize > 0 || drawerLayoutCornerSizeBackAnimationEnabled)
404411
&& getBackground() instanceof MaterialShapeDrawable) {
405412
int layoutGravity = ((DrawerLayout.LayoutParams) getLayoutParams()).gravity;
406413
boolean isAbsGravityLeft =
@@ -432,6 +439,13 @@ && getBackground() instanceof MaterialShapeDrawable) {
432439
}
433440
}
434441

442+
private void maybeClearCornerSizeAnimationForDrawerLayout() {
443+
if (drawerLayoutCornerSizeBackAnimationEnabled && drawerLayoutCornerSize != 0) {
444+
drawerLayoutCornerSize = 0;
445+
maybeUpdateCornerSizeForDrawerLayout(getWidth(), getHeight());
446+
}
447+
}
448+
435449
private boolean hasShapeAppearance(@NonNull TintTypedArray a) {
436450
return a.hasValue(R.styleable.NavigationView_itemShapeAppearance)
437451
|| a.hasValue(R.styleable.NavigationView_itemShapeAppearanceOverlay);
@@ -983,6 +997,13 @@ public void startBackProgress(@NonNull BackEventCompat backEvent) {
983997
public void updateBackProgress(@NonNull BackEventCompat backEvent) {
984998
Pair<DrawerLayout, DrawerLayout.LayoutParams> drawerLayoutPair = requireDrawerLayoutParent();
985999
sideContainerBackHelper.updateBackProgress(backEvent, drawerLayoutPair.second.gravity);
1000+
1001+
if (drawerLayoutCornerSizeBackAnimationEnabled) {
1002+
float progress = sideContainerBackHelper.interpolateProgress(backEvent.getProgress());
1003+
drawerLayoutCornerSize =
1004+
AnimationUtils.lerp(0, drawerLayoutCornerSizeBackAnimationMax, progress);
1005+
maybeUpdateCornerSizeForDrawerLayout(getWidth(), getHeight());
1006+
}
9861007
}
9871008

9881009
@Override
@@ -1009,6 +1030,7 @@ public void handleBackInvoked() {
10091030
public void cancelBackProgress() {
10101031
requireDrawerLayoutParent();
10111032
sideContainerBackHelper.cancelBackProgress();
1033+
maybeClearCornerSizeAnimationForDrawerLayout();
10121034
}
10131035

10141036
@CanIgnoreReturnValue

0 commit comments

Comments
 (0)
Please sign in to comment.