繁体   English   中英

android如何在canvas中设置自定义字体?

[英]How android set custom font in canvas?

您好,我想通过在 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”文件夹下创建“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);

用这个:

Typeface tf = Typeface.createFromAsset(getAssets(),"RECOGNITION.ttf");
   Paint paint = new Paint();
   paint.setTypeface(tf);
   canvas.drawText("Sample text in bold RECOGNITION",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));

使用下一个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM