【问题标题】:How android set custom font in canvas?android如何在画布中设置自定义字体?
【发布时间】:2013-12-15 06:08:56
【问题描述】:

嗨,我想通过在 android 中使用paint、canvas 来更改我的字体大小。我的代码在这里。 我该怎么做?

public class MainActivity extends Activity 

{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
Canvas canvas = new Canvas();
Typeface tf = Typeface.createFromAsset(getAssets(), "RECOGNITION.ttf");
     Paint paint = new Paint();
     paint.setTypeface(tf);
     canvas.drawText("Lorem ipsum", 0, 0, paint);


}
}

任何机构可以帮助我解决问题吗? 我读了一些教程,但不理解。 我读过 Stack 的一些帖子,遇到了一些问题。

【问题讨论】:

标签: java android fonts styles


【解决方案1】:

在“assets”文件夹下创建“fonts”文件夹。之后将你的字体文件放在“fonts”文件夹中并编写下面的代码。

   Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/YOURFONT.ttf");
   Paint paint = new Paint();
   paint.setTypeface(tf);
   canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);

【讨论】:

    【解决方案2】:

    使用这个:

    字体 tf = Typeface.createFromAsset(getAssets(),"RECOGNITION.ttf"); 油漆油漆 = 新油漆(); 油漆.setTypeface(tf); canvas.drawText("粗体识别示例文本",0,0,paint);

    【讨论】:

      【解决方案3】:

      使用下一个:

       Paint paint = new Paint();
       paint.setTypeface(tf);
       paint.setTextSize(yourTextSize);
       canvas.drawText("Lorem ipsum", 0, 0, paint);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-20
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-01
        • 2011-04-15
        • 2013-06-07
        相关资源
        最近更新 更多