This repository has been 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
/
FloatingActionButton.java
executable file
·1320 lines (1123 loc) · 44.6 KB
/
FloatingActionButton.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
package com.github.clans.fab;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.TextView;
public class FloatingActionButton extends ImageButton {
public static final int SIZE_NORMAL = 0;
public static final int SIZE_MINI = 1;
int mFabSize;
boolean mShowShadow;
int mShadowColor;
int mShadowRadius = Util.dpToPx(getContext(), 4f);
int mShadowXOffset = Util.dpToPx(getContext(), 1f);
int mShadowYOffset = Util.dpToPx(getContext(), 3f);
private static final Xfermode PORTER_DUFF_CLEAR = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
private static final long PAUSE_GROWING_TIME = 200;
private static final double BAR_SPIN_CYCLE_TIME = 500;
private static final int BAR_MAX_LENGTH = 270;
private int mColorNormal;
private int mColorPressed;
private int mColorDisabled;
private int mColorRipple;
private Drawable mIcon;
private int mIconSize = Util.dpToPx(getContext(), 24f);
private Animation mShowAnimation;
private Animation mHideAnimation;
private String mLabelText;
private OnClickListener mClickListener;
private Drawable mBackgroundDrawable;
private boolean mUsingElevation;
private boolean mUsingElevationCompat;
// Progress
private boolean mProgressBarEnabled;
private int mProgressWidth = Util.dpToPx(getContext(), 6f);
private int mProgressColor;
private int mProgressBackgroundColor;
private boolean mShouldUpdateButtonPosition;
private float mOriginalX = -1;
private float mOriginalY = -1;
private boolean mButtonPositionSaved;
private RectF mProgressCircleBounds = new RectF();
private Paint mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private boolean mProgressIndeterminate;
private long mLastTimeAnimated;
private float mSpinSpeed = 195.0f; //The amount of degrees per second
private long mPausedTimeWithoutGrowing = 0;
private double mTimeStartGrowing;
private boolean mBarGrowingFromFront = true;
private int mBarLength = 16;
private float mBarExtraLength;
private float mCurrentProgress;
private float mTargetProgress;
private int mProgress;
private boolean mAnimateProgress;
private boolean mShouldProgressIndeterminate;
private boolean mShouldSetProgress;
private int mProgressMax = 100;
private boolean mShowProgressBackground;
public FloatingActionButton(Context context) {
this(context, null);
}
public FloatingActionButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray attr = context.obtainStyledAttributes(attrs, R.styleable.FloatingActionButton, defStyleAttr, 0);
mColorNormal = attr.getColor(R.styleable.FloatingActionButton_fab_colorNormal, 0xFFDA4336);
mColorPressed = attr.getColor(R.styleable.FloatingActionButton_fab_colorPressed, 0xFFE75043);
mColorDisabled = attr.getColor(R.styleable.FloatingActionButton_fab_colorDisabled, 0xFFAAAAAA);
mColorRipple = attr.getColor(R.styleable.FloatingActionButton_fab_colorRipple, 0x99FFFFFF);
mShowShadow = attr.getBoolean(R.styleable.FloatingActionButton_fab_showShadow, true);
mShadowColor = attr.getColor(R.styleable.FloatingActionButton_fab_shadowColor, 0x66000000);
mShadowRadius = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_shadowRadius, mShadowRadius);
mShadowXOffset = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_shadowXOffset, mShadowXOffset);
mShadowYOffset = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_shadowYOffset, mShadowYOffset);
mFabSize = attr.getInt(R.styleable.FloatingActionButton_fab_size, SIZE_NORMAL);
mLabelText = attr.getString(R.styleable.FloatingActionButton_fab_label);
mShouldProgressIndeterminate = attr.getBoolean(R.styleable.FloatingActionButton_fab_progress_indeterminate, false);
mProgressColor = attr.getColor(R.styleable.FloatingActionButton_fab_progress_color, 0xFF009688);
mProgressBackgroundColor = attr.getColor(R.styleable.FloatingActionButton_fab_progress_backgroundColor, 0x4D000000);
mProgressMax = attr.getInt(R.styleable.FloatingActionButton_fab_progress_max, mProgressMax);
mShowProgressBackground = attr.getBoolean(R.styleable.FloatingActionButton_fab_progress_showBackground, true);
if (attr.hasValue(R.styleable.FloatingActionButton_fab_progress)) {
mProgress = attr.getInt(R.styleable.FloatingActionButton_fab_progress, 0);
mShouldSetProgress = true;
}
if (attr.hasValue(R.styleable.FloatingActionButton_fab_elevationCompat)) {
float elevation = attr.getDimensionPixelOffset(R.styleable.FloatingActionButton_fab_elevationCompat, 0);
if (isInEditMode()) {
setElevation(elevation);
} else {
setElevationCompat(elevation);
}
}
initShowAnimation(attr);
initHideAnimation(attr);
attr.recycle();
if (isInEditMode()) {
if (mShouldProgressIndeterminate) {
setIndeterminate(true);
} else if (mShouldSetProgress) {
saveButtonOriginalPosition();
setProgress(mProgress, false);
}
}
// updateBackground();
setClickable(true);
}
private void initShowAnimation(TypedArray attr) {
int resourceId = attr.getResourceId(R.styleable.FloatingActionButton_fab_showAnimation, R.anim.fab_scale_up);
mShowAnimation = AnimationUtils.loadAnimation(getContext(), resourceId);
}
private void initHideAnimation(TypedArray attr) {
int resourceId = attr.getResourceId(R.styleable.FloatingActionButton_fab_hideAnimation, R.anim.fab_scale_down);
mHideAnimation = AnimationUtils.loadAnimation(getContext(), resourceId);
}
private int getCircleSize() {
return getResources().getDimensionPixelSize(mFabSize == SIZE_NORMAL
? R.dimen.fab_size_normal : R.dimen.fab_size_mini);
}
private int calculateMeasuredWidth() {
int width = getCircleSize() + calculateShadowWidth();
if (mProgressBarEnabled) {
width += mProgressWidth * 2;
}
return width;
}
private int calculateMeasuredHeight() {
int height = getCircleSize() + calculateShadowHeight();
if (mProgressBarEnabled) {
height += mProgressWidth * 2;
}
return height;
}
int calculateShadowWidth() {
return hasShadow() ? getShadowX() * 2 : 0;
}
int calculateShadowHeight() {
return hasShadow() ? getShadowY() * 2 : 0;
}
private int getShadowX() {
return mShadowRadius + Math.abs(mShadowXOffset);
}
private int getShadowY() {
return mShadowRadius + Math.abs(mShadowYOffset);
}
private float calculateCenterX() {
return (float) (getMeasuredWidth() / 2);
}
private float calculateCenterY() {
return (float) (getMeasuredHeight() / 2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(calculateMeasuredWidth(), calculateMeasuredHeight());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mProgressBarEnabled) {
if (mShowProgressBackground) {
canvas.drawArc(mProgressCircleBounds, 360, 360, false, mBackgroundPaint);
}
boolean shouldInvalidate = false;
if (mProgressIndeterminate) {
shouldInvalidate = true;
long deltaTime = SystemClock.uptimeMillis() - mLastTimeAnimated;
float deltaNormalized = deltaTime * mSpinSpeed / 1000.0f;
updateProgressLength(deltaTime);
mCurrentProgress += deltaNormalized;
if (mCurrentProgress > 360f) {
mCurrentProgress -= 360f;
}
mLastTimeAnimated = SystemClock.uptimeMillis();
float from = mCurrentProgress - 90;
float to = mBarLength + mBarExtraLength;
if (isInEditMode()) {
from = 0;
to = 135;
}
canvas.drawArc(mProgressCircleBounds, from, to, false, mProgressPaint);
} else {
if (mCurrentProgress != mTargetProgress) {
shouldInvalidate = true;
float deltaTime = (float) (SystemClock.uptimeMillis() - mLastTimeAnimated) / 1000;
float deltaNormalized = deltaTime * mSpinSpeed;
if (mCurrentProgress > mTargetProgress) {
mCurrentProgress = Math.max(mCurrentProgress - deltaNormalized, mTargetProgress);
} else {
mCurrentProgress = Math.min(mCurrentProgress + deltaNormalized, mTargetProgress);
}
mLastTimeAnimated = SystemClock.uptimeMillis();
}
canvas.drawArc(mProgressCircleBounds, -90, mCurrentProgress, false, mProgressPaint);
}
if (shouldInvalidate) {
invalidate();
}
}
}
private void updateProgressLength(long deltaTimeInMillis) {
if (mPausedTimeWithoutGrowing >= PAUSE_GROWING_TIME) {
mTimeStartGrowing += deltaTimeInMillis;
if (mTimeStartGrowing > BAR_SPIN_CYCLE_TIME) {
mTimeStartGrowing -= BAR_SPIN_CYCLE_TIME;
mPausedTimeWithoutGrowing = 0;
mBarGrowingFromFront = !mBarGrowingFromFront;
}
float distance = (float) Math.cos((mTimeStartGrowing / BAR_SPIN_CYCLE_TIME + 1) * Math.PI) / 2 + 0.5f;
float length = BAR_MAX_LENGTH - mBarLength;
if (mBarGrowingFromFront) {
mBarExtraLength = distance * length;
} else {
float newLength = length * (1 - distance);
mCurrentProgress += (mBarExtraLength - newLength);
mBarExtraLength = newLength;
}
} else {
mPausedTimeWithoutGrowing += deltaTimeInMillis;
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
saveButtonOriginalPosition();
if (mShouldProgressIndeterminate) {
setIndeterminate(true);
mShouldProgressIndeterminate = false;
} else if (mShouldSetProgress) {
setProgress(mProgress, mAnimateProgress);
mShouldSetProgress = false;
} else if (mShouldUpdateButtonPosition) {
updateButtonPosition();
mShouldUpdateButtonPosition = false;
}
super.onSizeChanged(w, h, oldw, oldh);
setupProgressBounds();
setupProgressBarPaints();
updateBackground();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
if (params instanceof ViewGroup.MarginLayoutParams && mUsingElevationCompat) {
((ViewGroup.MarginLayoutParams) params).leftMargin += getShadowX();
((ViewGroup.MarginLayoutParams) params).topMargin += getShadowY();
((ViewGroup.MarginLayoutParams) params).rightMargin += getShadowX();
((ViewGroup.MarginLayoutParams) params).bottomMargin += getShadowY();
}
super.setLayoutParams(params);
}
void updateBackground() {
LayerDrawable layerDrawable;
if (hasShadow()) {
layerDrawable = new LayerDrawable(new Drawable[]{
new Shadow(),
createFillDrawable(),
getIconDrawable()
});
} else {
layerDrawable = new LayerDrawable(new Drawable[]{
createFillDrawable(),
getIconDrawable()
});
}
int iconSize = -1;
if (getIconDrawable() != null) {
iconSize = Math.max(getIconDrawable().getIntrinsicWidth(), getIconDrawable().getIntrinsicHeight());
}
int iconOffset = (getCircleSize() - (iconSize > 0 ? iconSize : mIconSize)) / 2;
int circleInsetHorizontal = hasShadow() ? mShadowRadius + Math.abs(mShadowXOffset) : 0;
int circleInsetVertical = hasShadow() ? mShadowRadius + Math.abs(mShadowYOffset) : 0;
if (mProgressBarEnabled) {
circleInsetHorizontal += mProgressWidth;
circleInsetVertical += mProgressWidth;
}
/*layerDrawable.setLayerInset(
mShowShadow ? 1 : 0,
circleInsetHorizontal,
circleInsetVertical,
circleInsetHorizontal,
circleInsetVertical
);*/
layerDrawable.setLayerInset(
hasShadow() ? 2 : 1,
circleInsetHorizontal + iconOffset,
circleInsetVertical + iconOffset,
circleInsetHorizontal + iconOffset,
circleInsetVertical + iconOffset
);
setBackgroundCompat(layerDrawable);
}
protected Drawable getIconDrawable() {
if (mIcon != null) {
return mIcon;
} else {
return new ColorDrawable(Color.TRANSPARENT);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled));
drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(mColorPressed));
drawable.addState(new int[]{}, createCircleDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
private Drawable createCircleDrawable(int color) {
CircleDrawable shapeDrawable = new CircleDrawable(new OvalShape());
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setBackgroundCompat(Drawable drawable) {
if (Util.hasJellyBean()) {
setBackground(drawable);
} else {
setBackgroundDrawable(drawable);
}
}
private void saveButtonOriginalPosition() {
if (!mButtonPositionSaved) {
if (mOriginalX == -1) {
mOriginalX = getX();
}
if (mOriginalY == -1) {
mOriginalY = getY();
}
mButtonPositionSaved = true;
}
}
private void updateButtonPosition() {
float x;
float y;
if (mProgressBarEnabled) {
x = mOriginalX > getX() ? getX() + mProgressWidth : getX() - mProgressWidth;
y = mOriginalY > getY() ? getY() + mProgressWidth : getY() - mProgressWidth;
} else {
x = mOriginalX;
y = mOriginalY;
}
setX(x);
setY(y);
}
private void setupProgressBarPaints() {
mBackgroundPaint.setColor(mProgressBackgroundColor);
mBackgroundPaint.setStyle(Paint.Style.STROKE);
mBackgroundPaint.setStrokeWidth(mProgressWidth);
mProgressPaint.setColor(mProgressColor);
mProgressPaint.setStyle(Paint.Style.STROKE);
mProgressPaint.setStrokeWidth(mProgressWidth);
}
private void setupProgressBounds() {
int circleInsetHorizontal = hasShadow() ? getShadowX() : 0;
int circleInsetVertical = hasShadow() ? getShadowY() : 0;
mProgressCircleBounds = new RectF(
circleInsetHorizontal + mProgressWidth / 2,
circleInsetVertical + mProgressWidth / 2,
calculateMeasuredWidth() - circleInsetHorizontal - mProgressWidth / 2,
calculateMeasuredHeight() - circleInsetVertical - mProgressWidth / 2
);
}
Animation getShowAnimation() {
return mShowAnimation;
}
Animation getHideAnimation() {
return mHideAnimation;
}
void playShowAnimation() {
mHideAnimation.cancel();
startAnimation(mShowAnimation);
}
void playHideAnimation() {
mShowAnimation.cancel();
startAnimation(mHideAnimation);
}
OnClickListener getOnClickListener() {
return mClickListener;
}
Label getLabelView() {
return (Label) getTag(R.id.fab_label);
}
void setColors(int colorNormal, int colorPressed, int colorRipple) {
mColorNormal = colorNormal;
mColorPressed = colorPressed;
mColorRipple = colorRipple;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_enabled});
} else if (Util.hasLollipop()) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled});
ripple.setHotspot(calculateCenterX(), calculateCenterY());
ripple.setVisible(true, true);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mClickListener != null && isEnabled()) {
Label label = (Label) getTag(R.id.fab_label);
if (label == null) return super.onTouchEvent(event);
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
if (label != null) {
label.onActionUp();
}
onActionUp();
break;
case MotionEvent.ACTION_CANCEL:
if (label != null) {
label.onActionUp();
}
onActionUp();
break;
}
mGestureDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
GestureDetector mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
Label label = (Label) getTag(R.id.fab_label);
if (label != null) {
label.onActionDown();
}
onActionDown();
return super.onDown(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Label label = (Label) getTag(R.id.fab_label);
if (label != null) {
label.onActionUp();
}
onActionUp();
return super.onSingleTapUp(e);
}
});
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
ProgressSavedState ss = new ProgressSavedState(superState);
ss.mCurrentProgress = this.mCurrentProgress;
ss.mTargetProgress = this.mTargetProgress;
ss.mSpinSpeed = this.mSpinSpeed;
ss.mProgressWidth = this.mProgressWidth;
ss.mProgressColor = this.mProgressColor;
ss.mProgressBackgroundColor = this.mProgressBackgroundColor;
ss.mShouldProgressIndeterminate = this.mProgressIndeterminate;
ss.mShouldSetProgress = this.mProgressBarEnabled && mProgress > 0 && !this.mProgressIndeterminate;
ss.mProgress = this.mProgress;
ss.mAnimateProgress = this.mAnimateProgress;
ss.mShowProgressBackground = this.mShowProgressBackground;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof ProgressSavedState)) {
super.onRestoreInstanceState(state);
return;
}
ProgressSavedState ss = (ProgressSavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
this.mCurrentProgress = ss.mCurrentProgress;
this.mTargetProgress = ss.mTargetProgress;
this.mSpinSpeed = ss.mSpinSpeed;
this.mProgressWidth = ss.mProgressWidth;
this.mProgressColor = ss.mProgressColor;
this.mProgressBackgroundColor = ss.mProgressBackgroundColor;
this.mShouldProgressIndeterminate = ss.mShouldProgressIndeterminate;
this.mShouldSetProgress = ss.mShouldSetProgress;
this.mProgress = ss.mProgress;
this.mAnimateProgress = ss.mAnimateProgress;
this.mShowProgressBackground = ss.mShowProgressBackground;
this.mLastTimeAnimated = SystemClock.uptimeMillis();
}
private class CircleDrawable extends ShapeDrawable {
private int circleInsetHorizontal;
private int circleInsetVertical;
private CircleDrawable() {
}
private CircleDrawable(Shape s) {
super(s);
circleInsetHorizontal = hasShadow() ? mShadowRadius + Math.abs(mShadowXOffset) : 0;
circleInsetVertical = hasShadow() ? mShadowRadius + Math.abs(mShadowYOffset) : 0;
if (mProgressBarEnabled) {
circleInsetHorizontal += mProgressWidth;
circleInsetVertical += mProgressWidth;
}
}
@Override
public void draw(Canvas canvas) {
setBounds(circleInsetHorizontal, circleInsetVertical, calculateMeasuredWidth()
- circleInsetHorizontal, calculateMeasuredHeight() - circleInsetVertical);
super.draw(canvas);
}
}
private class Shadow extends Drawable {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint mErase = new Paint(Paint.ANTI_ALIAS_FLAG);
private float mRadius;
private Shadow() {
this.init();
}
private void init() {
setLayerType(LAYER_TYPE_SOFTWARE, null);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mColorNormal);
mErase.setXfermode(PORTER_DUFF_CLEAR);
if (!isInEditMode()) {
mPaint.setShadowLayer(mShadowRadius, mShadowXOffset, mShadowYOffset, mShadowColor);
}
mRadius = getCircleSize() / 2;
if (mProgressBarEnabled && mShowProgressBackground) {
mRadius += mProgressWidth;
}
}
@Override
public void draw(Canvas canvas) {
canvas.drawCircle(calculateCenterX(), calculateCenterY(), mRadius, mPaint);
canvas.drawCircle(calculateCenterX(), calculateCenterY(), mRadius, mErase);
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return 0;
}
}
static class ProgressSavedState extends BaseSavedState {
float mCurrentProgress;
float mTargetProgress;
float mSpinSpeed;
int mProgress;
int mProgressWidth;
int mProgressColor;
int mProgressBackgroundColor;
boolean mProgressBarEnabled;
boolean mProgressBarVisibilityChanged;
boolean mProgressIndeterminate;
boolean mShouldProgressIndeterminate;
boolean mShouldSetProgress;
boolean mAnimateProgress;
boolean mShowProgressBackground;
ProgressSavedState(Parcelable superState) {
super(superState);
}
private ProgressSavedState(Parcel in) {
super(in);
this.mCurrentProgress = in.readFloat();
this.mTargetProgress = in.readFloat();
this.mProgressBarEnabled = in.readInt() != 0;
this.mSpinSpeed = in.readFloat();
this.mProgress = in.readInt();
this.mProgressWidth = in.readInt();
this.mProgressColor = in.readInt();
this.mProgressBackgroundColor = in.readInt();
this.mProgressBarVisibilityChanged = in.readInt() != 0;
this.mProgressIndeterminate = in.readInt() != 0;
this.mShouldProgressIndeterminate = in.readInt() != 0;
this.mShouldSetProgress = in.readInt() != 0;
this.mAnimateProgress = in.readInt() != 0;
this.mShowProgressBackground = in.readInt() != 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeFloat(this.mCurrentProgress);
out.writeFloat(this.mTargetProgress);
out.writeInt((mProgressBarEnabled ? 1 : 0));
out.writeFloat(this.mSpinSpeed);
out.writeInt(this.mProgress);
out.writeInt(this.mProgressWidth);
out.writeInt(this.mProgressColor);
out.writeInt(this.mProgressBackgroundColor);
out.writeInt(this.mProgressBarVisibilityChanged ? 1 : 0);
out.writeInt(this.mProgressIndeterminate ? 1 : 0);
out.writeInt(this.mShouldProgressIndeterminate ? 1 : 0);
out.writeInt(this.mShouldSetProgress ? 1 : 0);
out.writeInt(this.mAnimateProgress ? 1 : 0);
out.writeInt(this.mShowProgressBackground ? 1 : 0);
}
public static final Parcelable.Creator<ProgressSavedState> CREATOR =
new Parcelable.Creator<ProgressSavedState>() {
public ProgressSavedState createFromParcel(Parcel in) {
return new ProgressSavedState(in);
}
public ProgressSavedState[] newArray(int size) {
return new ProgressSavedState[size];
}
};
}
/* ===== API methods ===== */
@Override
public void setImageDrawable(Drawable drawable) {
if (mIcon != drawable) {
mIcon = drawable;
updateBackground();
}
}
@Override
public void setImageResource(int resId) {
Drawable drawable = getResources().getDrawable(resId);
if (mIcon != drawable) {
mIcon = drawable;
updateBackground();
}
}
@Override
public void setOnClickListener(final OnClickListener l) {
super.setOnClickListener(l);
mClickListener = l;
View label = (View) getTag(R.id.fab_label);
if (label != null) {
label.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mClickListener != null) {
mClickListener.onClick(FloatingActionButton.this);
}
}
});
}
}
/**
* Sets the size of the <b>FloatingActionButton</b> and invalidates its layout.
*
* @param size size of the <b>FloatingActionButton</b>. Accepted values: SIZE_NORMAL, SIZE_MINI.
*/
public void setButtonSize(int size) {
if (size != SIZE_NORMAL && size != SIZE_MINI) {
throw new IllegalArgumentException("Use @FabSize constants only!");
}
if (mFabSize != size) {
mFabSize = size;
updateBackground();
}
}
public int getButtonSize() {
return mFabSize;
}
public void setColorNormal(int color) {
if (mColorNormal != color) {
mColorNormal = color;
updateBackground();
}
}
public void setColorNormalResId(int colorResId) {
setColorNormal(getResources().getColor(colorResId));
}
public int getColorNormal() {
return mColorNormal;
}
public void setColorPressed(int color) {
if (color != mColorPressed) {
mColorPressed = color;
updateBackground();
}
}
public void setColorPressedResId(int colorResId) {
setColorPressed(getResources().getColor(colorResId));
}
public int getColorPressed() {
return mColorPressed;
}
public void setColorRipple(int color) {
if (color != mColorRipple) {
mColorRipple = color;
updateBackground();
}
}
public void setColorRippleResId(int colorResId) {
setColorRipple(getResources().getColor(colorResId));
}
public int getColorRipple() {
return mColorRipple;
}
public void setColorDisabled(int color) {
if (color != mColorDisabled) {
mColorDisabled = color;
updateBackground();
}
}
public void setColorDisabledResId(int colorResId) {
setColorDisabled(getResources().getColor(colorResId));
}
public int getColorDisabled() {
return mColorDisabled;
}
public void setShowShadow(boolean show) {
if (mShowShadow != show) {
mShowShadow = show;
updateBackground();
}
}
public boolean hasShadow() {
return !mUsingElevation && mShowShadow;
}
/**
* Sets the shadow radius of the <b>FloatingActionButton</b> and invalidates its layout.
*
* @param dimenResId the resource identifier of the dimension
*/
public void setShadowRadius(int dimenResId) {
int shadowRadius = getResources().getDimensionPixelSize(dimenResId);
if (mShadowRadius != shadowRadius) {
mShadowRadius = shadowRadius;
requestLayout();
updateBackground();
}
}
/**
* Sets the shadow radius of the <b>FloatingActionButton</b> and invalidates its layout.
* <p>
* Must be specified in density-independent (dp) pixels, which are then converted into actual
* pixels (px).
*
* @param shadowRadiusDp shadow radius specified in density-independent (dp) pixels
*/
public void setShadowRadius(float shadowRadiusDp) {
mShadowRadius = Util.dpToPx(getContext(), shadowRadiusDp);
requestLayout();
updateBackground();
}
public int getShadowRadius() {
return mShadowRadius;
}
/**
* Sets the shadow x offset of the <b>FloatingActionButton</b> and invalidates its layout.
*
* @param dimenResId the resource identifier of the dimension
*/
public void setShadowXOffset(int dimenResId) {
int shadowXOffset = getResources().getDimensionPixelSize(dimenResId);
if (mShadowXOffset != shadowXOffset) {
mShadowXOffset = shadowXOffset;
requestLayout();
updateBackground();
}
}
/**
* Sets the shadow x offset of the <b>FloatingActionButton</b> and invalidates its layout.
* <p>
* Must be specified in density-independent (dp) pixels, which are then converted into actual
* pixels (px).
*
* @param shadowXOffsetDp shadow radius specified in density-independent (dp) pixels
*/
public void setShadowXOffset(float shadowXOffsetDp) {
mShadowXOffset = Util.dpToPx(getContext(), shadowXOffsetDp);
requestLayout();
updateBackground();
}
public int getShadowXOffset() {
return mShadowXOffset;
}
/**
* Sets the shadow y offset of the <b>FloatingActionButton</b> and invalidates its layout.
*
* @param dimenResId the resource identifier of the dimension
*/
public void setShadowYOffset(int dimenResId) {
int shadowYOffset = getResources().getDimensionPixelSize(dimenResId);
if (mShadowYOffset != shadowYOffset) {
mShadowYOffset = shadowYOffset;
requestLayout();
updateBackground();
}
}
/**
* Sets the shadow y offset of the <b>FloatingActionButton</b> and invalidates its layout.
* <p>
* Must be specified in density-independent (dp) pixels, which are then converted into actual
* pixels (px).
*
* @param shadowYOffsetDp shadow radius specified in density-independent (dp) pixels