android将自定义字体设置为一个油漆

我想绘制一个文本到油漆。 如何使用自定义字体( 前Helvetica )和粗体绘制? 我会主张使用系统字体,而不是从资产创build。 谢谢。

如果通过“自定义字体”,你的意思是你提供的一种字体作为一种资产,下面的代码应该工作:

 Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD) Paint paint = new Paint(); paint.setTypeface(bold); canvas.drawText("Sample text in bold",0,0,paint); 

如果你已经有一个字体在使用,并想使用一个粗体版本,你可以这样做。

 currentPainter = new Paint(Paint.ANTI_ALIAS_FLAG); currentPainter.setColor(Color.WHITE); currentPainter.setTextSize(Utils.sp2px(getResources(), 14)); // set font size Typeface currentTypeFace = currentPainter.getTypeface(); Typeface bold = Typeface.create(currentTypeFace, Typeface.BOLD); currentPainter.setTypeface(bold); 

我使用了上面的答案,但是这个修改对我来说是必要的 – 所以我想我会提到它

使用这个油漆类:

  Paint paint = new Paint(); paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));