Skip to content

Commit 065f8d6

Browse files
committed
Add button icon color state simplification
1 parent bee24bb commit 065f8d6

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

app/src/main/java/pl/sigmapoint/StartFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private void init() {
9292
enableTB = (ToggleButton) view.findViewById(R.id.enable);
9393

9494
enableTB.setChecked(true);
95+
generateCB.setImageColors(Color.CYAN, Color.GREEN, Color.MAGENTA);
9596

9697
array = new String[4];
9798
array[CustomButton.LEFT] = "Left";

app/src/main/res/drawable/owl.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="oval">
4+
5+
<solid android:color="@color/red_dark" />
6+
7+
</shape>

app/src/main/res/layout/fragment_start.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@
313313
android:padding="2dp"
314314
android:text="@string/generate"
315315
app:cb_frame_size="5dp"
316+
app:cb_image="@drawable/ic_owl"
317+
app:cb_image_padding="6dp"
318+
app:cb_image_position="left"
316319
app:cb_primary_color="@color/blue_normal"
317320
app:cb_secondary_color="@color/red_normal"
318321
app:cb_shape_radius="5dp" />

library/src/main/java/pl/sigmapoint/customview/CustomButton.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
import android.content.Context;
55
import android.content.res.ColorStateList;
66
import android.content.res.TypedArray;
7+
import android.graphics.Bitmap;
8+
import android.graphics.BitmapShader;
9+
import android.graphics.Canvas;
710
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;
816
import android.graphics.drawable.Drawable;
917
import android.graphics.drawable.GradientDrawable;
1018
import android.graphics.drawable.RippleDrawable;
@@ -57,6 +65,8 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
5765
private float frameSize; // frame size
5866
private boolean isElevationEnabled; // is elevation should be displayed >=API 21
5967
private Drawable drawableNormal, drawableDisabled, drawablePressed;
68+
private int drawableColorNormal, drawableColorDisabled, drawableColorPressed;
69+
private ColorStateList drawableColorStateListAttr;
6070
private int drawablePosition;
6171
private int imagePadding;
6272
private int[] imagePaddingArray;
@@ -151,6 +161,10 @@ public CustomButton(Context context, AttributeSet attrs) {
151161
drawableNormal = attributes.getDrawable(R.styleable.CustomButton_cb_image_normal);
152162
drawablePressed = attributes.getDrawable(R.styleable.CustomButton_cb_image_pressed);
153163
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);
154168
imageScaleTypeAttr = attributes.getInteger(R.styleable.CustomButton_cb_image_scale_type, 3);
155169
imagePadding = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding, 0);
156170
imagePaddingArray[LEFT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_left, imagePadding);
@@ -167,6 +181,25 @@ public CustomButton(Context context, AttributeSet attrs) {
167181
frameColorStateListToIntegers(frameColorState);
168182
}
169183

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+
170203
//WARNING: If you want to change it, you should change it in attr xml too
171204
switch (imageScaleTypeAttr) {
172205
case 0:
@@ -216,6 +249,7 @@ public void setEnabled(boolean enabled) {
216249
super.setEnabled(enabled);
217250
textView.setEnabled(enabled); //chain child views with parent state
218251
container.setEnabled(enabled);
252+
imageContainer.setEnabled(enabled);
219253
}
220254

221255
private void setContent() {
@@ -377,6 +411,16 @@ private void frameColorStateListToIntegers(ColorStateList colorStateList) {
377411
frameColorDisabled = globalColor;
378412
}
379413

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+
380424
private void updateText() {
381425
textColorArray = new int[]{textColorPressed, textColorNormal, textColorDisabled};
382426
textColorList = new ColorStateList(stateArray, textColorArray);
@@ -426,6 +470,36 @@ private int lighterColor(int color) {
426470
return Color.HSVToColor(hsv);
427471
}
428472

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+
429503

430504
/**
431505
* Set shape backgroundColorNormal.
@@ -705,6 +779,40 @@ public void setImage(int position, Drawable drawable, ImageView.ScaleType scaleT
705779
setContent();
706780
}
707781

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+
708816
/**
709817
* Set elevation to button. If enabled button is smaller because shadow must have space to show.
710818
*
@@ -877,4 +985,16 @@ public int getPrimaryColor() {
877985
public int getSecondaryColor() {
878986
return secondaryColor;
879987
}
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+
}
8801000
}

library/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<attr name="cb_image_normal" format="reference" />
4747
<attr name="cb_image_disabled" format="reference" />
4848
<attr name="cb_image_pressed" format="reference" />
49+
<attr name="cb_image_color_normal" format="color" />
50+
<attr name="cb_image_color_pressed" format="color" />
51+
<attr name="cb_image_color_disabled" format="color" />
52+
<attr name="cb_image_color_list" format="color" />
4953
<attr name="cb_image_padding" format="dimension" />
5054
<attr name="cb_image_padding_left" format="dimension" />
5155
<attr name="cb_image_padding_top" format="dimension" />

0 commit comments

Comments
 (0)