Skip to content

Commit 1fa248c

Browse files
committed
Support of drawing image/icon in centre instead of text.
amulyakhare#21 fix NinePatchDrawable crash and add sample
1 parent 0038f19 commit 1fa248c

3 files changed

Lines changed: 91 additions & 9 deletions

File tree

library/src/main/java/com/amulyakhare/textdrawable/TextDrawable.java

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.amulyakhare.textdrawable;
22

33
import android.content.Context;
4+
import android.graphics.Bitmap;
45
import android.graphics.Canvas;
56
import android.graphics.Color;
67
import android.graphics.ColorFilter;
@@ -9,6 +10,9 @@
910
import android.graphics.Rect;
1011
import android.graphics.RectF;
1112
import android.graphics.Typeface;
13+
import android.graphics.drawable.BitmapDrawable;
14+
import android.graphics.drawable.Drawable;
15+
import android.graphics.drawable.NinePatchDrawable;
1216
import android.graphics.drawable.ShapeDrawable;
1317
import android.graphics.drawable.shapes.OvalShape;
1418
import 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
}

sample/src/main/java/com/amulyakhare/td/sample/sample/DataSource.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import android.content.Context;
44
import android.graphics.drawable.Drawable;
5+
import android.support.v4.content.ContextCompat;
6+
import android.support.v4.content.res.ResourcesCompat;
7+
8+
import com.amulyakhare.td.R;
59

610
import java.util.ArrayList;
711

@@ -13,10 +17,12 @@ public class DataSource {
1317

1418
public static final int NO_NAVIGATION = -1;
1519

20+
private Context mContext;
1621
private ArrayList<DataItem> mDataSource;
1722
private DrawableProvider mProvider;
1823

1924
public DataSource(Context context) {
25+
mContext = context;
2026
mProvider = new DrawableProvider(context);
2127
mDataSource = new ArrayList<DataItem>();
2228
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_RECT));
@@ -25,6 +31,7 @@ public DataSource(Context context) {
2531
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_RECT_BORDER));
2632
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_ROUND_RECT_BORDER));
2733
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_ROUND_BORDER));
34+
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_ROUND_DRAWABLE));
2835
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_MULTIPLE_LETTERS));
2936
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_FONT));
3037
mDataSource.add(itemFromType(DrawableProvider.SAMPLE_SIZE));
@@ -68,6 +75,11 @@ private DataItem itemFromType(int type) {
6875
label = "Round with Border";
6976
drawable = mProvider.getRoundWithBorder("F");
7077
break;
78+
case DrawableProvider.SAMPLE_ROUND_DRAWABLE:
79+
label = "Round with Drawable";
80+
drawable = mProvider.getRound(ContextCompat.getDrawable(mContext, R.drawable.check_sm));
81+
type = NO_NAVIGATION;
82+
break;
7183
case DrawableProvider.SAMPLE_MULTIPLE_LETTERS:
7284
label = "Support multiple letters";
7385
drawable = mProvider.getRectWithMultiLetter();

sample/src/main/java/com/amulyakhare/td/sample/sample/DrawableProvider.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public class DrawableProvider {
2525
public static final int SAMPLE_RECT_BORDER = 4;
2626
public static final int SAMPLE_ROUND_RECT_BORDER = 5;
2727
public static final int SAMPLE_ROUND_BORDER = 6;
28-
public static final int SAMPLE_MULTIPLE_LETTERS = 7;
29-
public static final int SAMPLE_FONT = 8;
30-
public static final int SAMPLE_SIZE = 9;
31-
public static final int SAMPLE_ANIMATION = 10;
32-
public static final int SAMPLE_MISC = 11;
28+
public static final int SAMPLE_ROUND_DRAWABLE = 7;
29+
public static final int SAMPLE_MULTIPLE_LETTERS = 8;
30+
public static final int SAMPLE_FONT = 9;
31+
public static final int SAMPLE_SIZE = 10;
32+
public static final int SAMPLE_ANIMATION = 11;
33+
public static final int SAMPLE_MISC = 12;
3334

3435
private final ColorGenerator mGenerator;
3536
private final Context mContext;
@@ -49,6 +50,11 @@ public TextDrawable getRound(String text) {
4950
.buildRound(text, mGenerator.getColor(text));
5051
}
5152

53+
public TextDrawable getRound(Drawable drawable) {
54+
return TextDrawable.builder(mContext)
55+
.buildRound(drawable, mGenerator.getColor(drawable));
56+
}
57+
5258
public TextDrawable getRoundRect(String text) {
5359
return TextDrawable.builder(mContext)
5460
.buildRoundRect(text, mGenerator.getColor(text), toPx(10));

0 commit comments

Comments
 (0)