如何在位图上绘制粗体文字?

时间:2013-07-29 14:22:17

标签: android bitmap android-canvas paint

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

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)对我不起作用。我希望Bitmap上绘制的文字能够加粗。

3 个答案:

答案 0 :(得分:143)

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

paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));

答案 1 :(得分:4)

对于自定义字体:

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

对于普通字体:

Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD);

然后

paint.setTypeface(typeface);

答案 2 :(得分:0)

Kotlin 语法为粗体文本设置 Paint

paint = Paint(Paint.ANTI_ALIAS_FLAG).also { it.typeface = Typeface.DEFAULT_BOLD }