24
24
*/
25
25
public class CustomButton extends LinearLayout implements View .OnClickListener {
26
26
27
- public static int LEFT = 0 ;
28
- public static int TOP = 1 ;
29
- public static int RIGHT = 2 ;
30
- public static int BOTTOM = 3 ;
27
+ public static final int LEFT = 0 ;
28
+ public static final int TOP = 1 ;
29
+ public static final int RIGHT = 2 ;
30
+ public static final int BOTTOM = 3 ;
31
31
32
32
protected TextView textView ; //text container
33
33
protected LinearLayout container ; //all content container
@@ -52,12 +52,12 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
52
52
private int drawablePosition ;
53
53
private int imagePadding ;
54
54
private int [] imagePaddingArray ;
55
-
55
+ private int imageScaleTypeAttr ;
56
56
57
57
private int shapeType ; // converted shape type from shapeTypeAttr
58
+ private ImageView .ScaleType imageScaleType ;
58
59
59
60
//TODO stosunek podziału image / text
60
- //TODO scaletype of image
61
61
62
62
public CustomButton (Context context , AttributeSet attrs ) {
63
63
super (context , attrs );
@@ -99,13 +99,13 @@ public CustomButton(Context context, AttributeSet attrs) {
99
99
drawableNormal = attributes .getDrawable (R .styleable .CustomButton_cb_image_normal );
100
100
drawablePressed = attributes .getDrawable (R .styleable .CustomButton_cb_image_pressed );
101
101
drawableDisabled = attributes .getDrawable (R .styleable .CustomButton_cb_image_disabled );
102
+ imageScaleTypeAttr = attributes .getInteger (R .styleable .CustomButton_cb_image_scale_type , 0 );
102
103
imagePadding = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding , 0 );
103
104
imagePaddingArray [LEFT ] = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding_left , imagePadding );
104
105
imagePaddingArray [TOP ] = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding_top , imagePadding );
105
106
imagePaddingArray [RIGHT ] = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding_right , imagePadding );
106
107
imagePaddingArray [BOTTOM ] = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding_bottom , imagePadding );
107
108
108
-
109
109
if (backgroundColorState != null ) { // if backgroundColor state is not null the set color from color state list to specific colors
110
110
backgroundColorStateListToIntegers (backgroundColorState );
111
111
}
@@ -114,6 +114,30 @@ public CustomButton(Context context, AttributeSet attrs) {
114
114
frameColorStateListToIntegers (frameColorState );
115
115
}
116
116
117
+ switch (imageScaleTypeAttr ) {
118
+ case 0 :
119
+ imageScaleType = ImageView .ScaleType .CENTER ;
120
+ break ;
121
+ case 1 :
122
+ imageScaleType = ImageView .ScaleType .CENTER_INSIDE ;
123
+ break ;
124
+ case 2 :
125
+ imageScaleType = ImageView .ScaleType .CENTER_CROP ;
126
+ break ;
127
+ case 3 :
128
+ imageScaleType = ImageView .ScaleType .FIT_CENTER ;
129
+ break ;
130
+ case 4 :
131
+ imageScaleType = ImageView .ScaleType .FIT_START ;
132
+ break ;
133
+ case 5 :
134
+ imageScaleType = ImageView .ScaleType .FIT_END ;
135
+ break ;
136
+ case 6 :
137
+ imageScaleType = ImageView .ScaleType .FIT_XY ;
138
+ break ;
139
+ }
140
+
117
141
setContent (context );
118
142
119
143
shapeType = (shapeTypeAttr == 0 ) ? GradientDrawable .RECTANGLE : GradientDrawable .OVAL ; // convert xml attributes value to shape type constant
@@ -163,25 +187,22 @@ private void setContent(Context context) {
163
187
164
188
imageContainer = new ImageView (context );
165
189
imageContainer .setPadding (imagePaddingArray [LEFT ], imagePaddingArray [TOP ], imagePaddingArray [RIGHT ], imagePaddingArray [BOTTOM ]);
190
+ imageContainer .setScaleType (imageScaleType );
191
+ imageContainer .setLayoutParams (layoutParams );
166
192
if (drawable != null ) {
167
193
imageContainer .setImageDrawable (drawable );
168
194
} else {
169
195
imageContainer .setImageDrawable (listDrawable );
170
196
}
171
197
172
- imageContainer .setLayoutParams (layoutParams );
173
- imageContainer .setScaleType (ImageView .ScaleType .FIT_CENTER );
174
-
175
198
switch (drawablePosition ) {
176
- case 0 :
199
+ case LEFT :
177
200
container .setOrientation (HORIZONTAL );
178
201
container .addView (imageContainer );
179
202
break ;
180
- case 1 :
203
+ case TOP :
181
204
container .addView (imageContainer );
182
205
break ;
183
- case 3 :
184
- break ;
185
206
}
186
207
if (text != null ) {
187
208
textView .setLayoutParams (layoutParams );
@@ -190,10 +211,10 @@ private void setContent(Context context) {
190
211
}
191
212
192
213
switch (drawablePosition ) {
193
- case 2 :
214
+ case RIGHT :
194
215
container .addView (imageContainer );
195
216
break ;
196
- case 3 :
217
+ case BOTTOM :
197
218
container .setOrientation (HORIZONTAL );
198
219
container .addView (imageContainer );
199
220
break ;
@@ -418,33 +439,40 @@ public void setTextPadding(int[] padding) {
418
439
* @param drawableNormal state normal
419
440
* @param drawablePressed state pressed
420
441
* @param drawableDisabled state disabled
442
+ * @param scaleType all without MATRIX
421
443
* @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
422
444
*/
423
- public void setImage (int position , Drawable drawableNormal , Drawable drawablePressed , Drawable drawableDisabled , int [] padding ) {
445
+ public void setImage (int position , Drawable drawableNormal , Drawable drawablePressed , Drawable drawableDisabled , ImageView . ScaleType scaleType , int [] padding ) {
424
446
this .drawableDisabled = drawableDisabled ;
425
447
this .drawablePressed = drawablePressed ;
426
448
this .drawableNormal = drawableNormal ;
427
449
this .drawablePosition = position ;
450
+ this .imageScaleType = scaleType ;
428
451
429
452
if (padding != null )
430
453
imageContainer .setPadding (padding [LEFT ], padding [TOP ], padding [RIGHT ], padding [BOTTOM ]);
454
+ if (scaleType != null )
455
+ imageContainer .setScaleType (scaleType );
431
456
432
457
setContent (getContext ());
433
458
}
434
459
435
460
/**
436
461
* Set image to button
437
462
*
438
- * @param position CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM
439
- * @param drawable image resource
440
- * @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
463
+ * @param position CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM
464
+ * @param drawable image resource
465
+ * @param scaleType all without MATRIX
466
+ * @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
441
467
*/
442
- public void setImage (int position , Drawable drawable , int [] padding ) {
468
+ public void setImage (int position , Drawable drawable , ImageView . ScaleType scaleType , int [] padding ) {
443
469
this .drawable = drawable ;
444
470
this .drawablePosition = position ;
445
471
446
472
if (padding != null )
447
473
imageContainer .setPadding (padding [LEFT ], padding [TOP ], padding [RIGHT ], padding [BOTTOM ]);
474
+ if (scaleType != null )
475
+ imageContainer .setScaleType (scaleType );
448
476
449
477
setContent (getContext ());
450
478
}
@@ -603,4 +631,8 @@ public int getImagePadding() {
603
631
public int [] getImagePaddingArray () {
604
632
return imagePaddingArray ;
605
633
}
634
+
635
+ public ImageView .ScaleType getImageScaleType () {
636
+ return imageScaleType ;
637
+ }
606
638
}
0 commit comments