11package com .amulyakhare .textdrawable ;
22
33import android .content .Context ;
4+ import android .graphics .Bitmap ;
45import android .graphics .Canvas ;
56import android .graphics .Color ;
67import android .graphics .ColorFilter ;
910import android .graphics .Rect ;
1011import android .graphics .RectF ;
1112import android .graphics .Typeface ;
13+ import android .graphics .drawable .BitmapDrawable ;
14+ import android .graphics .drawable .Drawable ;
15+ import android .graphics .drawable .NinePatchDrawable ;
1216import android .graphics .drawable .ShapeDrawable ;
1317import android .graphics .drawable .shapes .OvalShape ;
1418import android .graphics .drawable .shapes .RectShape ;
@@ -38,6 +42,7 @@ public class TextDrawable extends ShapeDrawable {
3842 private final float radius ;
3943 private final int borderThickness ;
4044 private final int borderColor ;
45+ private Bitmap bitmap ;
4146
4247 private TextDrawable (Builder builder ) {
4348 super (builder .shape );
@@ -75,6 +80,22 @@ private TextDrawable(Builder builder) {
7580 Paint paint = getPaint ();
7681 paint .setColor (builder .color );
7782
83+ //custom centre drawable
84+ if (builder .drawable != null ) {
85+ if (builder .drawable instanceof BitmapDrawable ) {
86+ bitmap = ((BitmapDrawable ) builder .drawable ).getBitmap ();
87+ } else {
88+ bitmap = Bitmap .createBitmap (builder .drawable .getIntrinsicWidth (),
89+ builder .drawable .getIntrinsicHeight (),
90+ builder .drawable .getOpacity () != PixelFormat .OPAQUE ?
91+ Bitmap .Config .ARGB_8888 : Bitmap .Config .RGB_565 );
92+ Canvas canvas = new Canvas (bitmap );
93+ builder .drawable .setBounds (0 , 0 , builder .drawable .getIntrinsicWidth (),
94+ builder .drawable .getIntrinsicHeight ());
95+ builder .drawable .draw (canvas );
96+ }
97+ }
98+
7899 }
79100
80101 private int getDarkerShade (@ ColorInt int color ) {
@@ -93,7 +114,9 @@ public void draw(Canvas canvas) {
93114 drawBorder (canvas );
94115
95116 int count = canvas .save ();
96- canvas .translate (r .left , r .top );
117+ if (bitmap == null ) {
118+ canvas .translate (r .left , r .top );
119+ }
97120
98121 // draw text
99122 int width = this .width < 0 ? r .width () : this .width ;
@@ -104,6 +127,12 @@ public void draw(Canvas canvas) {
104127 textPaint .getTextBounds (text , 0 , text .length (), textBounds );
105128 canvas .drawText (text , width / 2 , height / 2 - textBounds .exactCenterY (), textPaint );
106129
130+ if (bitmap == null ) {
131+ textPaint .setTextSize (fontSize );
132+ canvas .drawText (text , width / 2 , height / 2 - ((textPaint .descent () + textPaint .ascent ()) / 2 ), textPaint );
133+ } else {
134+ canvas .drawBitmap (bitmap , (width - bitmap .getWidth ()) / 2 , (height - bitmap .getHeight ()) / 2 , null );
135+ }
107136 canvas .restoreToCount (count );
108137
109138 }
@@ -166,6 +195,7 @@ public static class Builder implements IConfigBuilder, IShapeBuilder, IBuilder {
166195 private boolean isBold ;
167196 private boolean toUpperCase ;
168197 public float radius ;
198+ public Drawable drawable ;
169199
170200 private Builder (@ NonNull Context context ) {
171201 this .context = context ;
@@ -320,12 +350,24 @@ public TextDrawable buildRectRes(@NonNull String text, @ColorRes int colorRes) {
320350 return build (text , context .getResources ().getColor (colorRes ));
321351 }
322352
353+ @ Override
354+ public TextDrawable buildRect (@ NonNull Drawable drawable , int color ) {
355+ rect ();
356+ return build (drawable , color );
357+ }
358+
323359 @ Override
324360 public TextDrawable buildRoundRect (@ NonNull String text , @ ColorInt int color , @ IntRange (from = 1 , to = Integer .MAX_VALUE ) int radius ) {
325361 roundRect (radius );
326362 return build (text , color );
327363 }
328364
365+ @ Override
366+ public TextDrawable buildRoundRect (@ NonNull Drawable drawable , @ ColorInt int color , @ IntRange (from = 1 , to = Integer .MAX_VALUE ) int radius ) {
367+ roundRect (radius );
368+ return build (drawable , color );
369+ }
370+
329371 @ Override
330372 public TextDrawable buildRoundRectRes (@ NonNull String text , @ ColorRes int colorRes , @ DimenRes int radiusRes ) {
331373 //noinspection deprecation
@@ -339,12 +381,24 @@ public TextDrawable buildRound(@NonNull String text, @ColorInt int color) {
339381 return build (text , color );
340382 }
341383
384+ @ Override
385+ public TextDrawable buildRound (@ NonNull Drawable drawable , @ ColorInt int color ) {
386+ round ();
387+ return build (drawable , color );
388+ }
389+
342390 @ Override
343391 public TextDrawable buildRoundRes (@ NonNull String text , @ ColorRes int colorRes ) {
344392 //noinspection deprecation
345393 return buildRound (text , context .getResources ().getColor (colorRes ));
346394 }
347395
396+ @ Override
397+ public TextDrawable buildRes (@ NonNull String text , @ ColorRes int colorRes ) {
398+ //noinspection deprecation
399+ return build (text , context .getResources ().getColor (colorRes ));
400+ }
401+
348402 @ Override
349403 public TextDrawable build (@ NonNull String text , @ ColorInt int color ) {
350404 if (this .font == null )
@@ -355,9 +409,10 @@ public TextDrawable build(@NonNull String text, @ColorInt int color) {
355409 }
356410
357411 @ Override
358- public TextDrawable buildRes (@ NonNull String text , @ ColorRes int colorRes ) {
359- //noinspection deprecation
360- return build (text , context .getResources ().getColor (colorRes ));
412+ public TextDrawable build (@ NonNull Drawable drawable , @ ColorInt int color ) {
413+ this .drawable = drawable ;
414+ this .color = color ;
415+ return new TextDrawable (this );
361416 }
362417 }
363418
@@ -401,7 +456,10 @@ public interface IBuilder {
401456
402457 TextDrawable build (@ NonNull String text , @ ColorInt int color );
403458
459+ TextDrawable build (@ NonNull Drawable drawable , @ ColorRes int color );
460+
404461 TextDrawable buildRes (@ NonNull String text , @ ColorRes int colorRes );
462+
405463 }
406464
407465 public interface IShapeBuilder {
@@ -418,14 +476,20 @@ public interface IShapeBuilder {
418476
419477 TextDrawable buildRect (@ NonNull String text , @ ColorInt int color );
420478
479+ TextDrawable buildRect (@ NonNull Drawable drawable , @ ColorRes int color );
480+
421481 TextDrawable buildRectRes (@ NonNull String text , @ ColorRes int colorRes );
422482
423483 TextDrawable buildRoundRect (@ NonNull String text , @ ColorInt int color , @ IntRange (from = 1 , to = Integer .MAX_VALUE ) int radius );
424484
485+ TextDrawable buildRoundRect (@ NonNull Drawable drawable , @ ColorRes int color , @ IntRange (from = 1 , to = Integer .MAX_VALUE ) int radius );
486+
425487 TextDrawable buildRoundRectRes (@ NonNull String text , @ ColorRes int colorRes , @ IntRange (from = 1 , to = Integer .MAX_VALUE ) int radius );
426488
427489 TextDrawable buildRound (@ NonNull String text , @ ColorInt int color );
428490
491+ TextDrawable buildRound (@ NonNull Drawable drawable , @ ColorRes int color );
492+
429493 TextDrawable buildRoundRes (@ NonNull String text , @ ColorRes int colorRes );
430494 }
431495}
0 commit comments