78

我想要一个带有粗体文本的位图图标来在地图上绘制它。我有一个片段可以在图像上写文字:

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)对我不起作用。我希望在位图上绘制的文本加粗。

4

3 回答 3

192

使用对象setTypeface上的方法Paint将字体设置为打开粗体样式的内容。

paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
于 2013-07-29T14:27:29.473 回答
9

对于自定义字体:

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

对于普通字体:

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

接着

paint.setTypeface(typeface);
于 2018-10-17T05:43:33.453 回答
0

Kotlin 语法为粗体文本设置 Paint

paint = Paint(Paint.ANTI_ALIAS_FLAG).also { it.typeface = Typeface.DEFAULT_BOLD }
于 2021-07-15T11:58:32.820 回答