Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bgColor property #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.amulyakhare.textdrawable;

import android.graphics.*;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
Expand All @@ -15,14 +15,15 @@ public class TextDrawable extends ShapeDrawable {
private final Paint textPaint;
private final Paint borderPaint;
private static final float SHADE_FACTOR = 0.9f;
private final String text;
private final int color;
private final String text;
private final int color;
private final RectShape shape;
private final int height;
private final int width;
private final int fontSize;
private final float radius;
private final int borderThickness;
private final int height;
private final int width;
private final int fontSize;
private final float radius;
private final int borderThickness;
private final int bgColor;

private TextDrawable(Builder builder) {
super(builder.shape);
Expand All @@ -36,10 +37,11 @@ private TextDrawable(Builder builder) {
// text and color
text = builder.toUpperCase ? builder.text.toUpperCase() : builder.text;
color = builder.color;
bgColor = builder.backgroundColor;

// text paint settings
fontSize = builder.fontSize;
textPaint = new Paint();
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(builder.textColor);
textPaint.setAntiAlias(true);
textPaint.setFakeBoldText(builder.isBold);
Expand All @@ -50,21 +52,21 @@ private TextDrawable(Builder builder) {

// border paint settings
borderThickness = builder.borderThickness;
borderPaint = new Paint();
borderPaint.setColor(getDarkerShade(color));
borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setColor(color);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderThickness);

// drawable paint color
Paint paint = getPaint();
paint.setColor(color);
paint.setColor(builder.backgroundColor);

}

private int getDarkerShade(int color) {
return Color.rgb((int)(SHADE_FACTOR * Color.red(color)),
(int)(SHADE_FACTOR * Color.green(color)),
(int)(SHADE_FACTOR * Color.blue(color)));
return Color.rgb((int) (SHADE_FACTOR * Color.red(color)),
(int) (SHADE_FACTOR * Color.green(color)),
(int) (SHADE_FACTOR * Color.blue(color)));
}

@Override
Expand Down Expand Up @@ -93,16 +95,17 @@ public void draw(Canvas canvas) {
}

private void drawBorder(Canvas canvas) {
//for new device density 1dp=3px,so half of 1dp will get 1px,
//this will get 2px inset
RectF rect = new RectF(getBounds());
rect.inset(borderThickness/2, borderThickness/2);
double inset = Math.ceil(borderThickness / 2);
rect.inset((int) inset, (int) inset);

if (shape instanceof OvalShape) {
canvas.drawOval(rect, borderPaint);
}
else if (shape instanceof RoundRectShape) {
} else if (shape instanceof RoundRectShape) {
canvas.drawRoundRect(rect, radius, radius, borderPaint);
}
else {
} else {
canvas.drawRect(rect, borderPaint);
}
}
Expand Down Expand Up @@ -142,6 +145,8 @@ public static class Builder implements IConfigBuilder, IShapeBuilder, IBuilder {

private int color;

private int backgroundColor;

private int borderThickness;

private int width;
Expand All @@ -163,6 +168,7 @@ public static class Builder implements IConfigBuilder, IShapeBuilder, IBuilder {
public float radius;

private Builder() {
backgroundColor= Color.TRANSPARENT;
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
Expand All @@ -176,6 +182,11 @@ private Builder() {
toUpperCase = false;
}

public IConfigBuilder bgColor(int color) {
this.backgroundColor = color;
return this;
}

public IConfigBuilder width(int width) {
this.width = width;
return this;
Expand Down