|
@@ -4,43 +4,36 @@ import android.animation.Animator;
|
4
|
4
|
import android.animation.AnimatorListenerAdapter;
|
5
|
5
|
import android.support.design.widget.FloatingActionButton;
|
6
|
6
|
import android.view.View;
|
|
7
|
+import android.view.animation.DecelerateInterpolator;
|
7
|
8
|
|
8
|
9
|
import java.util.List;
|
9
|
10
|
|
10
|
11
|
public class FloatingActionButtonAnimator {
|
|
12
|
+ private static final int SHOW_DURATION = 120;
|
|
13
|
+ private static final int HIDE_DURATION = 120;
|
|
14
|
+ private static final float SCALE = 0.6f;
|
|
15
|
+ private static final int ANGLE = 90;
|
|
16
|
+ private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator(1.5f);
|
11
|
17
|
|
12
|
18
|
private final FloatingActionButton collapsedFab;
|
13
|
19
|
private final FloatingActionButton expendedFab;
|
14
|
|
- private int crossFadeAnimationDuration;
|
15
|
20
|
|
16
|
|
- private enum State{Showing, Idle, Removing}
|
17
|
|
- private State state = State.Idle;
|
18
|
|
-
|
19
|
|
- public FloatingActionButtonAnimator(FloatingActionButton collapsedFab, FloatingActionButton expendedFab, int crossFadeAnimationDuration) {
|
|
21
|
+ FloatingActionButtonAnimator(FloatingActionButton collapsedFab, FloatingActionButton expendedFab) {
|
20
|
22
|
this.collapsedFab = collapsedFab;
|
21
|
23
|
this.expendedFab = expendedFab;
|
22
|
|
- this.crossFadeAnimationDuration = crossFadeAnimationDuration;
|
23
|
|
- }
|
24
|
|
-
|
25
|
|
- boolean isAnimating() {
|
26
|
|
- return state == State.Showing || state == State.Removing;
|
27
|
24
|
}
|
28
|
25
|
|
29
|
26
|
void show() {
|
30
|
|
- state = State.Showing;
|
31
|
|
- collapsedFab.setScaleX(0);
|
32
|
|
- collapsedFab.setScaleY(0);
|
|
27
|
+ collapsedFab.setScaleX(SCALE);
|
|
28
|
+ collapsedFab.setScaleY(SCALE);
|
|
29
|
+ collapsedFab.setAlpha(0.0f);
|
|
30
|
+ collapsedFab.setAlpha(0.0f);
|
33
|
31
|
collapsedFab.animate()
|
34
|
32
|
.alpha(1)
|
35
|
33
|
.scaleX(1)
|
36
|
34
|
.scaleY(1)
|
37
|
|
- .setListener(new AnimatorListenerAdapter() {
|
38
|
|
- @Override
|
39
|
|
- public void onAnimationEnd(Animator animation) {
|
40
|
|
- state = State.Idle;
|
41
|
|
- }
|
42
|
|
- })
|
43
|
|
- .setDuration(crossFadeAnimationDuration)
|
|
35
|
+ .setInterpolator(DECELERATE_INTERPOLATOR)
|
|
36
|
+ .setDuration(SHOW_DURATION)
|
44
|
37
|
.start();
|
45
|
38
|
}
|
46
|
39
|
|
|
@@ -50,26 +43,26 @@ public class FloatingActionButtonAnimator {
|
50
|
43
|
}
|
51
|
44
|
|
52
|
45
|
void hideCollapsed() {
|
53
|
|
- animateFab(collapsedFab, 0, 90);
|
|
46
|
+ animateFab(collapsedFab, 0, ANGLE);
|
54
|
47
|
}
|
55
|
48
|
|
56
|
49
|
void showExpended() {
|
57
|
50
|
animateFab(expendedFab, 1, 0);
|
58
|
51
|
}
|
59
|
52
|
|
60
|
|
- void showCollapsed() {
|
|
53
|
+ private void showCollapsed() {
|
61
|
54
|
animateFab(collapsedFab, 1, 0);
|
62
|
55
|
collapsedFab.bringToFront();
|
63
|
56
|
}
|
64
|
57
|
|
65
|
|
- void hideExpended() {
|
66
|
|
- animateFab(expendedFab, 0, -90);
|
|
58
|
+ private void hideExpended() {
|
|
59
|
+ animateFab(expendedFab, 0, -ANGLE);
|
67
|
60
|
}
|
68
|
61
|
|
69
|
62
|
private void animateFab(final FloatingActionButton fab, final int alpha, int rotation) {
|
70
|
63
|
fab.animate()
|
71
|
64
|
.alpha(alpha)
|
72
|
|
- .setDuration(crossFadeAnimationDuration)
|
|
65
|
+ .setDuration(HIDE_DURATION)
|
73
|
66
|
.rotation(rotation)
|
74
|
67
|
.setListener(new AnimatorListenerAdapter() {
|
75
|
68
|
@Override
|
|
@@ -91,12 +84,11 @@ public class FloatingActionButtonAnimator {
|
91
|
84
|
if (fab == null) {
|
92
|
85
|
return;
|
93
|
86
|
}
|
94
|
|
- state = State.Removing;
|
95
|
87
|
fab.animate()
|
96
|
88
|
.alpha(0)
|
97
|
|
- .scaleX(0)
|
98
|
|
- .scaleY(0)
|
99
|
|
- .setDuration(crossFadeAnimationDuration)
|
|
89
|
+ .scaleX(SCALE)
|
|
90
|
+ .scaleY(SCALE)
|
|
91
|
+ .setDuration(HIDE_DURATION)
|
100
|
92
|
.setListener(new AnimatorListenerAdapter() {
|
101
|
93
|
@Override
|
102
|
94
|
public void onAnimationEnd(Animator animation) {
|
|
@@ -114,7 +106,7 @@ public class FloatingActionButtonAnimator {
|
114
|
106
|
.alpha(0)
|
115
|
107
|
.scaleX(0)
|
116
|
108
|
.scaleY(0)
|
117
|
|
- .setDuration(crossFadeAnimationDuration)
|
|
109
|
+ .setDuration(HIDE_DURATION)
|
118
|
110
|
.start();
|
119
|
111
|
}
|
120
|
112
|
}
|