22
33import android .content .Context ;
44import android .content .res .TypedArray ;
5- import android .graphics .Bitmap ;
65import android .graphics .Color ;
76import android .graphics .Paint ;
87import android .graphics .Rect ;
9- import android .graphics .drawable .BitmapDrawable ;
108import android .graphics .drawable .Drawable ;
11- import android .graphics .drawable .ScaleDrawable ;
129import android .text .method .TransformationMethod ;
1310import android .util .AttributeSet ;
1411
2522import java .util .List ;
2623import java .util .StringTokenizer ;
2724
28- import static android .view .Gravity .NO_GRAVITY ;
29-
3025public class OmegaCenterIconButton extends AppCompatButton {
3126
3227 private static final String DELIMITERS = "\n " ;
@@ -89,15 +84,15 @@ private void updateDrawables() {
8984 wrappedDrawable = getTintedDrawable (wrappedDrawable );
9085 }
9186 if (mDrawableSize != -1 ) {
92- wrappedDrawable = getScaleDrawable (wrappedDrawable );
87+ wrappedDrawable = updateDrawableBounds (wrappedDrawable );
9388 }
9489 wrappedDrawables [i ] = wrappedDrawable ;
9590 }
9691 }
97- setCompoundDrawablesWithIntrinsicBounds (wrappedDrawables [DRAWABLE_LEFT_POSITION ],
98- wrappedDrawables [DRAWABLE_TOP_POSITION ],
99- wrappedDrawables [DRAWABLE_RIGHT_POSITION ],
100- wrappedDrawables [DRAWABLE_BOTTOM_POSITION ]);
92+ setCompoundDrawables (wrappedDrawables [DRAWABLE_LEFT_POSITION ],
93+ wrappedDrawables [DRAWABLE_TOP_POSITION ],
94+ wrappedDrawables [DRAWABLE_RIGHT_POSITION ],
95+ wrappedDrawables [DRAWABLE_BOTTOM_POSITION ]);
10196 }
10297 }
10398
@@ -109,27 +104,14 @@ private Drawable getTintedDrawable(@NonNull Drawable drawable) {
109104 }
110105
111106 @ NonNull
112- private Drawable getScaleDrawable (@ NonNull Drawable drawable ) {
113- if (drawable instanceof BitmapDrawable ) {
114- Bitmap bitmap = ((BitmapDrawable ) drawable ).getBitmap ();
115- if (bitmap != null ) {
116- Bitmap scaledBitmap = Bitmap .createScaledBitmap (bitmap , mDrawableSize , mDrawableSize , true );
117- if (scaledBitmap != null ) return new BitmapDrawable (getResources (), scaledBitmap );
118- }
119- }
120-
121- Drawable scaleDrawable = new ScaleDrawable (drawable , NO_GRAVITY , mDrawableSize , mDrawableSize ).getDrawable ();
122- if (scaleDrawable != null ) {
123- scaleDrawable .setBounds (0 , 0 , mDrawableSize , mDrawableSize );
124- return scaleDrawable ;
125- }
126-
107+ private Drawable updateDrawableBounds (@ NonNull Drawable drawable ) {
108+ drawable .getBounds ().set (0 , 0 , mDrawableSize , mDrawableSize );
127109 return drawable ;
128110 }
129111
130112 @ Override
131- public void setCompoundDrawablesWithIntrinsicBounds ( @ DrawableRes int left , @ DrawableRes int top , @ DrawableRes int right , @ DrawableRes int bottom ) {
132- super .setCompoundDrawablesWithIntrinsicBounds (left , top , right , bottom );
113+ public void setCompoundDrawables ( @ Nullable Drawable left , @ Nullable Drawable top , @ Nullable Drawable right , @ Nullable Drawable bottom ) {
114+ super .setCompoundDrawables (left , top , right , bottom );
133115 updatePadding ();
134116 }
135117
@@ -167,7 +149,7 @@ private void updatePadding(int width) {
167149 if (width == 0 ) return ;
168150
169151 Drawable [] compoundDrawables = getCompoundDrawables ();
170- if (compoundDrawables .length == 0 || compoundDrawables . length != DRAWABLES_LENGTH ) return ;
152+ if (compoundDrawables .length != DRAWABLES_LENGTH ) return ;
171153
172154 Drawable leftDrawable = compoundDrawables [DRAWABLE_LEFT_POSITION ];
173155 Drawable rightDrawable = compoundDrawables [DRAWABLE_RIGHT_POSITION ];
@@ -177,15 +159,17 @@ private void updatePadding(int width) {
177159 int iconPadding = Math .max (getCompoundDrawablePadding (), 1 );
178160 int paddingSize ;
179161
162+ int leftWidth = leftDrawable == null ? 0 : leftDrawable .getBounds ().width ();
163+ int rightWidth = rightDrawable == null ? 0 : rightDrawable .getBounds ().width ();
164+
180165 if (leftDrawable != null && rightDrawable != null ) {
181- paddingSize = (width - leftDrawable . getIntrinsicWidth () - rightDrawable . getIntrinsicWidth () - textWidth - iconPadding * 4 ) / 2 ;
166+ paddingSize = (width - leftWidth - rightWidth - textWidth - iconPadding * 4 ) / 2 ;
182167 } else if (leftDrawable != null ) {
183- paddingSize = (width - leftDrawable . getIntrinsicWidth () - iconPadding * 2 - textWidth ) / 2 ;
168+ paddingSize = (width - leftWidth - iconPadding * 2 - textWidth ) / 2 ;
184169 } else {
185- paddingSize = (width - rightDrawable . getIntrinsicWidth () - iconPadding * 2 - textWidth ) / 2 ;
170+ paddingSize = (width - rightWidth - iconPadding * 2 - textWidth ) / 2 ;
186171 }
187172
188-
189173 super .setPadding (Math .max (mLeftPadding , paddingSize ), getPaddingTop (), Math .max (paddingSize , mRightPadding ), getPaddingBottom ());
190174 }
191175
@@ -213,7 +197,7 @@ private String divideText() {
213197 return isAllCaps () ? list .get (0 ).toUpperCase () : list .get (0 );
214198 }
215199 String longPart = list .get (0 );
216- for (int i = 0 ; i < list .size () - 1 ; i ++) {
200+ for (int i = 0 ; i < list .size () - 1 ; i ++) {
217201 if (list .get (i + 1 ).length () > list .get (i ).length ()) {
218202 longPart = list .get (i + 1 );
219203 }
@@ -224,7 +208,7 @@ private String divideText() {
224208
225209 public boolean isAllCaps () {
226210 TransformationMethod method = getTransformationMethod ();
227- if (method == null ) return false ;
211+ if (method == null ) return false ;
228212
229213 return method .getClass ().getSimpleName ().equals ("AllCapsTransformationMethod" );
230214 }
0 commit comments