I've done some research on the net and found this class here: link
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.WHITE);
paint.setTextSize(24f);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setShadowLayer(6f, 0, 0, Color.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setTextAlign(Paint.Align.LEFT);
}
@Override
public void draw(@NonNull Canvas canvas) {
canvas.drawText(text, 0, 0, paint);
}
@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
paint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
The drawable
is a <layer-list>
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/backgroundColor" />
<item android:top="10dp" android:left="10dp" android:bottom="10dp" android:right="10dp">
<shape android:shape="rectangle">
<solid android:color="#f00"/>
</shape>
</item>
<item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp" >
<bitmap android:src="@drawable/imagen1" />
</item>
</layer-list>
I have tried the following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextDrawable textDrawable = new TextDrawable(" ** Bienvenido ** ");
LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.layerdraw, null);
ld.mutate();
ld.addLayer(textDrawable);
getWindow().setBackgroundDrawable(ld);
}
But the text does not appear. If there is a better method, it is welcome.
DO NOT NOTE that I am displaying this drawable
as the background temporarily, but it could also be within ImageWiew
.
Edit
I have researched a little more and I have discovered that it is probably not possible to do it directly with the layer-list
, however, I have seen that it is possible to do it by converting said layer-list into a bitmap and then superimpose the text on it, how I do this? How do I convert this drawable to bitmap ?. In any case I would count as a solution to this question, because I only want the text to be together as a background image.