-
Notifications
You must be signed in to change notification settings - Fork 667
/
Style.java
564 lines (490 loc) · 17.1 KB
/
Style.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
/*
* Copyright 2012 - 2014 Benjamin Weiss
*
* 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 de.keyboardsurfer.android.widget.crouton;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
/** The style for a {@link Crouton}. */
public class Style {
public static final int NOT_SET = -1;
public static final int holoRedLight = 0xffff4444;
public static final int holoGreenLight = 0xff99cc00;
public static final int holoBlueLight = 0xff33b5e5;
/** Default style for alerting the user. */
public static final Style ALERT;
/** Default style for confirming an action. */
public static final Style CONFIRM;
/** Default style for general information. */
public static final Style INFO;
static {
ALERT = new Builder()
.setBackgroundColorValue(holoRedLight)
.build();
CONFIRM = new Builder()
.setBackgroundColorValue(holoGreenLight)
.build();
INFO = new Builder()
.setBackgroundColorValue(holoBlueLight)
.build();
}
/**
* The {@link Configuration} for this {@link Style}.
* It can be overridden via {@link Crouton#setConfiguration(Configuration)}.
*/
final Configuration configuration;
/**
* The resource id of the backgroundResourceId.
* <p/>
* 0 for no backgroundResourceId.
*/
final int backgroundColorResourceId;
/**
* The resource id of the backgroundDrawableResourceId.
* <p/>
* 0 for no backgroundDrawableResourceId.
*/
final int backgroundDrawableResourceId;
/**
* The backgroundColorResourceValue's e.g. 0xffff4444;
* <p/>
* NOT_SET for no value.
*/
final int backgroundColorValue;
/** Whether we should isTileEnabled the backgroundResourceId or not. */
final boolean isTileEnabled;
/**
* The text colorResourceId's resource id.
* <p/>
* 0 sets the text colorResourceId to the system theme default.
*/
final int textColorResourceId;
/**
* The textColorResourceValue's e.g. 0xffff4444;
* <p/>
* NOT_SET for no value.
*/
final int textColorValue;
/** The height of the {@link Crouton} in pixels. */
final int heightInPixels;
/** Resource ID for the height of the {@link Crouton}. */
final int heightDimensionResId;
/** The width of the {@link Crouton} in pixels. */
final int widthInPixels;
/** Resource ID for the width of the {@link Crouton}. */
final int widthDimensionResId;
/** The text's gravity as provided by {@link Gravity}. */
final int gravity;
/** An additional image to display in the {@link Crouton}. */
final Drawable imageDrawable;
/** An additional image to display in the {@link Crouton}. */
final int imageResId;
/** The {@link ImageView.ScaleType} for the image to display in the {@link Crouton}. */
final ImageView.ScaleType imageScaleType;
/**
* The text size in sp
* <p/>
* 0 sets the text size to the system theme default
*/
final int textSize;
/** The text shadow color's resource id */
final int textShadowColorResId;
/** The text shadow radius */
final float textShadowRadius;
/** The text shadow vertical offset */
final float textShadowDy;
/** The text shadow horizontal offset */
final float textShadowDx;
/** The text appearance resource id for the text. */
final int textAppearanceResId;
/** The padding for the crouton view content in pixels */
final int paddingInPixels;
/** The resource id for the padding for the view content */
final int paddingDimensionResId;
/** The file path and font name for the view content */
final String fontName;
/** The file path and font name resource id for the view content */
final int fontNameResId;
private Style(final Builder builder) {
this.configuration = builder.configuration;
this.backgroundColorResourceId = builder.backgroundColorResourceId;
this.backgroundDrawableResourceId = builder.backgroundDrawableResourceId;
this.isTileEnabled = builder.isTileEnabled;
this.textColorResourceId = builder.textColorResourceId;
this.textColorValue = builder.textColorValue;
this.heightInPixels = builder.heightInPixels;
this.heightDimensionResId = builder.heightDimensionResId;
this.widthInPixels = builder.widthInPixels;
this.widthDimensionResId = builder.widthDimensionResId;
this.gravity = builder.gravity;
this.imageDrawable = builder.imageDrawable;
this.textSize = builder.textSize;
this.textShadowColorResId = builder.textShadowColorResId;
this.textShadowRadius = builder.textShadowRadius;
this.textShadowDx = builder.textShadowDx;
this.textShadowDy = builder.textShadowDy;
this.textAppearanceResId = builder.textAppearanceResId;
this.imageResId = builder.imageResId;
this.imageScaleType = builder.imageScaleType;
this.paddingInPixels = builder.paddingInPixels;
this.paddingDimensionResId = builder.paddingDimensionResId;
this.backgroundColorValue = builder.backgroundColorValue;
this.fontName = builder.fontName;
this.fontNameResId = builder.fontNameResId;
}
/** Builder for the {@link Style} object. */
public static class Builder {
private Configuration configuration;
private int backgroundColorValue;
private int backgroundColorResourceId;
private int backgroundDrawableResourceId;
private boolean isTileEnabled;
private int textColorResourceId;
private int textColorValue;
private int heightInPixels;
private int heightDimensionResId;
private int widthInPixels;
private int widthDimensionResId;
private int gravity;
private Drawable imageDrawable;
private int textSize;
private int textShadowColorResId;
private float textShadowRadius;
private float textShadowDx;
private float textShadowDy;
private int textAppearanceResId;
private int imageResId;
private ImageView.ScaleType imageScaleType;
private int paddingInPixels;
private int paddingDimensionResId;
private String fontName;
private int fontNameResId;
/** Creates a {@link Builder} to build a {@link Style} upon. */
public Builder() {
configuration = Configuration.DEFAULT;
paddingInPixels = 10;
backgroundColorResourceId = android.R.color.holo_blue_light;
backgroundDrawableResourceId = 0;
backgroundColorValue = NOT_SET;
isTileEnabled = false;
textColorResourceId = android.R.color.white;
textColorValue = NOT_SET;
heightInPixels = LayoutParams.WRAP_CONTENT;
widthInPixels = LayoutParams.MATCH_PARENT;
gravity = Gravity.CENTER;
imageDrawable = null;
imageResId = 0;
imageScaleType = ImageView.ScaleType.FIT_XY;
fontName = null;
fontNameResId = 0;
}
/**
* Creates a {@link Builder} to build a {@link Style} upon.
*
* @param baseStyle
* The base {@link Style} to use for this {@link Style}.
*/
public Builder(final Style baseStyle) {
configuration = baseStyle.configuration;
backgroundColorValue = baseStyle.backgroundColorValue;
backgroundColorResourceId = baseStyle.backgroundColorResourceId;
backgroundDrawableResourceId = baseStyle.backgroundDrawableResourceId;
isTileEnabled = baseStyle.isTileEnabled;
textColorResourceId = baseStyle.textColorResourceId;
textColorValue = baseStyle.textColorValue;
heightInPixels = baseStyle.heightInPixels;
heightDimensionResId = baseStyle.heightDimensionResId;
widthInPixels = baseStyle.widthInPixels;
widthDimensionResId = baseStyle.widthDimensionResId;
gravity = baseStyle.gravity;
imageDrawable = baseStyle.imageDrawable;
textSize = baseStyle.textSize;
textShadowColorResId = baseStyle.textShadowColorResId;
textShadowRadius = baseStyle.textShadowRadius;
textShadowDx = baseStyle.textShadowDx;
textShadowDy = baseStyle.textShadowDy;
textAppearanceResId = baseStyle.textAppearanceResId;
imageResId = baseStyle.imageResId;
imageScaleType = baseStyle.imageScaleType;
paddingInPixels = baseStyle.paddingInPixels;
paddingDimensionResId = baseStyle.paddingDimensionResId;
fontName = baseStyle.fontName;
fontNameResId = baseStyle.fontNameResId;
}
/**
* Set the {@link Configuration} option of the {@link Crouton}.
*
* @param configuration
* The {@link Configuration}.
*
* @return the {@link Builder}.
*/
public Builder setConfiguration(Configuration configuration) {
this.configuration = configuration;
return this;
}
/**
* Set the backgroundColorResourceId option of the {@link Crouton}.
*
* @param backgroundColorResourceId
* The backgroundColorResourceId's resource id.
*
* @return the {@link Builder}.
*/
public Builder setBackgroundColor(int backgroundColorResourceId) {
this.backgroundColorResourceId = backgroundColorResourceId;
return this;
}
/**
* Set the backgroundColorResourceValue option of the {@link Crouton}.
*
* @param backgroundColorValue
* The backgroundColorResourceValue's e.g. 0xffff4444;
*
* @return the {@link Builder}.
*/
public Builder setBackgroundColorValue(int backgroundColorValue) {
this.backgroundColorValue = backgroundColorValue;
return this;
}
/**
* Set the backgroundDrawableResourceId option for the {@link Crouton}.
*
* @param backgroundDrawableResourceId
* Resource ID of a backgroundDrawableResourceId image drawable.
*
* @return the {@link Builder}.
*/
public Builder setBackgroundDrawable(int backgroundDrawableResourceId) {
this.backgroundDrawableResourceId = backgroundDrawableResourceId;
return this;
}
/**
* Set the heightInPixels option for the {@link Crouton}.
*
* @param height
* The height of the {@link Crouton} in pixel. Can also be
* {@link LayoutParams#MATCH_PARENT} or
* {@link LayoutParams#WRAP_CONTENT}.
*
* @return the {@link Builder}.
*/
public Builder setHeight(int height) {
this.heightInPixels = height;
return this;
}
/**
* Set the resource id for the height option for the {@link Crouton}.
*
* @param heightDimensionResId
* Resource ID of a dimension for the height of the {@link Crouton}.
*
* @return the {@link Builder}.
*/
public Builder setHeightDimensionResId(int heightDimensionResId) {
this.heightDimensionResId = heightDimensionResId;
return this;
}
/**
* Set the widthInPixels option for the {@link Crouton}.
*
* @param width
* The width of the {@link Crouton} in pixel. Can also be
* {@link LayoutParams#MATCH_PARENT} or
* {@link LayoutParams#WRAP_CONTENT}.
*
* @return the {@link Builder}.
*/
public Builder setWidth(int width) {
this.widthInPixels = width;
return this;
}
/**
* Set the resource id for the width option for the {@link Crouton}.
*
* @param widthDimensionResId
* Resource ID of a dimension for the width of the {@link Crouton}.
*
* @return the {@link Builder}.
*/
public Builder setWidthDimensionResId(int widthDimensionResId) {
this.widthDimensionResId = widthDimensionResId;
return this;
}
/**
* Set the isTileEnabled option for the {@link Crouton}.
*
* @param isTileEnabled
* <code>true</code> if you want the backgroundResourceId to be
* tiled, else <code>false</code>.
*
* @return the {@link Builder}.
*/
public Builder setTileEnabled(boolean isTileEnabled) {
this.isTileEnabled = isTileEnabled;
return this;
}
/**
* Set the textColorResourceId option for the {@link Crouton}.
*
* @param textColor
* The resource id of the text colorResourceId.
*
* @return the {@link Builder}.
*/
public Builder setTextColor(int textColor) {
this.textColorResourceId = textColor;
return this;
}
/**
* Set the textColorResourceValue option of the {@link Crouton}.
*
* @param textColorValue
* The textColorResourceValue's e.g. 0xffff4444;
*
* @return the {@link Builder}.
*/
public Builder setTextColorValue(int textColorValue) {
this.textColorValue = textColorValue;
return this;
}
/**
* Set the gravity option for the {@link Crouton}.
*
* @param gravity
* The text's gravity as provided by {@link Gravity}.
*
* @return the {@link Builder}.
*/
public Builder setGravity(int gravity) {
this.gravity = gravity;
return this;
}
/**
* Set the image option for the {@link Crouton}.
*
* @param imageDrawable
* An additional image to display in the {@link Crouton}.
*
* @return the {@link Builder}.
*/
public Builder setImageDrawable(Drawable imageDrawable) {
this.imageDrawable = imageDrawable;
return this;
}
/**
* Set the image resource option for the {@link Crouton}.
*
* @param imageResId
* An additional image to display in the {@link Crouton}.
*
* @return the {@link Builder}.
*/
public Builder setImageResource(int imageResId) {
this.imageResId = imageResId;
return this;
}
/** The text size in sp. */
public Builder setTextSize(int textSize) {
this.textSize = textSize;
return this;
}
/** The text shadow color resource id. */
public Builder setTextShadowColor(int textShadowColorResId) {
this.textShadowColorResId = textShadowColorResId;
return this;
}
/** The text shadow radius. */
public Builder setTextShadowRadius(float textShadowRadius) {
this.textShadowRadius = textShadowRadius;
return this;
}
/** The text shadow horizontal offset. */
public Builder setTextShadowDx(float textShadowDx) {
this.textShadowDx = textShadowDx;
return this;
}
/** The text shadow vertical offset. */
public Builder setTextShadowDy(float textShadowDy) {
this.textShadowDy = textShadowDy;
return this;
}
/** The text appearance resource id for the text. */
public Builder setTextAppearance(int textAppearanceResId) {
this.textAppearanceResId = textAppearanceResId;
return this;
}
/** The {@link android.widget.ImageView.ScaleType} for the image. */
public Builder setImageScaleType(ImageView.ScaleType imageScaleType) {
this.imageScaleType = imageScaleType;
return this;
}
/** The padding for the crouton view's content in pixels. */
public Builder setPaddingInPixels(int padding) {
this.paddingInPixels = padding;
return this;
}
/** The resource id for the padding for the crouton view's content. */
public Builder setPaddingDimensionResId(int paddingResId) {
this.paddingDimensionResId = paddingResId;
return this;
}
/** The file path and name of the font for the crouton view's content. */
public Builder setFontName(String fontName) {
this.fontName = fontName;
return this;
}
/** The resource id for the file path and name of the font for the crouton view's content. */
public Builder setFontNameResId(int fontNameResId) {
this.fontNameResId = fontNameResId;
return this;
}
/** @return a configured {@link Style} object. */
public Style build() {
return new Style(this);
}
}
@Override
public String toString() {
return "Style{" +
"configuration=" + configuration +
", backgroundColorResourceId=" + backgroundColorResourceId +
", backgroundDrawableResourceId=" + backgroundDrawableResourceId +
", backgroundColorValue=" + backgroundColorValue +
", isTileEnabled=" + isTileEnabled +
", textColorResourceId=" + textColorResourceId +
", textColorValue=" + textColorValue +
", heightInPixels=" + heightInPixels +
", heightDimensionResId=" + heightDimensionResId +
", widthInPixels=" + widthInPixels +
", widthDimensionResId=" + widthDimensionResId +
", gravity=" + gravity +
", imageDrawable=" + imageDrawable +
", imageResId=" + imageResId +
", imageScaleType=" + imageScaleType +
", textSize=" + textSize +
", textShadowColorResId=" + textShadowColorResId +
", textShadowRadius=" + textShadowRadius +
", textShadowDy=" + textShadowDy +
", textShadowDx=" + textShadowDx +
", textAppearanceResId=" + textAppearanceResId +
", paddingInPixels=" + paddingInPixels +
", paddingDimensionResId=" + paddingDimensionResId +
", fontName=" + fontName +
", fontNameResId=" + fontNameResId +
'}';
}
}