I have tried to create a custom class in order to implement a text as drawable but I am unable to set Typeface to Paint.
Below is the code implementation of the custom class (i.e. TextDrawable).
Here I want to get the context of Application to call the method getAssets(), but here I'm unable to call the method getContext().
public class TextDrawable extends Drawable {
private final String text;
private final Paint paint;
public TextDrawable(String text) {
this.text = text;
this.paint = new Paint();
paint.setColor(Color.GRAY);
paint.setTextSize(35f);
//paint.setTypeface(Typeface.createFromAsset(**getContext().getAssets()**, "fonts/Montserrat-Regular.otf"));
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.RIGHT);
}
@Override
public void draw(Canvas canvas) {
canvas.drawText(text, 0, 10, paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
I am not able to get getContext().getAssets() in this class.
You have to pass a Context object as a parameter to your class's constructor:
public class TextDrawable extends Drawable {
...
public TextDrawable(Context context, String text) {
paint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Montserrat-Regular.otf"));
...
}
...
}
Drawable object does not have a Context. So as suggested by @azizbekian and @Mike M, you have two options.
public TextDrawable(Context context, String text) {
paint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Montserrat-Regular.otf"));
...
}
Note that this approach will create a new Typeface instance everytime you use this Drawable, and this is generally a bad practice which also directly affects performance.
public TextDrawable(String text, Typeface typeface) {
paint.setTypeface(typeface);
...
}
This approach is better, as you have the possibility of using a single instance of Typeface for multiple objects, related or unrelated to this Drawable.
Extending the latter approach, you may create a Static TypefaceProvider as below. This makes sure that you have only One Instance of the Typeface at all times.
public class TypefaceProvider
{
private static Map<String, Typeface> TYPEFACE_MAP = new HashMap<>();
public static Typeface getTypeFaceForFont(Context context, String fontFile)
{
if (fontFile.length() <= 0) throw new InvalidParameterException("Font filename cannot be null or empty");
if (!TYPEFACE_MAP.containsKey(fontFile))
{
try
{
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+fontFile);
TYPEFACE_MAP.put(fontFile, typeface);
}
catch (Exception e)
{
throw new RuntimeException(String.format("Font file not found.\nMake sure that %s exists under \"assets/fonts/\" folder", fontFile));
}
}
return TYPEFACE_MAP.get(fontFile);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With