22
33import android .content .Context ;
44import android .content .res .TypedArray ;
5+ import android .graphics .Bitmap ;
56import android .graphics .Color ;
67import android .graphics .Paint ;
78import android .graphics .Rect ;
9+ import android .graphics .drawable .BitmapDrawable ;
810import android .graphics .drawable .Drawable ;
11+ import android .graphics .drawable .ScaleDrawable ;
912import android .text .method .TransformationMethod ;
1013import android .util .AttributeSet ;
1114
15+ import androidx .annotation .ColorInt ;
16+ import androidx .annotation .DrawableRes ;
17+ import androidx .annotation .NonNull ;
18+ import androidx .annotation .Nullable ;
19+ import androidx .appcompat .widget .AppCompatButton ;
20+ import androidx .core .graphics .drawable .DrawableCompat ;
21+
1222import com .omega_r .libs .centericonbutton .R ;
1323
1424import java .util .ArrayList ;
1525import java .util .List ;
1626import java .util .StringTokenizer ;
1727
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 ;
28+ import static android .view .Gravity .NO_GRAVITY ;
2329
2430public class OmegaCenterIconButton extends AppCompatButton {
2531
@@ -32,9 +38,10 @@ public class OmegaCenterIconButton extends AppCompatButton {
3238
3339 private Rect textBoundsRect ;
3440 @ ColorInt
35- private int tintColor = Color .TRANSPARENT ;
41+ private int mTintColor = Color .TRANSPARENT ;
3642 private int mLeftPadding ;
3743 private int mRightPadding ;
44+ private int mDrawableSize ;
3845
3946 public OmegaCenterIconButton (Context context ) {
4047 super (context );
@@ -54,29 +61,36 @@ public OmegaCenterIconButton(Context context, AttributeSet attrs, int defStyleAt
5461 private void init (Context context , AttributeSet attrs ) {
5562 if (attrs != null ) {
5663 TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .OmegaCenterIconButton );
57- tintColor = typedArray .getColor (R .styleable .OmegaCenterIconButton_drawableTint , Color .TRANSPARENT );
64+ mTintColor = typedArray .getColor (R .styleable .OmegaCenterIconButton_drawableTint , Color .TRANSPARENT );
65+ mDrawableSize = typedArray .getDimensionPixelSize (R .styleable .OmegaCenterIconButton_drawableSize , -1 );
66+
5867 float defaultDrawablePadding = getResources ().getDimension (R .dimen .omega_default_drawable_padding );
5968 int drawablePadding = (int ) typedArray .getDimension (R .styleable .OmegaCenterIconButton_android_drawablePadding , defaultDrawablePadding );
6069 setCompoundDrawablePadding (drawablePadding );
6170
62- updateTint ();
71+ updateDrawables ();
6372 typedArray .recycle ();
6473 }
6574 mLeftPadding = getPaddingLeft ();
6675 mRightPadding = getPaddingRight ();
6776 }
6877
69- private void updateTint () {
70- if (tintColor != Color .TRANSPARENT ) {
78+ private void updateDrawables () {
79+ if (mTintColor != Color .TRANSPARENT || mDrawableSize != - 1 ) {
7180 Drawable [] drawables = getCompoundDrawables ();
7281 if (drawables .length != DRAWABLES_LENGTH ) return ;
7382
7483 Drawable [] wrappedDrawables = new Drawable [DRAWABLES_LENGTH ];
7584 for (int i = 0 ; i < DRAWABLES_LENGTH ; i ++) {
7685 Drawable drawable = drawables [i ];
7786 if (drawable != null ) {
78- Drawable wrappedDrawable = DrawableCompat .wrap (drawable ).mutate ();
79- DrawableCompat .setTint (wrappedDrawable , tintColor );
87+ Drawable wrappedDrawable = drawable ;
88+ if (mTintColor != Color .TRANSPARENT ) {
89+ wrappedDrawable = getTintedDrawable (wrappedDrawable );
90+ }
91+ if (mDrawableSize != -1 ) {
92+ wrappedDrawable = getScaleDrawable (wrappedDrawable );
93+ }
8094 wrappedDrawables [i ] = wrappedDrawable ;
8195 }
8296 }
@@ -87,6 +101,32 @@ private void updateTint() {
87101 }
88102 }
89103
104+ @ NonNull
105+ private Drawable getTintedDrawable (@ NonNull Drawable drawable ) {
106+ Drawable mutate = DrawableCompat .wrap (drawable ).mutate ();
107+ DrawableCompat .setTint (mutate , mTintColor );
108+ return mutate ;
109+ }
110+
111+ @ 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+
127+ return drawable ;
128+ }
129+
90130 @ Override
91131 public void setCompoundDrawablesWithIntrinsicBounds (@ DrawableRes int left , @ DrawableRes int top , @ DrawableRes int right , @ DrawableRes int bottom ) {
92132 super .setCompoundDrawablesWithIntrinsicBounds (left , top , right , bottom );
0 commit comments