21
21
import android .graphics .BitmapFactory ;
22
22
import android .graphics .Canvas ;
23
23
import android .graphics .Color ;
24
+ import android .graphics .ColorFilter ;
24
25
import android .graphics .Matrix ;
25
26
import android .graphics .Paint ;
26
27
import android .graphics .PorterDuff ;
27
28
import android .graphics .PorterDuffColorFilter ;
28
29
import android .graphics .Rect ;
29
30
import android .graphics .RectF ;
30
31
import android .graphics .Typeface ;
32
+ import android .graphics .drawable .BitmapDrawable ;
33
+ import android .graphics .drawable .Drawable ;
34
+ import android .support .v4 .content .ContextCompat ;
31
35
import android .text .Layout ;
32
36
import android .text .StaticLayout ;
33
37
import android .text .TextPaint ;
34
38
import android .util .AttributeSet ;
39
+ import android .util .Log ;
35
40
import android .view .View ;
36
41
37
42
public class SegmentedButton extends View {
@@ -56,8 +61,6 @@ public SegmentedButton(Context context, AttributeSet attrs, int defStyleAttr) {
56
61
private float mClipAmount ;
57
62
private boolean clipLeftToRight ;
58
63
59
- private Paint mBitmapPaint ;
60
-
61
64
private TextPaint mTextPaint ;
62
65
private StaticLayout mStaticLayout , mStaticLayoutOverlay ;
63
66
private Rect mTextBounds = new Rect ();
@@ -123,42 +126,23 @@ else if (null != textTypeface) {
123
126
}
124
127
125
128
private void initBitmap () {
126
- if (!hasDrawable )
127
- return ;
128
-
129
- mBitmap = BitmapFactory .decodeResource (context .getResources (), drawable );
130
- if (hasDrawableWidth || hasDrawableHeight )
131
- mBitmap = getResizedBitmap (mBitmap , drawableWidth , drawableHeight );
132
-
133
- mBitmapPaint = new Paint (Paint .ANTI_ALIAS_FLAG );
129
+ if (hasDrawable ) {
130
+ mDrawable = ContextCompat .getDrawable (context , drawable );
131
+ }
134
132
135
133
if (hasDrawableTint ) {
136
134
mBitmapNormalColor = new PorterDuffColorFilter (drawableTint , PorterDuff .Mode .SRC_IN );
137
- mBitmapPaint .setColorFilter (mBitmapNormalColor );
138
135
}
139
136
140
137
if (hasDrawableTintOnSelection )
141
138
mBitmapClipColor = new PorterDuffColorFilter (drawableTintOnSelection , PorterDuff .Mode .SRC_IN );
142
139
}
143
-
144
- public Bitmap getResizedBitmap (Bitmap bm , int newWidth , int newHeight ) {
145
- int width = bm .getWidth ();
146
- int height = bm .getHeight ();
147
-
148
- float scaleWidth = hasDrawableWidth ? ((float ) newWidth ) / width : 1 ;
149
- float scaleHeight = hasDrawableHeight ? ((float ) newHeight ) / height : 1 ;
150
-
151
- Matrix matrix = new Matrix ();
152
- matrix .postScale (scaleWidth , scaleHeight );
153
-
154
- return Bitmap .createBitmap (bm , 0 , 0 , width , height , matrix , false );
155
- }
156
-
140
+
157
141
private void measureTextWidth (int width ) {
158
142
if (!hasText )
159
143
return ;
160
144
161
- int bitmapWidth = hasDrawable && drawableGravity .isHorizontal () ? mBitmap . getWidth () : 0 ;
145
+ int bitmapWidth = hasDrawable && drawableGravity .isHorizontal () ? mDrawable . getIntrinsicWidth () : 0 ;
162
146
163
147
int textWidth = width - (bitmapWidth + getPaddingLeft () + getPaddingRight ());
164
148
@@ -177,11 +161,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
177
161
int heightRequirement = MeasureSpec .getSize (heightMeasureSpec );
178
162
179
163
int width = 0 ;
180
- int bitmapWidth = hasDrawable ? mBitmap . getWidth () : 0 ;
164
+ int bitmapWidth = hasDrawable ? mDrawable . getIntrinsicWidth () : 0 ;
181
165
int textWidth = hasText ? mStaticLayout .getWidth () : 0 ;
182
166
183
167
int height = getPaddingTop () + getPaddingBottom ();
184
- int bitmapHeight = hasDrawable ? mBitmap . getHeight () : 0 ;
168
+ int bitmapHeight = hasDrawable ? mDrawable . getIntrinsicHeight () : 0 ;
185
169
int textHeight = hasText ? mStaticLayout .getHeight () : 0 ;
186
170
187
171
switch (widthMode ) {
@@ -265,8 +249,8 @@ private void calculate(int width, int height) {
265
249
266
250
float bitmapHeight = 0 , bitmapWidth = 0 ;
267
251
if (hasDrawable ) {
268
- bitmapHeight = mBitmap . getHeight ();
269
- bitmapWidth = mBitmap . getWidth ();
252
+ bitmapHeight = mDrawable . getIntrinsicHeight ();
253
+ bitmapWidth = mDrawable . getIntrinsicWidth ();
270
254
}
271
255
272
256
@@ -336,7 +320,7 @@ private void calculate(int width, int height) {
336
320
337
321
private PorterDuffColorFilter mBitmapNormalColor , mBitmapClipColor ;
338
322
339
- private Bitmap mBitmap ;
323
+ private Drawable mDrawable ;
340
324
341
325
@ Override
342
326
protected void onDraw (Canvas canvas ) {
@@ -372,8 +356,7 @@ protected void onDraw(Canvas canvas) {
372
356
373
357
// Bitmap normal
374
358
if (hasDrawable ) {
375
- mBitmapPaint .setColorFilter (mBitmapNormalColor );
376
- canvas .drawBitmap (mBitmap , bitmap_X , bitmap_Y , mBitmapPaint );
359
+ drawDrawableWithColorFilter (canvas , mBitmapNormalColor );
377
360
}
378
361
// NORMAL -end
379
362
@@ -399,15 +382,29 @@ protected void onDraw(Canvas canvas) {
399
382
400
383
// Bitmap clip
401
384
if (hasDrawable ) {
402
- if (hasDrawableTintOnSelection )
403
- mBitmapPaint .setColorFilter (mBitmapClipColor );
404
- canvas .drawBitmap (mBitmap , bitmap_X , bitmap_Y , mBitmapPaint );
385
+ drawDrawableWithColorFilter (canvas , mBitmapClipColor );
405
386
}
406
387
// CLIP -end
407
388
408
389
canvas .restore ();
409
390
}
410
391
392
+ private void drawDrawableWithColorFilter (Canvas canvas , ColorFilter colorFilter ){
393
+ int drawableX = (int )bitmap_X ;
394
+ int drawableY = (int )bitmap_Y ;
395
+ int drawableWidth = mDrawable .getIntrinsicWidth ();
396
+ if (hasDrawableWidth ) {
397
+ drawableWidth = this .drawableWidth ;
398
+ }
399
+ int drawableHeight = mDrawable .getIntrinsicHeight ();
400
+ if (hasDrawableHeight ) {
401
+ drawableHeight = this .drawableHeight ;
402
+ }
403
+ mDrawable .setColorFilter (colorFilter );
404
+ mDrawable .setBounds (drawableX , drawableY , drawableX + drawableWidth , drawableY + drawableHeight );
405
+ mDrawable .draw (canvas );
406
+ }
407
+
411
408
public void clipToLeft (float clip ) {
412
409
clipLeftToRight = false ;
413
410
mClipAmount = 1.0f - clip ;
@@ -557,8 +554,18 @@ public boolean isHorizontal() {
557
554
* @param resId is your drawable's resource id
558
555
*/
559
556
public void setDrawable (int resId ) {
560
- drawable = resId ;
561
- mBitmap = BitmapFactory .decodeResource (context .getResources (), resId );
557
+ setDrawable (ContextCompat .getDrawable (context , resId ));
558
+ }
559
+
560
+ /**
561
+ * Sets button's drawable by given drawable object and its position
562
+ *
563
+ * @param drawable is your drawable object
564
+ */
565
+ public void setDrawable (Drawable drawable ){
566
+ mDrawable = drawable ;
567
+ hasDrawable = true ;
568
+ requestLayout ();
562
569
}
563
570
564
571
/**
0 commit comments