3535import android .graphics .Rect ;
3636import android .graphics .drawable .ColorDrawable ;
3737import android .graphics .drawable .Drawable ;
38- import android .graphics .drawable .LayerDrawable ;
3938import android .os .Build ;
4039import android .os .Build .VERSION ;
4140import android .os .Build .VERSION_CODES ;
7372import androidx .core .view .accessibility .AccessibilityViewCommand ;
7473import androidx .customview .view .AbsSavedState ;
7574import com .google .android .material .animation .AnimationUtils ;
76- import com .google .android .material .animation .ArgbEvaluatorCompat ;
7775import com .google .android .material .appbar .AppBarLayout .BaseBehavior .SavedState ;
7876import com .google .android .material .color .MaterialColors ;
7977import com .google .android .material .drawable .DrawableUtils ;
@@ -210,7 +208,7 @@ public interface LiftOnScrollListener {
210208 private boolean liftOnScroll ;
211209 @ IdRes private int liftOnScrollTargetViewId ;
212210 @ Nullable private WeakReference <View > liftOnScrollTargetView ;
213- @ Nullable private final ColorStateList liftOnScrollColor ;
211+ private final boolean hasLiftOnScrollColor ;
214212 @ Nullable private ValueAnimator liftOnScrollColorAnimator ;
215213 @ Nullable private AnimatorUpdateListener liftOnScrollColorUpdateListener ;
216214 private final List <LiftOnScrollListener > liftOnScrollListeners = new ArrayList <>();
@@ -259,19 +257,19 @@ public AppBarLayout(@NonNull Context context, @Nullable AttributeSet attrs, int
259257
260258 ViewCompat .setBackground (this , a .getDrawable (R .styleable .AppBarLayout_android_background ));
261259
262- liftOnScrollColor =
263- MaterialResources .getColorStateList (
264- context , a , R . styleable . AppBarLayout_liftOnScrollColor ) ;
260+ ColorStateList liftOnScrollColor =
261+ MaterialResources .getColorStateList (context , a , R . styleable . AppBarLayout_liftOnScrollColor );
262+ hasLiftOnScrollColor = liftOnScrollColor != null ;
265263
266- ColorStateList backgroundColorStateList =
267- DrawableUtils .getColorStateListOrNull (getBackground ());
268- if (backgroundColorStateList != null ) {
264+ ColorStateList originalBackgroundColor = DrawableUtils .getColorStateListOrNull (getBackground ());
265+ if (originalBackgroundColor != null ) {
269266 MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable ();
270- materialShapeDrawable .setFillColor (backgroundColorStateList );
267+ materialShapeDrawable .setFillColor (originalBackgroundColor );
271268 // If there is a lift on scroll color specified, we do not initialize the elevation overlay
272269 // and set the alpha to zero manually.
273270 if (liftOnScrollColor != null ) {
274- initializeLiftOnScrollWithColor (materialShapeDrawable );
271+ initializeLiftOnScrollWithColor (
272+ materialShapeDrawable , originalBackgroundColor , liftOnScrollColor );
275273 } else {
276274 initializeLiftOnScrollWithElevation (context , materialShapeDrawable );
277275 }
@@ -328,26 +326,20 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
328326 });
329327 }
330328
331- private void initializeLiftOnScrollWithColor (MaterialShapeDrawable background ) {
332- MaterialShapeDrawable liftBackground = new MaterialShapeDrawable ();
333- liftBackground .setFillColor (liftOnScrollColor );
334- liftBackground .setAlpha (lifted ? 255 : 0 );
335- background .setAlpha (lifted ? 0 : 255 );
336-
329+ private void initializeLiftOnScrollWithColor (
330+ MaterialShapeDrawable background ,
331+ @ NonNull ColorStateList originalBackgroundColor ,
332+ @ NonNull ColorStateList liftOnScrollColor ) {
337333 Integer colorSurface = MaterialColors .getColorOrNull (getContext (), R .attr .colorSurface );
338-
339334 liftOnScrollColorUpdateListener =
340335 valueAnimator -> {
341- float liftAlpha = (float ) valueAnimator .getAnimatedValue ();
342- background .setAlpha ((int ) (255f - liftAlpha ));
343- liftBackground .setAlpha ((int ) liftAlpha );
344-
336+ float liftProgress = (float ) valueAnimator .getAnimatedValue ();
345337 int mixedColor =
346- ArgbEvaluatorCompat . getInstance ()
347- . evaluate (
348- liftAlpha / 255f ,
349- background . getResolvedTintColor (),
350- liftBackground . getResolvedTintColor ( ));
338+ MaterialColors . layer (
339+ originalBackgroundColor . getDefaultColor (),
340+ liftOnScrollColor . getDefaultColor () ,
341+ liftProgress );
342+ background . setFillColor ( ColorStateList . valueOf ( mixedColor ));
351343 if (statusBarForeground != null
352344 && statusBarForegroundOriginalColor != null
353345 && statusBarForegroundOriginalColor .equals (colorSurface )) {
@@ -362,9 +354,7 @@ private void initializeLiftOnScrollWithColor(MaterialShapeDrawable background) {
362354 }
363355 }
364356 };
365-
366- LayerDrawable layerBackground = new LayerDrawable (new Drawable [] {background , liftBackground });
367- ViewCompat .setBackground (this , layerBackground );
357+ ViewCompat .setBackground (this , background );
368358 }
369359
370360 private void initializeLiftOnScrollWithElevation (
@@ -693,9 +683,6 @@ public CoordinatorLayout.Behavior<AppBarLayout> getBehavior() {
693683 @ Nullable
694684 public MaterialShapeDrawable getMaterialShapeBackground () {
695685 Drawable background = getBackground ();
696- if (background instanceof LayerDrawable ) {
697- background = ((LayerDrawable ) background ).getDrawable (0 );
698- }
699686 return background instanceof MaterialShapeDrawable ? (MaterialShapeDrawable ) background : null ;
700687 }
701688
@@ -1043,10 +1030,10 @@ boolean setLiftedState(boolean lifted, boolean force) {
10431030 this .lifted = lifted ;
10441031 refreshDrawableState ();
10451032 if (isLiftOnScrollCompatibleBackground ()) {
1046- if (liftOnScrollColor != null ) {
1033+ if (hasLiftOnScrollColor ) {
10471034 // Only start the liftOnScrollColor based animation because the elevation based
10481035 // animation will happen via the lifted drawable state change and state list animator.
1049- startLiftOnScrollColorAnimation (lifted ? 0 : 255 , lifted ? 255 : 0 );
1036+ startLiftOnScrollColorAnimation (lifted ? 0 : 1 , lifted ? 1 : 0 );
10501037 } else if (liftOnScroll ) {
10511038 startLiftOnScrollColorAnimation (
10521039 lifted ? 0 : appBarElevation , lifted ? appBarElevation : 0 );
@@ -1058,19 +1045,7 @@ boolean setLiftedState(boolean lifted, boolean force) {
10581045 }
10591046
10601047 private boolean isLiftOnScrollCompatibleBackground () {
1061- Drawable background = getBackground ();
1062- if (background instanceof MaterialShapeDrawable ) {
1063- return true ;
1064- }
1065- if (background instanceof LayerDrawable ) {
1066- LayerDrawable layerBackground = (LayerDrawable ) background ;
1067- if (layerBackground .getNumberOfLayers () == 2
1068- && layerBackground .getDrawable (0 ) instanceof MaterialShapeDrawable
1069- && layerBackground .getDrawable (1 ) instanceof MaterialShapeDrawable ) {
1070- return true ;
1071- }
1072- }
1073- return false ;
1048+ return getBackground () instanceof MaterialShapeDrawable ;
10741049 }
10751050
10761051 private void startLiftOnScrollColorAnimation (
0 commit comments