4
4
import android .content .Context ;
5
5
import android .content .res .ColorStateList ;
6
6
import android .content .res .TypedArray ;
7
+ import android .graphics .Bitmap ;
8
+ import android .graphics .BitmapShader ;
9
+ import android .graphics .Canvas ;
7
10
import android .graphics .Color ;
11
+ import android .graphics .ColorFilter ;
12
+ import android .graphics .LightingColorFilter ;
13
+ import android .graphics .Paint ;
14
+ import android .graphics .Shader ;
15
+ import android .graphics .drawable .BitmapDrawable ;
8
16
import android .graphics .drawable .Drawable ;
9
17
import android .graphics .drawable .GradientDrawable ;
10
18
import android .graphics .drawable .RippleDrawable ;
@@ -57,6 +65,8 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
57
65
private float frameSize ; // frame size
58
66
private boolean isElevationEnabled ; // is elevation should be displayed >=API 21
59
67
private Drawable drawableNormal , drawableDisabled , drawablePressed ;
68
+ private int drawableColorNormal , drawableColorDisabled , drawableColorPressed ;
69
+ private ColorStateList drawableColorStateListAttr ;
60
70
private int drawablePosition ;
61
71
private int imagePadding ;
62
72
private int [] imagePaddingArray ;
@@ -151,6 +161,10 @@ public CustomButton(Context context, AttributeSet attrs) {
151
161
drawableNormal = attributes .getDrawable (R .styleable .CustomButton_cb_image_normal );
152
162
drawablePressed = attributes .getDrawable (R .styleable .CustomButton_cb_image_pressed );
153
163
drawableDisabled = attributes .getDrawable (R .styleable .CustomButton_cb_image_disabled );
164
+ drawableColorStateListAttr = attributes .getColorStateList (R .styleable .CustomButton_cb_image_color_list );
165
+ drawableColorNormal = attributes .getColor (R .styleable .CustomButton_cb_image_color_normal , Integer .MAX_VALUE );
166
+ drawableColorPressed = attributes .getColor (R .styleable .CustomButton_cb_image_color_pressed , Integer .MAX_VALUE );
167
+ drawableColorDisabled = attributes .getColor (R .styleable .CustomButton_cb_image_color_disabled , Integer .MAX_VALUE );
154
168
imageScaleTypeAttr = attributes .getInteger (R .styleable .CustomButton_cb_image_scale_type , 3 );
155
169
imagePadding = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding , 0 );
156
170
imagePaddingArray [LEFT ] = (int ) attributes .getDimension (R .styleable .CustomButton_cb_image_padding_left , imagePadding );
@@ -167,6 +181,25 @@ public CustomButton(Context context, AttributeSet attrs) {
167
181
frameColorStateListToIntegers (frameColorState );
168
182
}
169
183
184
+ if (drawableColorStateListAttr != null )
185
+ imageColorStateListToIntegers (drawableColorStateListAttr );
186
+ setImageColorStateList ();
187
+ if (drawableNormal != null ) {
188
+ if (drawableColorNormal != Integer .MAX_VALUE ) {
189
+ drawableNormal = changeDrawableColor (((BitmapDrawable ) drawableNormal ).getBitmap (), drawableColorNormal );
190
+ }
191
+ if (drawableColorPressed != Integer .MAX_VALUE ) {
192
+ if (drawablePressed == null )
193
+ drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
194
+ drawablePressed = changeDrawableColor (((BitmapDrawable ) drawablePressed ).getBitmap (), drawableColorPressed );
195
+ }
196
+ if (drawableColorDisabled != Integer .MAX_VALUE ) {
197
+ if (drawableDisabled == null )
198
+ drawableDisabled = drawableNormal .getConstantState ().newDrawable ().mutate ();
199
+ drawableDisabled = changeDrawableColor (((BitmapDrawable ) drawableDisabled ).getBitmap (), drawableColorDisabled );
200
+ }
201
+ }
202
+
170
203
//WARNING: If you want to change it, you should change it in attr xml too
171
204
switch (imageScaleTypeAttr ) {
172
205
case 0 :
@@ -216,6 +249,7 @@ public void setEnabled(boolean enabled) {
216
249
super .setEnabled (enabled );
217
250
textView .setEnabled (enabled ); //chain child views with parent state
218
251
container .setEnabled (enabled );
252
+ imageContainer .setEnabled (enabled );
219
253
}
220
254
221
255
private void setContent () {
@@ -377,6 +411,16 @@ private void frameColorStateListToIntegers(ColorStateList colorStateList) {
377
411
frameColorDisabled = globalColor ;
378
412
}
379
413
414
+ private void imageColorStateListToIntegers (ColorStateList colorStateList ) {
415
+
416
+ int globalColor = colorStateList .getColorForState (new int []{}, 0 );
417
+
418
+ drawableColorPressed = colorStateList .getColorForState (new int []{android .R .attr .state_pressed }, globalColor );
419
+ drawableColorNormal = colorStateList .getColorForState (new int []{android .R .attr .state_enabled }, globalColor );
420
+ drawableColorDisabled = globalColor ;
421
+ }
422
+
423
+
380
424
private void updateText () {
381
425
textColorArray = new int []{textColorPressed , textColorNormal , textColorDisabled };
382
426
textColorList = new ColorStateList (stateArray , textColorArray );
@@ -426,6 +470,36 @@ private int lighterColor(int color) {
426
470
return Color .HSVToColor (hsv );
427
471
}
428
472
473
+ private void setImageColorStateList () {
474
+ if (drawableNormal != null ) {
475
+ if (drawableColorNormal != Integer .MAX_VALUE ) {
476
+ drawableNormal = changeDrawableColor (((BitmapDrawable ) drawableNormal ).getBitmap (), drawableColorNormal );
477
+ }
478
+ if (drawableColorPressed != Integer .MAX_VALUE ) {
479
+ if (drawablePressed == null )
480
+ drawablePressed = drawableNormal .getConstantState ().newDrawable ().mutate ();
481
+ drawablePressed = changeDrawableColor (((BitmapDrawable ) drawablePressed ).getBitmap (), drawableColorPressed );
482
+ }
483
+ if (drawableColorDisabled != Integer .MAX_VALUE ) {
484
+ if (drawableDisabled == null )
485
+ drawableDisabled = drawableNormal .getConstantState ().newDrawable ().mutate ();
486
+ drawableDisabled = changeDrawableColor (((BitmapDrawable ) drawableDisabled ).getBitmap (), drawableColorDisabled );
487
+ }
488
+ }
489
+ }
490
+
491
+ private Drawable changeDrawableColor (Bitmap bitmap , int color ) {
492
+ BitmapShader shader = new BitmapShader (bitmap , Shader .TileMode .REPEAT , Shader .TileMode .REPEAT );
493
+ Paint paint = new Paint ();
494
+ paint .setShader (shader );
495
+ ColorFilter filter = new LightingColorFilter (color , color );
496
+ paint .setColorFilter (filter );
497
+ Bitmap b = bitmap .copy (Bitmap .Config .ARGB_8888 , true );
498
+ Canvas canvas = new Canvas (b );
499
+ canvas .drawPaint (paint );
500
+ return new BitmapDrawable (b );
501
+ }
502
+
429
503
430
504
/**
431
505
* Set shape backgroundColorNormal.
@@ -705,6 +779,40 @@ public void setImage(int position, Drawable drawable, ImageView.ScaleType scaleT
705
779
setContent ();
706
780
}
707
781
782
+ public void setImageColors (int normal , int pressed , int disabled ) {
783
+ this .drawableColorNormal = normal ;
784
+ this .drawableColorPressed = pressed ;
785
+ this .drawableColorDisabled = disabled ;
786
+
787
+ setImageColorStateList ();
788
+ setContent ();
789
+ }
790
+
791
+ public void setImageColors (ColorStateList colorStateList ) {
792
+ imageColorStateListToIntegers (colorStateList );
793
+ setImageColorStateList ();
794
+ setContent ();
795
+ }
796
+
797
+ public void setImageNormalColor (int color ) {
798
+ this .drawableColorNormal = color ;
799
+ setImageColorStateList ();
800
+ setContent ();
801
+ }
802
+
803
+ public void setImagePressedColor (int color ) {
804
+ this .drawableColorPressed = color ;
805
+ setImageColorStateList ();
806
+ setContent ();
807
+ }
808
+
809
+ public void setImageDisableColor (int color ) {
810
+ this .drawableColorDisabled = color ;
811
+ setImageColorStateList ();
812
+ setContent ();
813
+ }
814
+
815
+
708
816
/**
709
817
* Set elevation to button. If enabled button is smaller because shadow must have space to show.
710
818
*
@@ -877,4 +985,16 @@ public int getPrimaryColor() {
877
985
public int getSecondaryColor () {
878
986
return secondaryColor ;
879
987
}
988
+
989
+ public int getImageColorNormal () {
990
+ return drawableColorNormal ;
991
+ }
992
+
993
+ public int getImageColorDisabled () {
994
+ return drawableColorDisabled ;
995
+ }
996
+
997
+ public int getImageColorPressed () {
998
+ return drawableColorPressed ;
999
+ }
880
1000
}
0 commit comments