forked from material-components/material-components-android
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchViewAnimationHelper.java
More file actions
1405 lines (1238 loc) · 55.4 KB
/
SearchViewAnimationHelper.java
File metadata and controls
1405 lines (1238 loc) · 55.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 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
*
* https://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.google.android.material.search;
import com.google.android.material.R;
import static com.google.android.material.animation.AnimationUtils.lerp;
import static java.lang.Math.max;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION_CODES;
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewParent;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.activity.BackEventCompat;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.dynamicanimation.animation.FloatPropertyCompat;
import androidx.dynamicanimation.animation.SpringAnimation;
import androidx.dynamicanimation.animation.SpringForce;
import com.google.android.material.animation.AnimationCoordinator;
import com.google.android.material.animation.AnimationCoordinator.Listener;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.internal.ClippableRoundedCornerLayout;
import com.google.android.material.internal.FadeThroughDrawable;
import com.google.android.material.internal.FadeThroughUpdateListener;
import com.google.android.material.internal.MultiViewUpdateListener;
import com.google.android.material.internal.RectEvaluator;
import com.google.android.material.internal.ReversableAnimatedValueInterpolator;
import com.google.android.material.internal.ToolbarUtils;
import com.google.android.material.internal.TouchObserverFrameLayout;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.motion.MaterialMainContainerBackHelper;
import com.google.android.material.motion.MotionUtils;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** Helper class for {@link SearchView} animations. */
@SuppressWarnings("RestrictTo")
class SearchViewAnimationHelper {
// Constants for show expand animation
private static final long SHOW_DURATION_MS = 300;
private static final long SHOW_CLEAR_BUTTON_ALPHA_DURATION_MS = 50;
private static final long SHOW_CLEAR_BUTTON_ALPHA_START_DELAY_MS = 250;
private static final long SHOW_CONTENT_ALPHA_DURATION_MS = 150;
private static final long SHOW_CONTENT_ALPHA_START_DELAY_MS = 75;
private static final long SHOW_CONTENT_SCALE_DURATION_MS = SHOW_DURATION_MS;
private static final long SHOW_SCRIM_ALPHA_DURATION_MS = 100;
// Constants for hide collapse animation
private static final long HIDE_DURATION_MS = 250;
private static final long HIDE_CLEAR_BUTTON_ALPHA_DURATION_MS = 42;
private static final long HIDE_CLEAR_BUTTON_ALPHA_START_DELAY_MS = 0;
private static final long HIDE_CONTENT_ALPHA_DURATION_MS = 83;
private static final long HIDE_CONTENT_ALPHA_START_DELAY_MS = 0;
private static final long HIDE_CONTENT_SCALE_DURATION_MS = HIDE_DURATION_MS;
private static final float CONTENT_FROM_SCALE = 0.95f;
// Constants for show translate animation
private static final long SHOW_TRANSLATE_DURATION_MS = 350;
private static final long SHOW_TRANSLATE_KEYBOARD_START_DELAY_MS = 150;
// Constants for hide translate animation
private static final long HIDE_TRANSLATE_DURATION_MS = 300;
// Default duration for when a themed duration is not defined.
private static final int DEFAULT_DURATION_MS = 100;
// Default interpolator for when a themed interpolator is not defined.
private static final TimeInterpolator DEFAULT_INTERPOLATOR = AnimationUtils.LINEAR_INTERPOLATOR;
private final SearchView searchView;
private final View scrim;
private final View backgroundView;
private final ClippableRoundedCornerLayout rootView;
private final FrameLayout headerContainer;
private final FrameLayout toolbarContainer;
private final Toolbar toolbar;
private final Toolbar dummyToolbar;
private final LinearLayout textContainer;
private final TextView searchPrefix;
private final TextView dummyTextView;
private final EditText editText;
private final ImageButton clearButton;
private final View divider;
private final TouchObserverFrameLayout contentContainer;
private final MaterialMainContainerBackHelper backHelper;
@Nullable private AnimatorSet backProgressAnimatorSet;
private SearchBar searchBar;
private final Context context;
private final AnimationDelegate animationDelegate;
private final TimeInterpolator standardAccelerateInterpolator;
private final TimeInterpolator standardDecelerateInterpolator;
private final int durationShort1;
private final int durationShort2;
SearchViewAnimationHelper(
Context context, SearchView searchView, boolean containedAnimationEnabled) {
this.context = context;
this.searchView = searchView;
this.scrim = searchView.scrim;
this.backgroundView = searchView.backgroundView;
this.rootView = searchView.rootView;
this.headerContainer = searchView.headerContainer;
this.toolbarContainer = searchView.toolbarContainer;
this.toolbar = searchView.toolbar;
this.dummyToolbar = searchView.dummyToolbar;
this.searchPrefix = searchView.searchPrefix;
this.dummyTextView = searchView.dummyTextView;
this.editText = searchView.editText;
this.clearButton = searchView.clearButton;
this.divider = searchView.divider;
this.contentContainer = searchView.contentContainer;
this.textContainer = searchView.textContainer;
backHelper = new MaterialMainContainerBackHelper(rootView);
standardAccelerateInterpolator =
MotionUtils.resolveThemeInterpolator(
context, R.attr.motionEasingStandardAccelerateInterpolator, DEFAULT_INTERPOLATOR);
standardDecelerateInterpolator =
MotionUtils.resolveThemeInterpolator(
context, R.attr.motionEasingStandardDecelerateInterpolator, DEFAULT_INTERPOLATOR);
durationShort1 =
MotionUtils.resolveThemeDuration(context, R.attr.motionDurationShort1, DEFAULT_DURATION_MS);
durationShort2 =
MotionUtils.resolveThemeDuration(context, R.attr.motionDurationShort2, DEFAULT_DURATION_MS);
animationDelegate =
containedAnimationEnabled
? new ContainedAnimationDelegate()
: new DefaultAnimationDelegate();
}
void setSearchBar(SearchBar searchBar) {
this.searchBar = searchBar;
}
void show() {
if (searchBar != null) {
startShowAnimationExpand();
} else {
startShowAnimationTranslate();
}
}
@CanIgnoreReturnValue
AnimatorSet hide() {
if (searchBar != null) {
return startHideAnimationCollapse();
} else {
return startHideAnimationTranslate();
}
}
private void startShowAnimationExpand() {
if (searchView.isAdjustNothingSoftInputMode()) {
searchView.requestFocusAndShowKeyboardIfNeeded();
}
searchView.setTransitionState(SearchView.TransitionState.SHOWING);
animationDelegate.setUpDummyToolbarIfNeeded();
editText.setText(searchBar.getText());
editText.setSelection(editText.getText().length());
rootView.setVisibility(View.INVISIBLE);
rootView.post(
() -> {
boolean show = true;
AnimationCoordinator coordinator = new AnimationCoordinator();
coordinator.addAnimator(getExpandCollapseAnimatorSet(show));
for (SpringAnimation springAnimation : getExpandCollapseSpringAnimations(show)) {
coordinator.addDynamicAnimation(springAnimation);
}
coordinator.addListener(
new Listener() {
@Override
public void onAnimationsStart() {
animationDelegate.onAnimationStart(show);
rootView.setVisibility(View.VISIBLE);
searchBar.stopOnLoadAnimation();
}
@Override
public void onAnimationsEnd() {
animationDelegate.onAnimationEnd(show);
if (!searchView.isAdjustNothingSoftInputMode()) {
searchView.requestFocusAndShowKeyboardIfNeeded();
}
searchView.setTransitionState(SearchView.TransitionState.SHOWN);
}
});
coordinator.start();
});
}
private AnimatorSet startHideAnimationCollapse() {
if (searchView.isAdjustNothingSoftInputMode()) {
searchView.clearFocusAndHideKeyboard();
}
boolean show = false;
AnimationCoordinator coordinator = new AnimationCoordinator();
AnimatorSet animatorSet = getExpandCollapseAnimatorSet(show);
coordinator.addAnimator(animatorSet);
for (SpringAnimation springAnimation : getExpandCollapseSpringAnimations(show)) {
coordinator.addDynamicAnimation(springAnimation);
}
coordinator.addListener(
new Listener() {
@Override
public void onAnimationsStart() {
animationDelegate.onAnimationStart(show);
searchView.setTransitionState(SearchView.TransitionState.HIDING);
}
@Override
public void onAnimationsEnd() {
animationDelegate.onAnimationEnd(show);
rootView.setVisibility(View.GONE);
if (!searchView.isAdjustNothingSoftInputMode()) {
searchView.clearFocusAndHideKeyboard();
}
searchView.setTransitionState(SearchView.TransitionState.HIDDEN);
}
});
coordinator.start();
return animatorSet;
}
private void startShowAnimationTranslate() {
if (searchView.isAdjustNothingSoftInputMode()) {
searchView.postDelayed(
searchView::requestFocusAndShowKeyboardIfNeeded,
SHOW_TRANSLATE_KEYBOARD_START_DELAY_MS);
}
rootView.setVisibility(View.INVISIBLE);
rootView.post(
() -> {
rootView.setTranslationY(rootView.getHeight());
AnimatorSet animatorSet = getTranslateAnimatorSet(true);
animatorSet.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
rootView.setVisibility(View.VISIBLE);
searchView.setTransitionState(SearchView.TransitionState.SHOWING);
}
@Override
public void onAnimationEnd(Animator animation) {
if (!searchView.isAdjustNothingSoftInputMode()) {
searchView.requestFocusAndShowKeyboardIfNeeded();
}
searchView.setTransitionState(SearchView.TransitionState.SHOWN);
}
});
animatorSet.start();
});
}
private AnimatorSet startHideAnimationTranslate() {
if (searchView.isAdjustNothingSoftInputMode()) {
searchView.clearFocusAndHideKeyboard();
}
AnimatorSet animatorSet = getTranslateAnimatorSet(false);
animatorSet.addListener(
new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
searchView.setTransitionState(SearchView.TransitionState.HIDING);
}
@Override
public void onAnimationEnd(Animator animation) {
rootView.setVisibility(View.GONE);
if (!searchView.isAdjustNothingSoftInputMode()) {
searchView.clearFocusAndHideKeyboard();
}
searchView.setTransitionState(SearchView.TransitionState.HIDDEN);
}
});
animatorSet.start();
return animatorSet;
}
private AnimatorSet getTranslateAnimatorSet(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(getTranslationYAnimator());
addBackButtonProgressAnimatorIfNeeded(animatorSet);
animatorSet.setInterpolator(
ReversableAnimatedValueInterpolator.of(show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
animatorSet.setDuration(show ? SHOW_TRANSLATE_DURATION_MS : HIDE_TRANSLATE_DURATION_MS);
return animatorSet;
}
private Animator getTranslationYAnimator() {
ValueAnimator animator = ValueAnimator.ofFloat(rootView.getHeight(), 0);
animator.addUpdateListener(MultiViewUpdateListener.translationYListener(rootView));
return animator;
}
private AnimatorSet getExpandCollapseAnimatorSet(boolean show) {
AnimatorSet animatorSet = animationDelegate.getExpandCollapseAnimatorSet(show);
if (backProgressAnimatorSet == null) {
animatorSet.playTogether(getButtonsProgressAnimator(show));
}
return animatorSet;
}
/**
* Returns a list that contains all the physics-based spring animations for the contained style
* expand/collapse animation.
*/
private List<SpringAnimation> getExpandCollapseSpringAnimations(boolean show) {
return animationDelegate.getExpandCollapseSpringAnimations(show);
}
private Animator getClearButtonAnimator(boolean show) {
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(
show ? SHOW_CLEAR_BUTTON_ALPHA_DURATION_MS : HIDE_CLEAR_BUTTON_ALPHA_DURATION_MS);
animator.setStartDelay(
show ? SHOW_CLEAR_BUTTON_ALPHA_START_DELAY_MS : HIDE_CLEAR_BUTTON_ALPHA_START_DELAY_MS);
animator.setInterpolator(
ReversableAnimatedValueInterpolator.of(show, AnimationUtils.LINEAR_INTERPOLATOR));
animator.addUpdateListener(MultiViewUpdateListener.alphaListener(clearButton));
return animator;
}
private AnimatorSet getButtonsProgressAnimator(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
addBackButtonProgressAnimatorIfNeeded(animatorSet);
animatorSet.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animatorSet.setInterpolator(
ReversableAnimatedValueInterpolator.of(show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
return animatorSet;
}
private void addBackButtonProgressAnimatorIfNeeded(AnimatorSet animatorSet) {
ImageButton backButton = ToolbarUtils.getNavigationIconButton(toolbar);
if (backButton == null) {
return;
}
Drawable drawable = DrawableCompat.unwrap(backButton.getDrawable());
if (searchView.isAnimatedNavigationIcon()) {
addDrawerArrowDrawableAnimatorIfNeeded(animatorSet, drawable);
addFadeThroughDrawableAnimatorIfNeeded(animatorSet, drawable);
addBackButtonAnimatorIfNeeded(animatorSet, backButton);
} else {
setFullDrawableProgressIfNeeded(drawable);
}
}
private void addBackButtonAnimatorIfNeeded(AnimatorSet animatorSet, ImageButton backButton) {
// If there's no navigation icon on the search bar, we should set the alpha for the button
// itself instead of the drawables since the button background has a ripple.
if (searchBar == null || searchBar.getNavigationIcon() != null) {
return;
}
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(
animation -> backButton.setAlpha((Float) animation.getAnimatedValue()));
animatorSet.playTogether(animator);
}
private void addDrawerArrowDrawableAnimatorIfNeeded(AnimatorSet animatorSet, Drawable drawable) {
if (drawable instanceof DrawerArrowDrawable) {
DrawerArrowDrawable drawerArrowDrawable = (DrawerArrowDrawable) drawable;
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(
animation -> drawerArrowDrawable.setProgress((Float) animation.getAnimatedValue()));
animatorSet.playTogether(animator);
}
}
private void addFadeThroughDrawableAnimatorIfNeeded(AnimatorSet animatorSet, Drawable drawable) {
if (drawable instanceof FadeThroughDrawable) {
FadeThroughDrawable fadeThroughDrawable = (FadeThroughDrawable) drawable;
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(
animation -> fadeThroughDrawable.setProgress((Float) animation.getAnimatedValue()));
animatorSet.playTogether(animator);
}
}
private void setFullDrawableProgressIfNeeded(Drawable drawable) {
if (drawable instanceof DrawerArrowDrawable) {
((DrawerArrowDrawable) drawable).setProgress(1);
}
if (drawable instanceof FadeThroughDrawable) {
((FadeThroughDrawable) drawable).setProgress(1);
}
}
private void setMenuItemsNotClickable(Toolbar toolbar) {
ActionMenuView actionMenuView = ToolbarUtils.getActionMenuView(toolbar);
if (actionMenuView != null) {
for (int i = 0; i < actionMenuView.getChildCount(); i++) {
View menuItem = actionMenuView.getChildAt(i);
menuItem.setClickable(false);
menuItem.setFocusable(false);
menuItem.setFocusableInTouchMode(false);
}
}
}
void startBackProgress(@NonNull BackEventCompat backEvent) {
backHelper.startBackProgress(backEvent, searchBar);
}
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
public void updateBackProgress(@NonNull BackEventCompat backEvent) {
if (backEvent.getProgress() <= 0f) {
return;
}
backHelper.updateBackProgress(backEvent, searchBar, searchBar.getCornerSize());
if (backProgressAnimatorSet == null) {
if (searchView.isAdjustNothingSoftInputMode()) {
searchView.clearFocusAndHideKeyboard();
}
// Early return if navigation icon animation is disabled.
if (!searchView.isAnimatedNavigationIcon()) {
return;
}
// Start and immediately pause the animator set so we can seek it with setCurrentPlayTime() in
// subsequent updateBackProgress() calls when the progress value changes.
backProgressAnimatorSet = getButtonsProgressAnimator(/* show= */ false);
backProgressAnimatorSet.start();
backProgressAnimatorSet.pause();
} else {
backProgressAnimatorSet.setCurrentPlayTime(
(long) (backEvent.getProgress() * backProgressAnimatorSet.getDuration()));
}
}
@Nullable
public BackEventCompat onHandleBackInvoked() {
return backHelper.onHandleBackInvoked();
}
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
public void finishBackProgress() {
AnimatorSet hideAnimatorSet = hide();
long totalDuration = hideAnimatorSet.getTotalDuration();
backHelper.finishBackProgress(totalDuration, searchBar);
if (backProgressAnimatorSet != null) {
animationDelegate.startButtonsTranslationAnimation();
backProgressAnimatorSet.resume();
}
backProgressAnimatorSet = null;
}
@RequiresApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
public void cancelBackProgress() {
backHelper.cancelBackProgress(searchBar);
if (backProgressAnimatorSet != null) {
backProgressAnimatorSet.reverse();
}
backProgressAnimatorSet = null;
}
MaterialMainContainerBackHelper getBackHelper() {
return backHelper;
}
/**
* Sets the alpha of the background. Note that this doesn't set the alpha on the entire {@code
* backgroundView}, but only on the background while retaining visibility of its children.
*/
private void setBackgroundAlpha(float alpha) {
backgroundView.getBackground().mutate().setAlpha((int) (alpha * 255));
}
private void setContentViewsAlpha(float alpha) {
clearButton.setAlpha(alpha);
divider.setAlpha(alpha);
contentContainer.setAlpha(alpha);
setActionMenuViewAlphaIfNeeded(alpha);
}
private void setActionMenuViewAlphaIfNeeded(float alpha) {
if (searchView.isMenuItemsAnimated()) {
ActionMenuView actionMenuView = ToolbarUtils.getActionMenuView(toolbar);
if (actionMenuView != null) {
actionMenuView.setAlpha(alpha);
}
}
}
private int getTranslationXBetweenViews(
@Nullable View searchBarSubView, @NonNull View searchViewSubView) {
// If there is no equivalent for the SearchView subview in the SearchBar, we return the
// translation between the SearchBar and the start of the SearchView subview
if (searchBarSubView == null) {
int marginStart = ((MarginLayoutParams) searchViewSubView.getLayoutParams()).getMarginStart();
int paddingStart = searchBar.getPaddingStart();
int searchBarLeft = getViewLeftFromSearchViewParent(searchBar);
return ViewUtils.isLayoutRtl(searchBar)
? searchBarLeft
+ searchBar.getWidth()
+ marginStart
- paddingStart
- searchView.getRight()
: (searchBarLeft - marginStart + paddingStart);
}
return getViewLeftFromSearchViewParent(searchBarSubView)
- getViewLeftFromSearchViewParent(searchViewSubView);
}
private int getViewLeftFromSearchViewParent(@NonNull View v) {
int left = v.getLeft();
ViewParent viewParent = v.getParent();
while (viewParent instanceof View && viewParent != searchView.getParent()) {
left += ((View) viewParent).getLeft();
viewParent = viewParent.getParent();
}
return left;
}
private int getViewTopFromSearchViewParent(@NonNull View v) {
int top = v.getTop();
ViewParent viewParent = v.getParent();
while (viewParent instanceof View && viewParent != searchView.getParent()) {
top += ((View) viewParent).getTop();
viewParent = viewParent.getParent();
}
return top;
}
private class DefaultAnimationDelegate implements AnimationDelegate {
@Override
public void setUpDummyToolbarIfNeeded() {
Menu menu = dummyToolbar.getMenu();
if (menu != null) {
menu.clear();
}
if (searchBar.getMenuResId() != SearchBar.NO_RES_ID && searchView.isMenuItemsAnimated()) {
dummyToolbar.inflateMenu(searchBar.getMenuResId());
setMenuItemsNotClickable(dummyToolbar);
dummyToolbar.setVisibility(View.VISIBLE);
} else {
dummyToolbar.setVisibility(View.GONE);
}
}
@NonNull
@Override
public AnimatorSet getExpandCollapseAnimatorSet(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
getScrimAlphaAnimator(show),
getRootViewAnimator(show),
getClearButtonAnimator(show),
getContentAnimator(show),
getHeaderContainerAnimator(show),
getDummyToolbarAnimator(show),
getActionMenuViewsAlphaAnimator(show),
getEditTextAnimator(show),
getSearchPrefixAnimator(show),
getTextAnimator(show));
return animatorSet;
}
@NonNull
@Override
public List<SpringAnimation> getExpandCollapseSpringAnimations(boolean show) {
return new ArrayList<>();
}
@Override
public void onAnimationStart(boolean show) {
setContentViewsAlpha(show ? 0 : 1);
}
@Override
public void onAnimationEnd(boolean show) {
setContentViewsAlpha(show ? 1 : 0);
// Reset edittext and searchbar textview alphas after the animations are finished since
// the visibilities for searchview and searchbar have been set accordingly.
editText.setAlpha(1);
if (searchBar != null) {
searchBar.getTextView().setAlpha(1);
}
// Reset clip bounds so it can react to the screen or layout changes.
editText.setClipBounds(null);
// After expanding or collapsing, we should reset the clip bounds so it can react to the
// screen or layout changes. Otherwise it will result in wrong clipping on the layout.
rootView.resetClipBoundsAndCornerRadii();
// After collapsing, we should reset the expanded corner radii in case the search view
// is shown in a different location the next time.
if (!show) {
backHelper.clearExpandedCornerRadii();
}
}
@Override
public void startButtonsTranslationAnimation() {
getButtonsTranslationAnimator(/* show= */ false).start();
}
private Animator getScrimAlphaAnimator(boolean show) {
TimeInterpolator interpolator =
show ? AnimationUtils.LINEAR_INTERPOLATOR : AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR;
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animator.setStartDelay(show ? SHOW_SCRIM_ALPHA_DURATION_MS : 0);
animator.setInterpolator(ReversableAnimatedValueInterpolator.of(show, interpolator));
animator.addUpdateListener(MultiViewUpdateListener.alphaListener(scrim));
return animator;
}
private Animator getRootViewAnimator(boolean show) {
Rect initialHideToClipBounds = backHelper.getInitialHideToClipBounds();
Rect initialHideFromClipBounds = backHelper.getInitialHideFromClipBounds();
Rect toClipBounds =
initialHideToClipBounds != null
? initialHideToClipBounds
: ViewUtils.calculateRectFromBounds(searchView);
Rect fromClipBounds =
initialHideFromClipBounds != null
? initialHideFromClipBounds
: ViewUtils.calculateOffsetRectFromBounds(rootView, searchBar);
Rect clipBounds = new Rect(fromClipBounds);
float fromCornerRadius = searchBar.getCornerSize();
float[] toCornerRadius =
maxCornerRadii(rootView.getCornerRadii(), backHelper.getExpandedCornerRadii());
ValueAnimator animator =
ValueAnimator.ofObject(new RectEvaluator(clipBounds), fromClipBounds, toClipBounds);
animator.addUpdateListener(
valueAnimator -> {
float[] cornerRadii =
lerpCornerRadii(
fromCornerRadius, toCornerRadius, valueAnimator.getAnimatedFraction());
rootView.updateClipBoundsAndCornerRadii(clipBounds, cornerRadii);
});
animator.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animator.setInterpolator(
ReversableAnimatedValueInterpolator.of(
show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
return animator;
}
private float[] maxCornerRadii(float[] startValue, float[] endValue) {
return new float[] {
max(startValue[0], endValue[0]),
max(startValue[1], endValue[1]),
max(startValue[2], endValue[2]),
max(startValue[3], endValue[3]),
max(startValue[4], endValue[4]),
max(startValue[5], endValue[5]),
max(startValue[6], endValue[6]),
max(startValue[7], endValue[7])
};
}
private float[] lerpCornerRadii(float startValue, float[] endValue, float fraction) {
return new float[] {
lerp(startValue, endValue[0], fraction),
lerp(startValue, endValue[1], fraction),
lerp(startValue, endValue[2], fraction),
lerp(startValue, endValue[3], fraction),
lerp(startValue, endValue[4], fraction),
lerp(startValue, endValue[5], fraction),
lerp(startValue, endValue[6], fraction),
lerp(startValue, endValue[7], fraction)
};
}
private Animator getDummyToolbarAnimator(boolean show) {
return getTranslationAnimator(
show,
dummyToolbar,
getFromTranslationXEnd(dummyToolbar) - (searchBar.getPaddingEnd() - dummyToolbar.getPaddingEnd()),
getFromTranslationY());
}
private Animator getHeaderContainerAnimator(boolean show) {
return getTranslationAnimator(
show, headerContainer, getFromTranslationXEnd(headerContainer), getFromTranslationY());
}
private Animator getActionMenuViewsAlphaAnimator(boolean show) {
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animator.setInterpolator(
ReversableAnimatedValueInterpolator.of(
show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
if (searchView.isMenuItemsAnimated()) {
ActionMenuView dummyActionMenuView = ToolbarUtils.getActionMenuView(dummyToolbar);
ActionMenuView actionMenuView = ToolbarUtils.getActionMenuView(toolbar);
animator.addUpdateListener(
new FadeThroughUpdateListener(dummyActionMenuView, actionMenuView));
}
return animator;
}
private Animator getSearchPrefixAnimator(boolean show) {
return getTranslationAnimatorForText(show, searchPrefix);
}
private Animator getEditTextAnimator(boolean show) {
return getTranslationAnimatorForText(show, editText);
}
private AnimatorSet getTextAnimator(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
addTextFadeAnimatorIfNeeded(animatorSet);
addEditTextClipAnimator(animatorSet);
animatorSet.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animatorSet.setInterpolator(
ReversableAnimatedValueInterpolator.of(show, AnimationUtils.LINEAR_INTERPOLATOR));
return animatorSet;
}
private void addEditTextClipAnimator(AnimatorSet animatorSet) {
// We only want to add a clip animation if the edittext and searchbar text is the same, which
// means it is translating instead of fading.
if (searchBar == null || !TextUtils.equals(editText.getText(), searchBar.getText())) {
return;
}
Rect editTextClipBounds = new Rect(0, 0, editText.getWidth(), editText.getHeight());
ValueAnimator animator =
ValueAnimator.ofInt(searchBar.getTextView().getWidth(), editText.getWidth());
animator.addUpdateListener(
animation -> {
editTextClipBounds.right = (int) animation.getAnimatedValue();
editText.setClipBounds(editTextClipBounds);
});
animatorSet.playTogether(animator);
}
private void addTextFadeAnimatorIfNeeded(AnimatorSet animatorSet) {
if (searchBar == null || TextUtils.equals(editText.getText(), searchBar.getText())) {
return;
}
// If the searchbar text is not equal to the searchview edittext, we want to fade out the
// edittext and fade in the searchbar text
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(
animation -> {
editText.setAlpha((Float) animation.getAnimatedValue());
searchBar.getTextView().setAlpha(1 - (Float) animation.getAnimatedValue());
});
animatorSet.playTogether(animator);
}
private Animator getTranslationAnimatorForText(boolean show, View v) {
TextView textView = searchBar.getPlaceholderTextView();
// If the placeholder text is empty, we animate to the searchbar textview instead.
// Or if we're showing the searchview, we always animate from the searchbar textview, not
// from the placeholder text.
if (TextUtils.isEmpty(textView.getText()) || show) {
textView = searchBar.getTextView();
}
int startX =
getViewLeftFromSearchViewParent(textView) - (v.getLeft() + textContainer.getLeft());
return getTranslationAnimator(show, v, startX, getFromTranslationY());
}
private Animator getContentAnimator(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
getContentAlphaAnimator(show), getDividerAnimator(show), getContentScaleAnimator(show));
return animatorSet;
}
private Animator getContentAlphaAnimator(boolean show) {
ValueAnimator animatorAlpha = ValueAnimator.ofFloat(0, 1);
animatorAlpha.setDuration(
show ? SHOW_CONTENT_ALPHA_DURATION_MS : HIDE_CONTENT_ALPHA_DURATION_MS);
animatorAlpha.setStartDelay(
show ? SHOW_CONTENT_ALPHA_START_DELAY_MS : HIDE_CONTENT_ALPHA_START_DELAY_MS);
animatorAlpha.setInterpolator(
ReversableAnimatedValueInterpolator.of(show, AnimationUtils.LINEAR_INTERPOLATOR));
animatorAlpha.addUpdateListener(
MultiViewUpdateListener.alphaListener(divider, contentContainer));
return animatorAlpha;
}
private Animator getDividerAnimator(boolean show) {
float dividerTranslationY =
(float) contentContainer.getHeight() * (1f - CONTENT_FROM_SCALE) / 2f;
ValueAnimator animatorDivider = ValueAnimator.ofFloat(dividerTranslationY, 0);
animatorDivider.setDuration(
show ? SHOW_CONTENT_SCALE_DURATION_MS : HIDE_CONTENT_SCALE_DURATION_MS);
animatorDivider.setInterpolator(
ReversableAnimatedValueInterpolator.of(
show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
animatorDivider.addUpdateListener(MultiViewUpdateListener.translationYListener(divider));
return animatorDivider;
}
private Animator getContentScaleAnimator(boolean show) {
ValueAnimator animatorScale = ValueAnimator.ofFloat(CONTENT_FROM_SCALE, 1);
animatorScale.setDuration(
show ? SHOW_CONTENT_SCALE_DURATION_MS : HIDE_CONTENT_SCALE_DURATION_MS);
animatorScale.setInterpolator(
ReversableAnimatedValueInterpolator.of(
show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
animatorScale.addUpdateListener(MultiViewUpdateListener.scaleListener(contentContainer));
return animatorScale;
}
private Animator getTranslationAnimator(boolean show, View view, int startX, int startY) {
ValueAnimator animatorX = ValueAnimator.ofFloat(startX, 0);
animatorX.addUpdateListener(MultiViewUpdateListener.translationXListener(view));
ValueAnimator animatorY = ValueAnimator.ofFloat(startY, 0);
animatorY.addUpdateListener(MultiViewUpdateListener.translationYListener(view));
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animatorX, animatorY);
animatorSet.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animatorSet.setInterpolator(
ReversableAnimatedValueInterpolator.of(
show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
return animatorSet;
}
private int getFromTranslationXEnd(View view) {
int marginEnd = ((MarginLayoutParams) view.getLayoutParams()).getMarginEnd();
int viewLeft = getViewLeftFromSearchViewParent(searchBar);
return ViewUtils.isLayoutRtl(searchBar)
? viewLeft - marginEnd
: viewLeft + searchBar.getWidth() + marginEnd - searchView.getWidth();
}
private int getFromTranslationY() {
int toolbarMiddleY = toolbarContainer.getTop() + toolbarContainer.getHeight() / 2;
int searchBarMiddleY = getViewTopFromSearchViewParent(searchBar) + searchBar.getHeight() / 2;
return searchBarMiddleY - toolbarMiddleY;
}
private AnimatorSet getButtonsTranslationAnimator(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
addBackButtonTranslationAnimatorIfNeeded(animatorSet);
addActionMenuViewAnimatorIfNeeded(animatorSet);
animatorSet.setDuration(show ? SHOW_DURATION_MS : HIDE_DURATION_MS);
animatorSet.setInterpolator(
ReversableAnimatedValueInterpolator.of(
show, AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR));
return animatorSet;
}
private void addBackButtonTranslationAnimatorIfNeeded(AnimatorSet animatorSet) {
ImageButton searchViewBackButton = ToolbarUtils.getNavigationIconButton(toolbar);
if (searchViewBackButton == null) {
return;
}
ImageButton searchBarBackButton = ToolbarUtils.getNavigationIconButton(searchBar);
ValueAnimator backButtonAnimatorX =
ValueAnimator.ofFloat(
getTranslationXBetweenViews(searchBarBackButton, searchViewBackButton), 0);
backButtonAnimatorX.addUpdateListener(
MultiViewUpdateListener.translationXListener(searchViewBackButton));
ValueAnimator backButtonAnimatorY = ValueAnimator.ofFloat(getFromTranslationY(), 0);
backButtonAnimatorY.addUpdateListener(
MultiViewUpdateListener.translationYListener(searchViewBackButton));
animatorSet.playTogether(backButtonAnimatorX, backButtonAnimatorY);
}
private void addActionMenuViewAnimatorIfNeeded(AnimatorSet animatorSet) {
ActionMenuView searchViewActionMenuView = ToolbarUtils.getActionMenuView(toolbar);
if (searchViewActionMenuView == null) {
return;
}
ActionMenuView searchBarActionMenuView = ToolbarUtils.getActionMenuView(searchBar);
ValueAnimator actionMenuViewAnimatorX =
ValueAnimator.ofFloat(
getTranslationXBetweenViews(searchBarActionMenuView, searchViewActionMenuView), 0);
actionMenuViewAnimatorX.addUpdateListener(
MultiViewUpdateListener.translationXListener(searchViewActionMenuView));
ValueAnimator actionMenuViewAnimatorY = ValueAnimator.ofFloat(getFromTranslationY(), 0);
actionMenuViewAnimatorY.addUpdateListener(
MultiViewUpdateListener.translationYListener(searchViewActionMenuView));
animatorSet.playTogether(actionMenuViewAnimatorX, actionMenuViewAnimatorY);
}
}
private class ContainedAnimationDelegate implements AnimationDelegate {
@Override
public void setUpDummyToolbarIfNeeded() {
setUpDummyTextViewIfNeeded();
// Copy the search bar background to dummy toolbar so to create a seamless transition. Needed
// because search bar may have a different background color from the search view toolbar.
if (searchBar.getBackground() != null
&& searchBar.getBackground().getConstantState() != null) {
dummyToolbar.setBackground(searchBar.getBackground().getConstantState().newDrawable());
}
Menu menu = dummyToolbar.getMenu();
if (menu != null) {
menu.clear();
}
// Inflate the dummy toolbar menu to match the search bar if needed.
if (searchBar.getMenuResId() != SearchBar.NO_RES_ID && searchView.isMenuItemsAnimated()) {
dummyToolbar.inflateMenu(searchBar.getMenuResId());
setMenuItemsNotClickable(dummyToolbar);
}
}
private void setUpDummyTextViewIfNeeded() {
TextView searchBarTextView = searchBar.getTextView();
dummyTextView.setText(searchBarTextView.getText());
dummyTextView.setHint(searchBarTextView.getHint());
dummyTextView.setVisibility(View.VISIBLE);
}
@NonNull
@Override
public AnimatorSet getExpandCollapseAnimatorSet(boolean show) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(
getBackgroundAlphaAnimator(show),
getContentAlphaAnimator(show),
getToolbarAlphaAnimator(show),
getDummyTextViewWidthAnimator(show),
getClearButtonAnimator(show),
getSearchBarSiblingsTranslationAnimator(show));
return animatorSet;
}
@NonNull
@Override
public List<SpringAnimation> getExpandCollapseSpringAnimations(boolean show) {
return Arrays.asList(
getToolbarWidthSpringAnimation(show),
getToolbarTranslationXSpringAnimation(show),