Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw bold text on a Bitmap?

I want a Bitmap icon with bold text to draw it on map. I have a snippet to write text on image:

Bitmap icon = BitmapFactory.decodeResource(PropertyMapList.this.getResources(),         R.drawable.location_mark); TextPaint paint = new TextPaint(); paint.setColor(Color.BLACK); paint.setTextSize(14); paint.setFakeBoldText(true); //paint.setTextAlign(Align.CENTER); Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);  Canvas canvas = new Canvas(copy); //canvas.drawText(jsonObj.getString("district_name"), 5, canvas.getHeight()/2, paint); String districtName = jsonObj.getString("district_name"); StaticLayout layout = new StaticLayout((districtName.length()>25 ? districtName.substring(0, 24)+"..":districtName)+"\n"+jsonObj.getString("total_properties"), paint, canvas.getWidth()-10,Layout.Alignment.ALIGN_CENTER, 1.3f, 0, false); canvas.translate(5, canvas.getHeight()/2); //position the text layout.draw(canvas); 

setFakeBoldText(true) doesn't work for me. I would like the text drawn on the Bitmap to be bolded.

like image 831
Sampath Kumar Avatar asked Jul 29 '13 14:07

Sampath Kumar


People also ask

How do I put text on a bitmap?

First we read bitmap from resources. This bitmap is immutable, so we create mutable copy. Then we create Paint object and set them text size, color, shadow, etc. Now we are ready to draw!

How do you create bold text?

Type the keyboard shortcut: CTRL+B.

How do I make text bold in paint?

Use the setTypeface method on your Paint object to set the font to something with the bold style turned on. setTypeface allows you to set a font. Fonts have styles, like bold, italic, etc.


2 Answers

Use the setTypeface method on your Paint object to set the font to something with the bold style turned on.

paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); 
like image 197
Gabe Sechan Avatar answered Sep 22 '22 19:09

Gabe Sechan


For Custom Fonts:

Typeface typeface = Typeface.create(Typeface.createFromAsset(mContext.getAssets(), "fonts/bangla/bensen_handwriting.ttf"), Typeface.BOLD); 

For Normal Fonts:

Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD); // or Typeface typeface = Typeface.DEFAULT_BOLD; 

and then

paint.setTypeface(typeface); 
like image 36
Ahamadullah Saikat Avatar answered Sep 18 '22 19:09

Ahamadullah Saikat