30
30
31
31
import androidx .annotation .ColorInt ;
32
32
import androidx .annotation .DrawableRes ;
33
+ import androidx .annotation .IntDef ;
33
34
import androidx .annotation .Keep ;
34
35
import androidx .annotation .NonNull ;
35
36
import androidx .annotation .Nullable ;
@@ -49,6 +50,7 @@ public class DropdownView extends LinearLayout {
49
50
private int arrowTint , arrowTintExpanded ;
50
51
private int titleHeight , contentHeight , dividerHeight ;
51
52
private boolean useDivider , expanded , isMoving ;
53
+ @ RotationModel
52
54
private int arrowRotation ;
53
55
54
56
public DropdownView (@ NonNull Context context ) {
@@ -64,27 +66,27 @@ public DropdownView(@NonNull Context context, @Nullable AttributeSet attrs, int
64
66
setOrientation (VERTICAL );
65
67
TypedArray typed = context .obtainStyledAttributes (attrs , R .styleable .DropdownView , defStyleAttr , 0 );
66
68
elevation = typed .getDimension (R .styleable .DropdownView_elevation , getElevation ());
67
- cardCornerRadius = typed .getDimension (R .styleable .DropdownView_cardCornerRadius , px2dpf (10 ));
69
+ cardCornerRadius = typed .getDimension (R .styleable .DropdownView_cardCornerRadius , dpf (10 ));
68
70
cardBackground = getMultiColourAttr (getContext (), typed , R .styleable .DropdownView_cardBackground , null );
69
71
cardBackgroundExpanded = getMultiColourAttr (getContext (), typed , R .styleable .DropdownView_cardBackgroundExpanded , cardBackground );
70
72
int resourceId = typed .getResourceId (R .styleable .DropdownView_arrow , R .drawable .ic_round_arrow_right_24 );
71
73
Drawable arrowIcon = ResourcesCompat .getDrawable (context .getResources (), resourceId , null );
72
74
arrowTint = typed .getColor (R .styleable .DropdownView_arrowTint , Color .WHITE );
73
75
arrowTintExpanded = typed .getColor (R .styleable .DropdownView_arrowTintExpanded , arrowTint );
74
- arrowRotation = typed .getInteger (R .styleable .DropdownView_arrowRotation , RotationModel . QUARTER );
76
+ arrowRotation = typed .getInteger (R .styleable .DropdownView_arrowRotation , QUARTER );
75
77
useDivider = typed .getBoolean (R .styleable .DropdownView_useDivider , false );
76
- dividerHeight = (int ) typed .getDimension (R .styleable .DropdownView_dividerHeight , px2dpf (1 ));
78
+ dividerHeight = (int ) typed .getDimension (R .styleable .DropdownView_dividerHeight , dpf (1 ));
77
79
int dividerColor = typed .getColor (R .styleable .DropdownView_dividerColor , 0xffe2e2e2 );
78
80
titleBackgroundExpanded = getMultiColourAttr (getContext (), typed , R .styleable .DropdownView_titleBackgroundExpanded , null , true );
79
81
String titleText = typed .getString (R .styleable .DropdownView_titleText );
80
82
titleTextColor = typed .getColor (R .styleable .DropdownView_titleTextColor , Color .WHITE );
81
83
titleTextColorExpanded = typed .getColor (R .styleable .DropdownView_titleTextColorExpanded , titleTextColor );
82
- float titleTextSize = typed .getDimension (R .styleable .DropdownView_titleTextSize , px2dpf (17 ));
84
+ float titleTextSize = typed .getDimension (R .styleable .DropdownView_titleTextSize , dpf (17 ));
83
85
String titleTextFont = typed .getString (R .styleable .DropdownView_titleTextFont );
84
86
boolean titleTextBold = typed .getBoolean (R .styleable .DropdownView_titleTextBold , true );
85
87
String contentText = typed .getString (R .styleable .DropdownView_contentText );
86
88
contentTextColor = typed .getColor (R .styleable .DropdownView_contentTextColor , Color .WHITE );
87
- float contentTextSize = typed .getDimension (R .styleable .DropdownView_contentTextSize , px2dpf (17 ));
89
+ float contentTextSize = typed .getDimension (R .styleable .DropdownView_contentTextSize , dpf (17 ));
88
90
String contentTextFont = typed .getString (R .styleable .DropdownView_contentTextFont );
89
91
boolean contentTextBold = typed .getBoolean (R .styleable .DropdownView_contentTextBold , false );
90
92
expanded = typed .getBoolean (R .styleable .DropdownView_expanded , false );
@@ -145,15 +147,15 @@ public void setExpanded(boolean expanded, boolean animate) {
145
147
146
148
@ Override
147
149
public void setElevation (float elevation ) {
148
- this .elevation = px2dpf (elevation );
150
+ this .elevation = dpf (elevation );
149
151
super .setElevation (elevation );
150
152
}
151
153
152
- public void setCardCornerRadius (float cardCornerRadius ) {
153
- if (this .cardCornerRadius == cardCornerRadius ) {
154
+ public void setCardCornerRadius (float radius ) {
155
+ if (this .cardCornerRadius == radius ) {
154
156
return ;
155
157
}
156
- this .cardCornerRadius = cardCornerRadius ;
158
+ this .cardCornerRadius = radius ;
157
159
if (cardBackground instanceof PaintDrawable ) {
158
160
PaintDrawableSetCornerRadius ((PaintDrawable ) cardBackground , true );
159
161
}
@@ -167,43 +169,46 @@ public void setCardCornerRadius(float cardCornerRadius) {
167
169
titleLinearLayout .setBackground (expanded ? null : titleBackgroundExpanded );
168
170
}
169
171
172
+ public void setTitleBackgroundColor (@ ColorInt int color ) {
173
+ setCardBackgroundColor (color );
174
+ }
175
+
170
176
public void setCardBackgroundColor (@ ColorInt int color ) {
171
177
setCardBackground (createColorDrawable (color , true ));
172
178
}
173
179
174
- public void setCardBackground (Drawable cardBackground ) {
180
+ public void setCardBackground (Drawable drawable ) {
175
181
if (this .cardBackground == cardBackgroundExpanded ) {
176
- cardBackgroundExpanded = cardBackground ;
182
+ cardBackgroundExpanded = drawable ;
177
183
if (expanded ) {
178
- setBackground (cardBackground );
184
+ setBackground (drawable );
179
185
}
180
186
}
181
- this .cardBackground = cardBackground ;
187
+ this .cardBackground = drawable ;
182
188
if (!expanded ) {
183
- setBackground (cardBackground );
189
+ setBackground (drawable );
184
190
}
185
191
}
186
192
187
193
public void setCardBackgroundColorExpanded (@ ColorInt int color ) {
188
194
setCardBackgroundExpanded (createColorDrawable (color , true ));
189
195
}
190
196
191
- public void setCardBackgroundExpanded (Drawable cardBackgroundExpanded ) {
192
- this .cardBackgroundExpanded = cardBackgroundExpanded ;
197
+ public void setCardBackgroundExpanded (Drawable drawable ) {
198
+ this .cardBackgroundExpanded = drawable ;
193
199
if (expanded ) {
194
- setBackground (cardBackgroundExpanded );
200
+ setBackground (drawable );
195
201
}
196
202
}
197
203
198
-
199
204
public void setTitleBackgroundColorExpanded (@ ColorInt int color ) {
200
205
setTitleBackgroundExpanded (createColorDrawable (color , false ));
201
206
}
202
207
203
- public void setTitleBackgroundExpanded (Drawable titleBackgroundExpanded ) {
204
- this .titleBackgroundExpanded = titleBackgroundExpanded ;
208
+ public void setTitleBackgroundExpanded (Drawable drawable ) {
209
+ this .titleBackgroundExpanded = drawable ;
205
210
if (expanded ) {
206
- titleLinearLayout .setBackground (titleBackgroundExpanded );
211
+ titleLinearLayout .setBackground (drawable );
207
212
}
208
213
}
209
214
@@ -215,35 +220,35 @@ public void setArrow(Drawable drawable) {
215
220
arrow .setImageDrawable (drawable );
216
221
}
217
222
218
- public void setArrowTint (int arrowTint ) {
219
- if (this .arrowTint == arrowTint ) {
223
+ public void setArrowTint (@ ColorInt int color ) {
224
+ if (this .arrowTint == color ) {
220
225
return ;
221
226
}
222
227
if (this .arrowTint == arrowTintExpanded ) {
223
- arrowTintExpanded = arrowTint ;
228
+ arrowTintExpanded = color ;
224
229
if (expanded ) {
225
- if (Color .TRANSPARENT == arrowTint ) {
230
+ if (Color .TRANSPARENT == color ) {
226
231
arrow .clearColorFilter ();
227
232
} else {
228
- arrow .setColorFilter (arrowTint );
233
+ arrow .setColorFilter (color );
229
234
}
230
235
}
231
236
}
232
- this .arrowTint = arrowTint ;
233
- if (Color .TRANSPARENT == arrowTint ) {
237
+ this .arrowTint = color ;
238
+ if (Color .TRANSPARENT == color ) {
234
239
arrow .clearColorFilter ();
235
240
} else if (!expanded ) {
236
- arrow .setColorFilter (arrowTint );
241
+ arrow .setColorFilter (color );
237
242
}
238
243
}
239
244
240
- public void setArrowTintExpanded (int arrowTintExpanded ) {
241
- if (this .arrowTintExpanded == arrowTintExpanded ) {
245
+ public void setArrowTintExpanded (@ ColorInt int color ) {
246
+ if (this .arrowTintExpanded == color ) {
242
247
return ;
243
248
}
244
- this .arrowTintExpanded = arrowTintExpanded ;
249
+ this .arrowTintExpanded = color ;
245
250
if (expanded ) {
246
- arrow .setColorFilter (arrowTintExpanded );
251
+ arrow .setColorFilter (color );
247
252
}
248
253
}
249
254
@@ -268,39 +273,39 @@ public void setUseDivider(boolean useDivider) {
268
273
divider .setVisibility (useDivider ? VISIBLE : GONE );
269
274
}
270
275
271
- public void setDividerHeight (int pxHeight ) {
272
- int height = (int ) px2dpf ( pxHeight );
276
+ public void setDividerHeight (int height ) {
277
+ height = (int ) dpf ( height );
273
278
if (dividerHeight == height ) {
274
279
return ;
275
280
}
276
281
dividerHeight = height ;
277
282
divider .setLayoutParams (createLinear (LayoutParams .MATCH_PARENT , dividerHeight ));
278
283
}
279
284
280
- public void setDividerColor (int dividerColor ) {
281
- divider .setBackgroundColor (dividerColor );
285
+ public void setDividerColor (@ ColorInt int color ) {
286
+ divider .setBackgroundColor (color );
282
287
}
283
288
284
289
public void setTitleText (String title ) {
285
290
this .title .setText (title );
286
291
}
287
292
288
- public void setTitleTextColor (int titleTextColor ) {
289
- this .titleTextColor = titleTextColor ;
293
+ public void setTitleTextColor (@ ColorInt int color ) {
294
+ this .titleTextColor = color ;
290
295
if (!expanded ) {
291
- title .setTextColor (titleTextColor );
296
+ title .setTextColor (color );
292
297
}
293
298
}
294
299
295
- public void setTitleTextColorExpanded (int titleTextColorExpanded ) {
296
- this .titleTextColorExpanded = titleTextColorExpanded ;
300
+ public void setTitleTextColorExpanded (@ ColorInt int color ) {
301
+ this .titleTextColorExpanded = color ;
297
302
if (expanded ) {
298
- title .setTextColor (titleTextColorExpanded );
303
+ title .setTextColor (color );
299
304
}
300
305
}
301
306
302
- public void setTitleTextSize (int titleTextColor ) {
303
- title .setTextSize (TypedValue .COMPLEX_UNIT_DIP , titleTextColor );
307
+ public void setTitleTextSize (int size ) {
308
+ title .setTextSize (TypedValue .COMPLEX_UNIT_DIP , size );
304
309
}
305
310
306
311
public void setTitleTypeface (Typeface titleTypeface ) {
@@ -321,13 +326,13 @@ public void setHtmlContent(String content) {
321
326
this .content .setMovementMethod (LinkMovementMethod .getInstance ());
322
327
}
323
328
324
- public void setContentTextColor (int contentTextColor ) {
325
- this .contentTextColor = contentTextColor ;
326
- content .setTextColor (contentTextColor );
329
+ public void setContentTextColor (@ ColorInt int color ) {
330
+ this .contentTextColor = color ;
331
+ content .setTextColor (color );
327
332
}
328
333
329
- public void setContentTextSize (int contentTextColor ) {
330
- content .setTextSize (TypedValue .COMPLEX_UNIT_DIP , contentTextColor );
334
+ public void setContentTextSize (int size ) {
335
+ content .setTextSize (TypedValue .COMPLEX_UNIT_DIP , size );
331
336
}
332
337
333
338
public void setContentTypeface (Typeface contentTypeface ) {
@@ -427,14 +432,14 @@ private void initView(int dividerColor) {
427
432
title .setMaxLines (2 );
428
433
title .setEllipsize (TextUtils .TruncateAt .END );
429
434
titleLinearLayout .addView (title , createLinear (0 , LayoutParams .WRAP_CONTENT , 1 , Gravity .CENTER_VERTICAL ));
430
- titleLinearLayout .setPadding ((int ) px2dpf (10 ), 0 , (int ) px2dpf (10 ), 0 );
431
- titleLinearLayout .addView (arrow , createLinear ((int ) px2dpf (24 ), (int ) px2dpf (24 ), 0 , Gravity .CENTER_VERTICAL , 10 , 0 , 0 , 0 ));
432
- titleHeight = (int ) px2dpf (60 );
435
+ titleLinearLayout .setPadding ((int ) dpf (10 ), 0 , (int ) dpf (10 ), 0 );
436
+ titleLinearLayout .addView (arrow , createLinear ((int ) dpf (24 ), (int ) dpf (24 ), 0 , Gravity .CENTER_VERTICAL , 10 , 0 , 0 , 0 ));
437
+ titleHeight = (int ) dpf (60 );
433
438
addView (titleLinearLayout , createLinear (LayoutParams .MATCH_PARENT , titleHeight ));
434
439
divider .setBackgroundColor (dividerColor );
435
440
divider .setVisibility (useDivider ? VISIBLE : GONE );
436
441
addView (divider , createLinear (LayoutParams .MATCH_PARENT , dividerHeight , 0 , Gravity .NO_GRAVITY , 0 , 0 , 0 , 5 ));
437
- content .setPadding ((int ) px2dpf (10 ), 0 , (int ) px2dpf (10 ), (int ) px2dpf (10 ));
442
+ content .setPadding ((int ) dpf (10 ), 0 , (int ) dpf (10 ), (int ) dpf (10 ));
438
443
addView (content , createLinear (LayoutParams .MATCH_PARENT , LayoutParams .WRAP_CONTENT ));
439
444
post (() -> expandOrCollapseContent (expanded , false ));
440
445
}
@@ -443,7 +448,7 @@ private void requestHeight() {
443
448
int widthMS = MeasureSpec .makeMeasureSpec (getWidth (), MeasureSpec .AT_MOST );
444
449
int heightMS = MeasureSpec .makeMeasureSpec (0 , MeasureSpec .UNSPECIFIED );
445
450
content .measure (widthMS , heightMS );
446
- contentHeight = (int ) (content .getMeasuredHeight () + px2dpf (10 ));
451
+ contentHeight = (int ) (content .getMeasuredHeight () + dpf (10 ));
447
452
}
448
453
449
454
@ Keep
@@ -565,7 +570,7 @@ private void PaintDrawableSetCornerRadius(PaintDrawable drawable, boolean allCor
565
570
}
566
571
}
567
572
568
- private float px2dpf (float value ) {
573
+ private float dpf (float value ) {
569
574
return TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , value , getResources ().getDisplayMetrics ());
570
575
}
571
576
@@ -581,10 +586,10 @@ private LinearLayout.LayoutParams createLinear(int w, int h, int weight, int gra
581
586
LinearLayout .LayoutParams lp = new LinearLayout .LayoutParams (w , h );
582
587
lp .weight = weight ;
583
588
lp .gravity = gravity ;
584
- lp .topMargin = (int ) px2dpf (mt );
585
- lp .bottomMargin = (int ) px2dpf (mb );
586
- lp .setMarginStart ((int ) px2dpf (ms ));
587
- lp .setMarginEnd ((int ) px2dpf (me ));
589
+ lp .topMargin = (int ) dpf (mt );
590
+ lp .bottomMargin = (int ) dpf (mb );
591
+ lp .setMarginStart ((int ) dpf (ms ));
592
+ lp .setMarginEnd ((int ) dpf (me ));
588
593
return lp ;
589
594
}
590
595
@@ -593,9 +598,10 @@ private LinearLayout.LayoutParams createLinear(int w, int h, int weight, int gra
593
598
public final static int REVERSE_QUARTER = -90 ;
594
599
595
600
@ Retention (RetentionPolicy .SOURCE )
596
- public @interface RotationModel {
597
- int QUARTER = DropdownView .QUARTER ;
598
- int HALF = DropdownView .HALF ;
599
- int REVERSE_QUARTER = DropdownView .REVERSE_QUARTER ;
600
- }
601
+ @ IntDef ({
602
+ QUARTER ,
603
+ HALF ,
604
+ REVERSE_QUARTER ,
605
+ })
606
+ public @interface RotationModel {}
601
607
}
0 commit comments