|
@@ -0,0 +1,115 @@
|
|
1
|
+package com.reactnativenavigation.views;
|
|
2
|
+
|
|
3
|
+import android.animation.Animator;
|
|
4
|
+import android.animation.AnimatorListenerAdapter;
|
|
5
|
+import android.graphics.drawable.Drawable;
|
|
6
|
+import android.support.design.widget.CoordinatorLayout;
|
|
7
|
+import android.support.design.widget.FloatingActionButton;
|
|
8
|
+import android.view.Gravity;
|
|
9
|
+import android.view.View;
|
|
10
|
+import android.view.ViewGroup;
|
|
11
|
+
|
|
12
|
+import com.reactnativenavigation.params.FabParams;
|
|
13
|
+import com.reactnativenavigation.utils.ViewUtils;
|
|
14
|
+
|
|
15
|
+public class FloatingActionButtonCoordinator {
|
|
16
|
+
|
|
17
|
+ private CoordinatorLayout parent;
|
|
18
|
+ private FabParams params;
|
|
19
|
+ FloatingActionButton collapsedFab;
|
|
20
|
+ FloatingActionButton expendedFab;
|
|
21
|
+ final int crossFadeAnimationDuration;
|
|
22
|
+
|
|
23
|
+ public FloatingActionButtonCoordinator(CoordinatorLayout parent, FabParams params) {
|
|
24
|
+ this.parent = parent;
|
|
25
|
+ this.params = params;
|
|
26
|
+ crossFadeAnimationDuration = parent.getResources().getInteger(android.R.integer.config_shortAnimTime);
|
|
27
|
+ createCollapsedFab();
|
|
28
|
+ createExpendedFab();
|
|
29
|
+ setStyle();
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ private void createCollapsedFab() {
|
|
33
|
+ collapsedFab = createFab(params.collapsedIcon);
|
|
34
|
+ collapsedFab.setOnClickListener(new View.OnClickListener() {
|
|
35
|
+ @Override
|
|
36
|
+ public void onClick(View v) {
|
|
37
|
+ collapsedFab.animate()
|
|
38
|
+ .alpha(0)
|
|
39
|
+ .setDuration(crossFadeAnimationDuration)
|
|
40
|
+ .rotation(90)
|
|
41
|
+ .setListener(new AnimatorListenerAdapter() {
|
|
42
|
+ @Override
|
|
43
|
+ public void onAnimationEnd(Animator animation) {
|
|
44
|
+ collapsedFab.setVisibility(View.GONE);
|
|
45
|
+ }
|
|
46
|
+ })
|
|
47
|
+ .start();
|
|
48
|
+ expendedFab.animate()
|
|
49
|
+ .alpha(1)
|
|
50
|
+ .setDuration(crossFadeAnimationDuration)
|
|
51
|
+ .rotation(0)
|
|
52
|
+ .setListener(new AnimatorListenerAdapter() {
|
|
53
|
+ @Override
|
|
54
|
+ public void onAnimationStart(Animator animation) {
|
|
55
|
+ expendedFab.setVisibility(View.VISIBLE);
|
|
56
|
+ }
|
|
57
|
+ })
|
|
58
|
+ .start();
|
|
59
|
+ }
|
|
60
|
+ });
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ private void createExpendedFab() {
|
|
64
|
+ expendedFab = createFab(params.expendedIcon);
|
|
65
|
+ expendedFab.setVisibility(View.GONE);
|
|
66
|
+ expendedFab.setRotation(-90);
|
|
67
|
+ expendedFab.setOnClickListener(new View.OnClickListener() {
|
|
68
|
+ @Override
|
|
69
|
+ public void onClick(View v) {
|
|
70
|
+ expendedFab.animate()
|
|
71
|
+ .alpha(0)
|
|
72
|
+ .setDuration(crossFadeAnimationDuration)
|
|
73
|
+ .rotation(-90)
|
|
74
|
+ .setListener(new AnimatorListenerAdapter() {
|
|
75
|
+ @Override
|
|
76
|
+ public void onAnimationEnd(Animator animation) {
|
|
77
|
+ expendedFab.setVisibility(View.GONE);
|
|
78
|
+ }
|
|
79
|
+ })
|
|
80
|
+ .start();
|
|
81
|
+ collapsedFab.animate()
|
|
82
|
+ .alpha(1)
|
|
83
|
+ .setDuration(crossFadeAnimationDuration)
|
|
84
|
+ .rotation(0)
|
|
85
|
+ .setListener(new AnimatorListenerAdapter() {
|
|
86
|
+ @Override
|
|
87
|
+ public void onAnimationStart(Animator animation) {
|
|
88
|
+ collapsedFab.setVisibility(View.VISIBLE);
|
|
89
|
+ }
|
|
90
|
+ })
|
|
91
|
+ .start();
|
|
92
|
+ }
|
|
93
|
+ });
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ private FloatingActionButton createFab(Drawable icon) {
|
|
97
|
+ FloatingActionButton fab = new FloatingActionButton(parent.getContext());
|
|
98
|
+ fab.setImageDrawable(icon);
|
|
99
|
+ parent.addView(fab, createFabLayoutParams());
|
|
100
|
+ return fab;
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ private CoordinatorLayout.LayoutParams createFabLayoutParams() {
|
|
104
|
+ final CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
105
|
+ lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
|
|
106
|
+ final int margin = (int) ViewUtils.convertDpToPixel(16);
|
|
107
|
+ lp.bottomMargin = margin;
|
|
108
|
+ lp.rightMargin = margin;
|
|
109
|
+ return lp;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ private void setStyle() {
|
|
113
|
+
|
|
114
|
+ }
|
|
115
|
+}
|