Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to put text on drawable programmatically

I have this icon: icon

I am going to work with it as drawable.

Drawable myIcon = getResources().getDrawable( R.drawable.icon );

I need programmaticaly put some text on it (file extension).

This is my desired result: desired result.

I can't make several static icons because I can receive arbitrary file extension

like image 336
Slavik Y Avatar asked Dec 01 '22 15:12

Slavik Y


1 Answers

public BitmapDrawable writeOnDrawable(int drawableId, String text){

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

        Paint paint = new Paint(); 
        paint.setStyle(Style.FILL);  
        paint.setColor(Color.BLACK); 
        paint.setTextSize(20); 

        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight()/2, paint);

        return new BitmapDrawable(bm);
    }

Use this method.will help you.

like image 116
Rishabh Mahatha Avatar answered Dec 04 '22 22:12

Rishabh Mahatha