-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathExtendedFloatingActionButton.java
1555 lines (1366 loc) · 51 KB
/
ExtendedFloatingActionButton.java
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 (C) 2019 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.google.android.material.floatingactionbutton;
import com.google.android.material.R;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
import static java.lang.Math.min;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Property;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import androidx.annotation.AnimatorRes;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout.AttachedBehavior;
import androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior;
import com.google.android.material.animation.MotionSpec;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.internal.DescendantOffsetUtils;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.shape.ShapeAppearanceModel;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
/**
* Extended floating action buttons are used for a special type of promoted action. They are
* distinguished by an icon and a text floating above the UI and have special motion behaviors
* related to morphing, launching, and the transferring anchor point.
*
* <p>Extended floating action buttons may have icon and text, but may also hold just an icon or
* text.
*
* <p>As this class descends from {@link MaterialButton}, you can control the icon which is
* displayed via {@link #setIcon(android.graphics.drawable.Drawable)}, and the text via {@link
* #setText(CharSequence)}.
*
* <p>The background color of this view defaults to the your theme's {@code colorSecondary}. If you
* wish to change this at runtime then you can do so via {@link
* #setBackgroundTintList(android.content.res.ColorStateList)}.
*
* <p>For more information, see the <a
* href="https://github.com/material-components/material-components-android/blob/master/docs/components/FloatingActionButton.md">component
* developer guidance</a> and <a href="https://material.io/components/extended-fab/overview">design
* guidelines</a>.
*/
public class ExtendedFloatingActionButton extends MaterialButton implements AttachedBehavior {
private static final int DEF_STYLE_RES =
R.style.Widget_MaterialComponents_ExtendedFloatingActionButton_Icon;
private static final int ANIM_STATE_NONE = 0;
private static final int ANIM_STATE_HIDING = 1;
private static final int ANIM_STATE_SHOWING = 2;
private int animState = ANIM_STATE_NONE;
/** Strategy to show the FAB. */
private static final int SHOW = 0;
/** Strategy to hide the FAB. */
private static final int HIDE = 1;
/** Strategy to shrink the FAB. */
private static final int SHRINK = 2;
/** Strategy to extend the FAB. */
private static final int EXTEND = 3;
private boolean animationEnabled = true;
/**
* The strategy type determines what motion strategy to apply on the FAB.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@IntDef({SHOW, HIDE, SHRINK, EXTEND})
@Retention(RetentionPolicy.SOURCE)
private @interface StrategyType {}
private final AnimatorTracker changeVisibilityTracker = new AnimatorTracker();
@NonNull private final MotionStrategy shrinkStrategy;
@NonNull private final MotionStrategy extendStrategy;
private final MotionStrategy showStrategy = new ShowStrategy(changeVisibilityTracker);
private final MotionStrategy hideStrategy = new HideStrategy(changeVisibilityTracker);
private final int collapsedSize;
private int extendedPaddingStart;
private int extendedPaddingEnd;
@NonNull private final Behavior<ExtendedFloatingActionButton> behavior;
private boolean isExtended = true;
private boolean isTransforming = false;
private boolean animateShowBeforeLayout = false;
@NonNull protected ColorStateList originalTextCsl;
/** Extend strategy to extend FAB back to the width from which it shrunk. */
private static final int EXTEND_STRATEGY_AUTO = 0;
/** Extend strategy to extend FAB to wrap its content. */
private static final int EXTEND_STRATEGY_WRAP_CONTENT = 1;
/** Extend strategy to extend FAB to match parent. */
private static final int EXTEND_STRATEGY_MATCH_PARENT = 2;
/**
* Extend strategy to choose how to extend the fab.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
@IntDef({EXTEND_STRATEGY_AUTO, EXTEND_STRATEGY_WRAP_CONTENT, EXTEND_STRATEGY_MATCH_PARENT})
@Retention(RetentionPolicy.SOURCE)
private @interface ExtendStrategy {}
private int originalWidth;
private int originalHeight;
@ExtendStrategy private final int extendStrategyType;
/**
* Callback to be invoked when the visibility or the state of an ExtendedFloatingActionButton
* changes.
*/
public abstract static class OnChangedCallback {
/**
* Called when a ExtendedFloatingActionButton has been {@link
* #show(OnChangedCallback) shown}.
*
* @param extendedFab the FloatingActionButton that was shown.
*/
public void onShown(ExtendedFloatingActionButton extendedFab) {}
/**
* Called when a ExtendedFloatingActionButton has been {@link
* #hide(OnChangedCallback) hidden}.
*
* @param extendedFab the ExtendedFloatingActionButton that was hidden.
*/
public void onHidden(ExtendedFloatingActionButton extendedFab) {}
/**
* Called when a ExtendedFloatingActionButton has been {@link
* #extend(OnChangedCallback) extended} to show the icon and the
* text.
*
* @param extendedFab the ExtendedFloatingActionButton that was extended.
*/
public void onExtended(ExtendedFloatingActionButton extendedFab) {}
/**
* Called when a ExtendedFloatingActionButton has been {@link
* #shrink(OnChangedCallback) shrunken} to show just the icon.
*
* @param extendedFab the ExtendedFloatingActionButton that was shrunk.
*/
public void onShrunken(ExtendedFloatingActionButton extendedFab) {}
}
public ExtendedFloatingActionButton(@NonNull Context context) {
this(context, null);
}
public ExtendedFloatingActionButton(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.extendedFloatingActionButtonStyle);
}
@SuppressWarnings("nullness")
public ExtendedFloatingActionButton(
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext();
behavior = new ExtendedFloatingActionButtonBehavior<>(context, attrs);
TypedArray a =
ThemeEnforcement.obtainStyledAttributes(
context, attrs, R.styleable.ExtendedFloatingActionButton, defStyleAttr, DEF_STYLE_RES);
MotionSpec showMotionSpec =
MotionSpec.createFromAttribute(
context, a, R.styleable.ExtendedFloatingActionButton_showMotionSpec);
MotionSpec hideMotionSpec =
MotionSpec.createFromAttribute(
context, a, R.styleable.ExtendedFloatingActionButton_hideMotionSpec);
MotionSpec extendMotionSpec =
MotionSpec.createFromAttribute(
context, a, R.styleable.ExtendedFloatingActionButton_extendMotionSpec);
MotionSpec shrinkMotionSpec =
MotionSpec.createFromAttribute(
context, a, R.styleable.ExtendedFloatingActionButton_shrinkMotionSpec);
collapsedSize =
a.getDimensionPixelSize(R.styleable.ExtendedFloatingActionButton_collapsedSize, -1);
extendStrategyType =
a.getInt(
R.styleable.ExtendedFloatingActionButton_extendStrategy, EXTEND_STRATEGY_WRAP_CONTENT);
extendedPaddingStart = getPaddingStart();
extendedPaddingEnd = getPaddingEnd();
AnimatorTracker changeSizeTracker = new AnimatorTracker();
extendStrategy =
new ChangeSizeStrategy(
changeSizeTracker,
getSizeFromExtendStrategyType(extendStrategyType),
/* extending= */ true);
shrinkStrategy =
new ChangeSizeStrategy(
changeSizeTracker,
new Size() {
@Override
public int getWidth() {
return getCollapsedSize();
}
@Override
public int getHeight() {
return getCollapsedSize();
}
@Override
public int getPaddingStart() {
return getCollapsedPadding();
}
@Override
public int getPaddingEnd() {
return getCollapsedPadding();
}
@Override
public LayoutParams getLayoutParams() {
return new LayoutParams(getWidth(), getHeight());
}
},
/* extending= */ false);
showStrategy.setMotionSpec(showMotionSpec);
hideStrategy.setMotionSpec(hideMotionSpec);
extendStrategy.setMotionSpec(extendMotionSpec);
shrinkStrategy.setMotionSpec(shrinkMotionSpec);
a.recycle();
ShapeAppearanceModel shapeAppearanceModel =
ShapeAppearanceModel.builder(
context, attrs, defStyleAttr, DEF_STYLE_RES, ShapeAppearanceModel.PILL
).build();
setShapeAppearanceModel(shapeAppearanceModel);
saveOriginalTextCsl();
}
private Size getSizeFromExtendStrategyType(@ExtendStrategy int extendStrategyType) {
Size wrapContentSize =
new Size() {
@Override
public int getWidth() {
return getMeasuredWidth()
- getCollapsedPadding() * 2
+ extendedPaddingStart
+ extendedPaddingEnd;
}
@Override
public int getHeight() {
return getMeasuredHeight();
}
@Override
public int getPaddingStart() {
return extendedPaddingStart;
}
@Override
public int getPaddingEnd() {
return extendedPaddingEnd;
}
@Override
public LayoutParams getLayoutParams() {
return new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
}
};
Size matchParentSize =
new Size() {
@Override
public int getWidth() {
int padding = 0;
int margins = 0;
if (!(getParent() instanceof View)) {
return wrapContentSize.getWidth();
}
View parent = (View) getParent();
// If the parent's width is wrap content and the fab's width is match parent,
// we return the wrap content width.
LayoutParams parentLayoutParams = parent.getLayoutParams();
if (parentLayoutParams != null && parentLayoutParams.width == WRAP_CONTENT) {
return wrapContentSize.getWidth();
}
padding += parent.getPaddingLeft() + parent.getPaddingRight();
if (ExtendedFloatingActionButton.this.getLayoutParams() instanceof MarginLayoutParams) {
MarginLayoutParams layoutParams =
(MarginLayoutParams) ExtendedFloatingActionButton.this.getLayoutParams();
if (layoutParams != null) {
margins += layoutParams.leftMargin + layoutParams.rightMargin;
}
}
return parent.getWidth() - margins - padding;
}
@Override
public int getHeight() {
// If the extend strategy is MATCH_PARENT, we will extend the height to the original
// height.
if (originalHeight == MATCH_PARENT) {
if (!(getParent() instanceof View)) {
return wrapContentSize.getHeight();
}
int padding = 0;
int margins = 0;
View parent = (View) getParent();
// If the parent's height is wrap content and the fab's height is match parent,
// we return the wrap content height.
LayoutParams parentLayoutParams = parent.getLayoutParams();
if (parentLayoutParams != null && parentLayoutParams.height == WRAP_CONTENT) {
return wrapContentSize.getHeight();
}
padding += parent.getPaddingTop() + parent.getPaddingBottom();
if (ExtendedFloatingActionButton.this.getLayoutParams()
instanceof MarginLayoutParams) {
MarginLayoutParams layoutParams =
(MarginLayoutParams) ExtendedFloatingActionButton.this.getLayoutParams();
if (layoutParams != null) {
margins += layoutParams.topMargin + layoutParams.bottomMargin;
}
}
return parent.getHeight() - margins - padding;
} else if (originalHeight == 0 || originalHeight == WRAP_CONTENT) {
return wrapContentSize.getHeight();
}
return originalHeight;
}
@Override
public int getPaddingStart() {
return extendedPaddingStart;
}
@Override
public int getPaddingEnd() {
return extendedPaddingEnd;
}
@Override
public LayoutParams getLayoutParams() {
return new LayoutParams(
MATCH_PARENT, originalHeight == 0 ? WRAP_CONTENT : originalHeight);
}
};
Size autoSize =
new Size() {
@Override
public int getWidth() {
if (originalWidth == MATCH_PARENT) {
return matchParentSize.getWidth();
} else if (originalWidth == 0 || originalWidth == WRAP_CONTENT) {
return wrapContentSize.getWidth();
}
return originalWidth;
}
@Override
public int getHeight() {
if (originalHeight == MATCH_PARENT) {
return matchParentSize.getHeight();
} else if (originalHeight == 0 || originalHeight == WRAP_CONTENT) {
return wrapContentSize.getHeight();
}
return originalHeight;
}
@Override
public int getPaddingStart() {
return extendedPaddingStart;
}
@Override
public int getPaddingEnd() {
return extendedPaddingEnd;
}
@Override
public LayoutParams getLayoutParams() {
return new LayoutParams(
originalWidth == 0 ? WRAP_CONTENT : originalWidth,
originalHeight == 0 ? WRAP_CONTENT : originalHeight);
}
};
switch (extendStrategyType) {
case EXTEND_STRATEGY_WRAP_CONTENT:
return wrapContentSize;
case EXTEND_STRATEGY_MATCH_PARENT:
return matchParentSize;
case EXTEND_STRATEGY_AUTO:
default:
return autoSize;
}
}
@Override
public void setTextColor(int color) {
super.setTextColor(color);
saveOriginalTextCsl();
}
@Override
public void setTextColor(@NonNull ColorStateList colors) {
super.setTextColor(colors);
saveOriginalTextCsl();
}
private void saveOriginalTextCsl() {
originalTextCsl = getTextColors();
}
/**
* Update the text color without affecting the original, client-set color.
*/
protected void silentlyUpdateTextColor(@NonNull ColorStateList csl) {
// Call super to avoid saving this silent update through extended FAB's setTextColor overrides.
super.setTextColor(csl);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
// Shrink the button in case the text is empty.
if (isExtended && TextUtils.isEmpty(getText()) && getIcon() != null) {
isExtended = false;
shrinkStrategy.performNow();
}
}
@NonNull
@Override
public Behavior<ExtendedFloatingActionButton> getBehavior() {
return behavior;
}
/**
* Extends or shrinks the fab depending on the value of {@param extended}.
*/
public void setExtended(boolean extended) {
if (this.isExtended == extended) {
return;
}
MotionStrategy motionStrategy = extended ? extendStrategy : shrinkStrategy;
if (motionStrategy.shouldCancel()) {
return;
}
motionStrategy.performNow();
}
public final boolean isExtended() {
return isExtended;
}
/**
* Sets whether to enable animation for a call to show {@link #show} even if the view has not been
* laid out yet.
*
* <p>This may be set to {@code true} if the button is initially hidden but should animate when
* later shown. The default is {@code false}.
*/
public void setAnimateShowBeforeLayout(boolean animateShowBeforeLayout) {
this.animateShowBeforeLayout = animateShowBeforeLayout;
}
@Override
public void setPaddingRelative(int start, int top, int end, int bottom) {
super.setPaddingRelative(start, top, end, bottom);
if (isExtended && !isTransforming) {
extendedPaddingStart = start;
extendedPaddingEnd = end;
}
}
@Override
public void setPadding(int left, int top, int right, int bottom) {
super.setPadding(left, top, right, bottom);
if (isExtended && !isTransforming) {
extendedPaddingStart = getPaddingStart();
extendedPaddingEnd = getPaddingEnd();
}
}
@Override
public CharSequence getAccessibilityClassName() {
return FloatingActionButton.ACCESSIBIILTY_FAB_ROLE;
}
/**
* Add a listener that will be invoked when this ExtendedFloatingActionButton is shown. See {@link
* AnimatorListener}.
*
* <p>Components that add a listener should take care to remove it when finished via {@link
* #removeOnShowAnimationListener(AnimatorListener)}.
*
* @param listener listener to add
*/
public void addOnShowAnimationListener(@NonNull AnimatorListener listener) {
showStrategy.addAnimationListener(listener);
}
/**
* Remove a listener that was previously added via
* {@link #addOnShowAnimationListener(AnimatorListener)}.
*
* @param listener listener to remove
*/
public void removeOnShowAnimationListener(@NonNull AnimatorListener listener) {
showStrategy.removeAnimationListener(listener);
}
/**
* Add a listener that will be invoked when this ExtendedFloatingActionButton is hidden. See
* {@link AnimatorListener}.
*
* <p>Components that add a listener should take care to remove it when finished via {@link
* #removeOnHideAnimationListener(AnimatorListener)}.
*
* @param listener listener to add
*/
public void addOnHideAnimationListener(@NonNull AnimatorListener listener) {
hideStrategy.addAnimationListener(listener);
}
/**
* Remove a listener that was previously added via
* {@link #addOnHideAnimationListener(AnimatorListener)}.
*
* @param listener listener to remove
*/
public void removeOnHideAnimationListener(@NonNull AnimatorListener listener) {
hideStrategy.removeAnimationListener(listener);
}
/**
* Add a listener that will be invoked when this ExtendedFloatingActionButton is shrunk. See
* {@link AnimatorListener}.
*
* <p>Components that add a listener should take care to remove it when finished via {@link
* #removeOnShrinkAnimationListener(AnimatorListener)}.
*
* @param listener listener to add
*/
public void addOnShrinkAnimationListener(@NonNull AnimatorListener listener) {
shrinkStrategy.addAnimationListener(listener);
}
/**
* Remove a listener that was previously added via
* {@link #addOnShrinkAnimationListener(AnimatorListener)}.
*
* @param listener listener to remove
*/
public void removeOnShrinkAnimationListener(@NonNull AnimatorListener listener) {
shrinkStrategy.removeAnimationListener(listener);
}
/**
* Add a listener that will be invoked when this ExtendedFloatingActionButton is extended. See
* {@link AnimatorListener}.
*
* <p>Components that add a listener should take care to remove it when finished via {@link
* #removeOnExtendAnimationListener(AnimatorListener)}.
*
* @param listener listener to add
*/
public void addOnExtendAnimationListener(@NonNull AnimatorListener listener) {
extendStrategy.addAnimationListener(listener);
}
/**
* Remove a listener that was previously added via
* {@link #addOnExtendAnimationListener(AnimatorListener)}.
*
* @param listener listener to remove
*/
public void removeOnExtendAnimationListener(@NonNull AnimatorListener listener) {
extendStrategy.removeAnimationListener(listener);
}
/**
* Hides the button.
*
* <p>This method will animate the button hide if the view has already been laid out.
*/
public void hide() {
performMotion(HIDE, null);
}
/**
* Hides the button.
*
* <p>This method will animate the button hide if the view has already been laid out.
*
* @param callback the callback to notify when this view is hidden
*/
public void hide(@NonNull OnChangedCallback callback) {
performMotion(HIDE, callback);
}
/**
* Shows the button.
*
* <p>This method will animate the button show if the view has already been laid out, or if {@link
* #setAnimateShowBeforeLayout} is {@code true}.
*/
public void show() {
performMotion(SHOW, null);
}
/**
* Shows the button.
*
* <p>This method will animate the button show if the view has already been laid out, or if {@link
* #setAnimateShowBeforeLayout} is {@code true}.
*
* @param callback the callback to notify when this view is shown
*/
public void show(@NonNull OnChangedCallback callback) {
performMotion(SHOW, callback);
}
/**
* Extends the FAB to show the text and the icon.
*
* <p>This method will not affect an extended FAB which holds just text and no icon. Also, this
* method will animate the button show if the view has already been laid out.
*
* @see #extend(OnChangedCallback)
*/
public void extend() {
performMotion(EXTEND, null);
}
/**
* Extends the FAB to show the text and the icon.
*
* <p>This method will not affect an extended FAB which holds just text and no icon. Also, this
* method will animate the button show if the view has already been laid out.
*
* @param callback the callback to notify when the FAB is extended
*/
public void extend(@NonNull final OnChangedCallback callback) {
performMotion(EXTEND, callback);
}
/**
* Shrinks the FAB to show just the icon.
*
* <p>This method will not affect an extended FAB which holds just text and no icon. Also, this
* method will animate the button show if the view has already been laid out.
*
* @see #shrink(OnChangedCallback)
*/
public void shrink() {
performMotion(SHRINK, null);
}
/**
* Shrinks the FAB to show just the icon.
*
* <p>This method will not affect an extended FAB which holds just text and no icon. Also, this
* method will animate the button show if the view has already been laid out.
*
* @param callback the callback to notify when the FAB shrank
*/
public void shrink(@NonNull final OnChangedCallback callback) {
performMotion(SHRINK, callback);
}
/** Returns the motion spec for the show animation. */
@Nullable
public MotionSpec getShowMotionSpec() {
return showStrategy.getMotionSpec();
}
/**
* Updates the motion spec for the show animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_showMotionSpec
*/
public void setShowMotionSpec(@Nullable MotionSpec spec) {
showStrategy.setMotionSpec(spec);
}
/**
* Updates the motion spec for the show animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_showMotionSpec
*/
public void setShowMotionSpecResource(@AnimatorRes int id) {
setShowMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/** Returns the motion spec for the hide animation. */
@Nullable
public MotionSpec getHideMotionSpec() {
return hideStrategy.getMotionSpec();
}
/**
* Updates the motion spec for the hide animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_hideMotionSpec
*/
public void setHideMotionSpec(@Nullable MotionSpec spec) {
hideStrategy.setMotionSpec(spec);
}
/**
* Updates the motion spec for the hide animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_hideMotionSpec
*/
public void setHideMotionSpecResource(@AnimatorRes int id) {
setHideMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/** Returns the motion spec for the extend animation. */
@Nullable
public MotionSpec getExtendMotionSpec() {
return extendStrategy.getMotionSpec();
}
/**
* Updates the motion spec for the extend animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_extendMotionSpec
*/
public void setExtendMotionSpec(@Nullable MotionSpec spec) {
extendStrategy.setMotionSpec(spec);
}
/**
* Updates the motion spec for the extend animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_extendMotionSpec
*/
public void setExtendMotionSpecResource(@AnimatorRes int id) {
setExtendMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Returns the motion spec for the shrink animation.
*/
@Nullable
public MotionSpec getShrinkMotionSpec() {
return shrinkStrategy.getMotionSpec();
}
/**
* Updates the motion spec for the shrink animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_shrinkMotionSpec
*/
public void setShrinkMotionSpec(@Nullable MotionSpec spec) {
shrinkStrategy.setMotionSpec(spec);
}
/**
* Updates the motion spec for the shrink animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_shrinkMotionSpec
*/
public void setShrinkMotionSpecResource(@AnimatorRes int id) {
setShrinkMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
private void performMotion(
@StrategyType final int strategyType, @Nullable final OnChangedCallback callback) {
MotionStrategy strategy;
switch (strategyType) {
case SHOW:
strategy = showStrategy;
break;
case HIDE:
strategy = hideStrategy;
break;
case SHRINK:
strategy = shrinkStrategy;
break;
case EXTEND:
strategy = extendStrategy;
break;
default:
throw new IllegalStateException("Unknown strategy type: " + strategyType);
}
if (strategy.shouldCancel()) {
return;
}
boolean shouldAnimate = shouldAnimateVisibilityChange();
if (!shouldAnimate) {
strategy.performNow();
strategy.onChange(callback);
return;
}
// If shrinking, we should remember what width to re-expand at.
if (strategyType == SHRINK) {
LayoutParams lp = getLayoutParams();
if (lp != null) {
originalWidth = lp.width;
originalHeight = lp.height;
} else {
originalWidth = getWidth();
originalHeight = getHeight();
}
}
measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
Animator animator = strategy.createAnimator();
animator.addListener(
new AnimatorListenerAdapter() {
private boolean cancelled;
@Override
public void onAnimationStart(Animator animation) {
strategy.onAnimationStart(animation);
cancelled = false;
}
@Override
public void onAnimationCancel(Animator animation) {
cancelled = true;
strategy.onAnimationCancel();
}
@Override
public void onAnimationEnd(Animator animation) {
strategy.onAnimationEnd();
if (!cancelled) {
strategy.onChange(callback);
}
}
});
for (AnimatorListener l : strategy.getListeners()) {
animator.addListener(l);
}
animator.start();
}
private boolean isOrWillBeShown() {
if (getVisibility() != View.VISIBLE) {
// If we're not currently visible, return true if we're animating to be shown
return animState == ANIM_STATE_SHOWING;
} else {
// Otherwise if we're visible, return true if we're not animating to be hidden
return animState != ANIM_STATE_HIDING;
}
}
private boolean isOrWillBeHidden() {
if (getVisibility() == View.VISIBLE) {
// If we're currently visible, return true if we're animating to be hidden
return animState == ANIM_STATE_HIDING;
} else {
// Otherwise if we're not visible, return true if we're not animating to be shown
return animState != ANIM_STATE_SHOWING;
}
}
/**
* Set whether or not animations are enabled.
*/
public void setAnimationEnabled(boolean animationEnabled) {
this.animationEnabled = animationEnabled;
}
/** Return whether or not animations are enabled. */
public boolean isAnimationEnabled() {
return animationEnabled;
}
private boolean shouldAnimateVisibilityChange() {
return animationEnabled
&& (isLaidOut() || (!isOrWillBeShown() && animateShowBeforeLayout))
&& !isInEditMode();
}
/**
* A Property wrapper around the <code>width</code> functionality handled by the {@link
* LayoutParams#width} value.
*/
static final Property<View, Float> WIDTH =
new Property<View, Float>(Float.class, "width") {
// dereference of possibly-null reference object.getLayoutParams()
@SuppressWarnings("nullness:dereference.of.nullable")
@Override
public void set(@NonNull View object, @NonNull Float value) {
object.getLayoutParams().width = value.intValue();
object.requestLayout();
}
// dereference of possibly-null reference object.getLayoutParams()
@SuppressWarnings("nullness:dereference.of.nullable")
@NonNull
@Override
public Float get(@NonNull View object) {
return (float) object.getLayoutParams().width;
}
};
/**
* A Property wrapper around the <code>height</code> functionality handled by the {@link
* LayoutParams#height} value.
*/
static final Property<View, Float> HEIGHT =
new Property<View, Float>(Float.class, "height") {
// dereference of possibly-null reference object.getLayoutParams()
@SuppressWarnings("nullness:dereference.of.nullable")
@Override
public void set(@NonNull View object, @NonNull Float value) {
object.getLayoutParams().height = value.intValue();
object.requestLayout();
}
// dereference of possibly-null reference object.getLayoutParams()
@SuppressWarnings("nullness:dereference.of.nullable")
@NonNull
@Override
public Float get(@NonNull View object) {
return (float) object.getLayoutParams().height;
}
};
/**
* A Property wrapper around the <code>paddingStart</code> functionality handled by the {@link
* View#setPaddingRelative(int, int, int, int)}.
*/
static final Property<View, Float> PADDING_START =
new Property<View, Float>(Float.class, "paddingStart") {
@Override
public void set(@NonNull View object, @NonNull Float value) {
object.setPaddingRelative(
value.intValue(),
object.getPaddingTop(),
object.getPaddingEnd(),
object.getPaddingBottom());
}
@NonNull
@Override