【问题标题】:How do I draw bold text on a Bitmap?如何在位图上绘制粗体文本?
【发布时间】:2013-07-29 09:21:10
【问题描述】:

我想要一个带有粗体文本的位图图标来在地图上绘制它。我有一个 sn-p 在图像上写文字:

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 上绘制的文本加粗。

【问题讨论】:

    标签: android bitmap android-canvas paint


    【解决方案1】:

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

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

    【讨论】:

    • setTypeface 允许您设置字体。字体有样式,如粗体、斜体等。您可以查看 Typeface 的构造函数,了解如何创建具有该样式的字体。一旦你创建了一个并通过这个调用设置它,所有未来的绘制命令都将使用这个字体。
    • 我尝试了 6 或 7 个答案,而这一个是唯一一个真正有效的。
    • 更短的版本是:paint.setTypeface(Typeface.DEFAULT_BOLD) 而不是调用 Typeface.create()
    【解决方案2】:

    对于自定义字体:

    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);
    

    【讨论】:

      【解决方案3】:

      Kotlin 语法为粗体文本设置 Paint

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        相关资源
        最近更新 更多