99import android.text.method.TransformationMethod;
1010import android.util.AttributeSet;
1111
12+ import androidx.annotation.ColorInt;
13+ import androidx.annotation.NonNull;
14+ import androidx.annotation.Nullable;
15+ import androidx.appcompat.widget.AppCompatButton;
16+ import androidx.core.graphics.drawable.DrawableCompat;
17+
1218import com.omega_r.libs.centericonbutton.R;
1319
1420import java.util.ArrayList;
1521import java.util.List;
1622import java.util.StringTokenizer;
1723
18- import androidx.annotation.ColorInt;
19- import androidx.annotation.DrawableRes;
20- import androidx.annotation.Nullable;
21- import androidx.appcompat.widget.AppCompatButton;
22- import androidx.core.graphics.drawable.DrawableCompat;
23-
2424public class OmegaCenterIconButton extends AppCompatButton {
2525
2626 private static final String DELIMITERS = "\n";
@@ -32,9 +32,10 @@ public class OmegaCenterIconButton extends AppCompatButton {
3232
3333 private Rect textBoundsRect;
3434 @ColorInt
35- private int tintColor = Color.TRANSPARENT;
35+ private int mTintColor = Color.TRANSPARENT;
3636 private int mLeftPadding;
3737 private int mRightPadding;
38+ private int mDrawableSize;
3839
3940 public OmegaCenterIconButton(Context context) {
4041 super(context);
@@ -54,42 +55,69 @@ public OmegaCenterIconButton(Context context, AttributeSet attrs, int defStyleAt
5455 private void init(Context context, AttributeSet attrs) {
5556 if (attrs != null) {
5657 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OmegaCenterIconButton);
57- tintColor = typedArray.getColor(R.styleable.OmegaCenterIconButton_drawableTint, Color.TRANSPARENT);
58+ mTintColor = typedArray.getColor(R.styleable.OmegaCenterIconButton_drawableTint, Color.TRANSPARENT);
59+ mDrawableSize = typedArray.getDimensionPixelSize(R.styleable.OmegaCenterIconButton_drawableSize, -1);
60+
5861 float defaultDrawablePadding = getResources().getDimension(R.dimen.omega_default_drawable_padding);
5962 int drawablePadding = (int) typedArray.getDimension(R.styleable.OmegaCenterIconButton_android_drawablePadding, defaultDrawablePadding);
6063 setCompoundDrawablePadding(drawablePadding);
6164
62- updateTint ();
65+ updateDrawables ();
6366 typedArray.recycle();
6467 }
6568 mLeftPadding = getPaddingLeft();
6669 mRightPadding = getPaddingRight();
6770 }
6871
69- private void updateTint () {
70- if (tintColor != Color.TRANSPARENT) {
72+ private void updateDrawables () {
73+ if (mTintColor != Color.TRANSPARENT || mDrawableSize != -1 ) {
7174 Drawable[] drawables = getCompoundDrawables();
7275 if (drawables.length != DRAWABLES_LENGTH) return;
7376
7477 Drawable[] wrappedDrawables = new Drawable[DRAWABLES_LENGTH];
7578 for (int i = 0; i < DRAWABLES_LENGTH; i++) {
7679 Drawable drawable = drawables[i];
7780 if (drawable != null) {
78- Drawable wrappedDrawable = DrawableCompat.wrap(drawable).mutate();
79- DrawableCompat.setTint(wrappedDrawable, tintColor);
81+ Drawable wrappedDrawable = drawable;
82+ if (mTintColor != Color.TRANSPARENT) {
83+ wrappedDrawable = getTintedDrawable(wrappedDrawable);
84+ }
85+ if (mDrawableSize > 0) {
86+ wrappedDrawable = updateDrawableBounds(wrappedDrawable);
87+ }
8088 wrappedDrawables[i] = wrappedDrawable;
8189 }
8290 }
83- setCompoundDrawablesWithIntrinsicBounds(wrappedDrawables[DRAWABLE_LEFT_POSITION],
84- wrappedDrawables[DRAWABLE_TOP_POSITION],
85- wrappedDrawables[DRAWABLE_RIGHT_POSITION],
86- wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
91+ if (mDrawableSize > 0) {
92+ setCompoundDrawables(wrappedDrawables[DRAWABLE_LEFT_POSITION],
93+ wrappedDrawables[DRAWABLE_TOP_POSITION],
94+ wrappedDrawables[DRAWABLE_RIGHT_POSITION],
95+ wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
96+ } else {
97+ setCompoundDrawablesWithIntrinsicBounds(wrappedDrawables[DRAWABLE_LEFT_POSITION],
98+ wrappedDrawables[DRAWABLE_TOP_POSITION],
99+ wrappedDrawables[DRAWABLE_RIGHT_POSITION],
100+ wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
101+ }
87102 }
88103 }
89104
105+ @NonNull
106+ private Drawable getTintedDrawable(@NonNull Drawable drawable) {
107+ Drawable mutate = DrawableCompat.wrap(drawable).mutate();
108+ DrawableCompat.setTint(mutate, mTintColor);
109+ return mutate;
110+ }
111+
112+ @NonNull
113+ private Drawable updateDrawableBounds(@NonNull Drawable drawable) {
114+ drawable.getBounds().set(0, 0, mDrawableSize, mDrawableSize);
115+ return drawable;
116+ }
117+
90118 @Override
91- public void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
92- super.setCompoundDrawablesWithIntrinsicBounds (left, top, right, bottom);
119+ public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
120+ super.setCompoundDrawables (left, top, right, bottom);
93121 updatePadding();
94122 }
95123
@@ -127,7 +155,7 @@ private void updatePadding(int width) {
127155 if (width == 0) return;
128156
129157 Drawable[] compoundDrawables = getCompoundDrawables();
130- if (compoundDrawables.length == 0 || compoundDrawables.length != DRAWABLES_LENGTH) return;
158+ if (compoundDrawables.length != DRAWABLES_LENGTH) return;
131159
132160 Drawable leftDrawable = compoundDrawables[DRAWABLE_LEFT_POSITION];
133161 Drawable rightDrawable = compoundDrawables[DRAWABLE_RIGHT_POSITION];
@@ -137,15 +165,17 @@ private void updatePadding(int width) {
137165 int iconPadding = Math.max(getCompoundDrawablePadding(), 1);
138166 int paddingSize;
139167
168+ int leftWidth = leftDrawable == null ? 0 : leftDrawable.getBounds().width();
169+ int rightWidth = rightDrawable == null ? 0 : rightDrawable.getBounds().width();
170+
140171 if (leftDrawable != null && rightDrawable != null) {
141- paddingSize = (width - leftDrawable.getIntrinsicWidth() - rightDrawable.getIntrinsicWidth() - textWidth - iconPadding * 4) / 2;
172+ paddingSize = (width - leftWidth - rightWidth - textWidth - iconPadding * 4) / 2;
142173 } else if (leftDrawable != null) {
143- paddingSize = (width - leftDrawable.getIntrinsicWidth() - iconPadding * 2 - textWidth) / 2;
174+ paddingSize = (width - leftWidth - iconPadding * 2 - textWidth) / 2;
144175 } else {
145- paddingSize = (width - rightDrawable.getIntrinsicWidth() - iconPadding * 2 - textWidth) / 2;
176+ paddingSize = (width - rightWidth - iconPadding * 2 - textWidth) / 2;
146177 }
147178
148-
149179 super.setPadding(Math.max(mLeftPadding, paddingSize), getPaddingTop(), Math.max(paddingSize, mRightPadding), getPaddingBottom());
150180 }
151181
@@ -173,7 +203,7 @@ private String divideText() {
173203 return isAllCaps() ? list.get(0).toUpperCase() : list.get(0);
174204 }
175205 String longPart = list.get(0);
176- for(int i = 0; i < list.size() - 1; i++) {
206+ for (int i = 0; i < list.size() - 1; i++) {
177207 if (list.get(i + 1).length() > list.get(i).length()) {
178208 longPart = list.get(i + 1);
179209 }
@@ -184,7 +214,7 @@ private String divideText() {
184214
185215 public boolean isAllCaps() {
186216 TransformationMethod method = getTransformationMethod();
187- if(method == null) return false;
217+ if (method == null) return false;
188218
189219 return method.getClass().getSimpleName().equals("AllCapsTransformationMethod");
190220 }
0 commit comments