android如何在画布中设置自定义字体



hi我想在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的一些帖子,遇到了一些问题。

在"assets"文件夹下创建"font"文件夹。之后,把你的字体文件放在"字体"文件夹中,并写下下面的代码。

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

使用此:

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

使用下一个:

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

自定义视图中的完整设置文本:

    TextPaint textPaint = new TextPaint();
    Typeface tf =Typeface.createFromAsset(getContext().getAssets(),"fonts/BungeeSpice-Regular.ttf");
    textPaint.setTypeface(tf);
    textPaint.setStrokeWidth(7);
    textPaint.setTextSize(Tool.convertSpToPx(getContext(), 30));
    textPaint.setAntiAlias(true);
    textPaint.setPathEffect(null);
    textPaint.setColor(getResources().getColor(R.color.green, null));

相关内容

  • 没有找到相关文章

最新更新