This repository was archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathFloatingActionButtonBuilder.java
More file actions
216 lines (200 loc) · 6.71 KB
/
FloatingActionButtonBuilder.java
File metadata and controls
216 lines (200 loc) · 6.71 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
package com.github.clans.fab;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
/**
* The <code>FloatingActionButtonBuilder</code> helps you construct easily a
* {@link #FloatingActionButton} directly from your activity (no xml resource
* invocation).<br>
* <br>
* Example:
* <br>
* <br>
* <b><code>FloatingActionButtonBuilder builder = new FloatingActionButtonBuilder(this);</code></b><br>
* <b><code>builder.setSize(56);</code></b><br>
* <b><code>builder.setColorNormal(colorNormal);</code></b><br>
* <b><code>builder.setColorPressed(colorPressed);</code></b><br>
* <b><code>builder.setColorRipple(colorRipple);</code></b><br>
* <b><code>builder.setDrawable(getResources().getDrawable(R.drawable.ic_add_white_24dp));</code></b><br>
* <b><code>builder.setElevation(4);</code></b><br>
* <b><code>builder.setMargins(0, 0, bottomEndMargins, bottomEndMargins);</code></b><br>
* <b><code>FloatingActionButton fab = builder.build();</code></b><br>
* <b><code>fab.setOnClickListener(this);</code></b><br>
*
* @author Erkan Molla
* @version 1.0.0
*/
public class FloatingActionButtonBuilder {
private final Activity mActivity;
private final DisplayMetrics mDisplayMetrics;
private LayoutParams mLayoutParams = null;
private int mGravity = Gravity.BOTTOM | Gravity.END;
private Drawable mDrawable = null;
private int mColorNormal = 0;
private int mColorPressed = 0;
private int mColorRipple = 0;
private float mElevation = 0;
/**
* Initialize the builder class with your activity as parameter.
*
* @param context The activity that is calling this constructor
*/
public FloatingActionButtonBuilder(final Activity context) {
mActivity = context;
mDisplayMetrics = context.getResources().getDisplayMetrics();
}
/**
* Sets the size in dp.
*
* @param dp density-independent pixel
* @return this
* @see <a
* href="http://developer.android.com/guide/practices/screens_support.html">Supporting
* Multiple Screens</a>
*/
public FloatingActionButtonBuilder setSize(final int dp) {
int px = (int) toPixel(dp);
mLayoutParams = new FrameLayout.LayoutParams(px, px);
return this;
}
/**
* Sets the gravity.
*
* @param gravity to be set
* @return this
* @see <a
* href="http://developer.android.com/reference/android/view/Gravity.html">Gravity</a>
*/
public FloatingActionButtonBuilder setGravity(final int gravity) {
mGravity = gravity;
return this;
}
/**
* Sets the margins in dp.
*
* @param left the left margin size
* @param top the top margin size
* @param right the right margin size
* @param bottom the bottom margin size
* @return this
*/
public FloatingActionButtonBuilder setMargins(final int left, final int top, final int right,
final int bottom) {
int leftPx = (int) toPixel(left);
int topPx = (int) toPixel(top);
int rightPx = (int) toPixel(right);
int bottomPx = (int) toPixel(bottom);
mLayoutParams.setMargins(leftPx, topPx, rightPx, bottomPx);
return this;
}
/**
* Sets the drawable.
*
* @param drawable to be set
* @return this
*/
public FloatingActionButtonBuilder setDrawable(final Drawable drawable) {
mDrawable = drawable;
return this;
}
/**
* Sets the normal, pressed and ripple colors.
*
* @param colorNormal normal color
* @param colorPressed pressed color
* @param colorRipple ripple color
* @return this
*/
public FloatingActionButtonBuilder setColors(final int colorNormal, final int colorPressed,
final int colorRipple) {
mColorNormal = colorNormal;
mColorPressed = colorPressed;
mColorRipple = colorRipple;
return this;
}
/**
* Sets the normal color.
*
* @param colorNormal normal color
* @return this
*/
public FloatingActionButtonBuilder setColorNormal(final int colorNormal) {
mColorNormal = colorNormal;
return this;
}
/**
* Sets the pressed color.
*
* @param colorPressed pressed color
* @return this
*/
public FloatingActionButtonBuilder setColorPressed(final int colorPressed) {
mColorPressed = colorPressed;
return this;
}
/**
* Sets the ripple color.
*
* @param colorRipple ripple color
* @return this
*/
public FloatingActionButtonBuilder setColorRipple(final int colorRipple) {
mColorRipple = colorRipple;
return this;
}
/**
* Sets the elevation in dp.
*
* @param dp elevation to be applied
* @return this
*/
public FloatingActionButtonBuilder setElevation(final int dp) {
mElevation = toPixel(dp);
return this;
}
/**
* Builds the <code>FloatingActionButton</code> with all specified
* parameters.
*
* @return a <code>FloatingActionButton</code> as requested
*/
public FloatingActionButton build() {
final FloatingActionButton fab = new FloatingActionButton(mActivity);
fab.setButtonSize(FloatingActionButton.SIZE_NORMAL);
fab.setImageDrawable(mDrawable);
fab.setColors(mColorNormal, mColorPressed, mColorRipple);
fab.setElevationCompat(mElevation);
mLayoutParams.gravity = mGravity;
ViewGroup root = (ViewGroup) mActivity.findViewById(android.R.id.content);
root.addView(fab, mLayoutParams);
return fab;
}
/**
* The calculation (dp * scale + 0.5f) is a widely used to convert dp to
* pixel units based on density scale.
*
* @param dp density-independent pixel
* @param scale the logical density of the display
* @return the pixel value
* @see <a href=
* "http://developer.android.com/guide/practices/screens_support.html#dips-pels"
* >Converting dp units to pixel units</a>
*/
public int toPixel(int dp, float scale) {
return (int) (dp * scale + 0.5f);
}
/**
* Converts the given dp value to pixels.
*
* @param dp density-independent pixel
* @return pixels of the given dp
*/
public float toPixel(final int dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mDisplayMetrics);
}
}