Java android如何在画布中设置自定义字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20279084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How android set custom font in canvas?
提问by NadeemYousaf
hi i want to change my font size by using paint , canvas in android. My code is here. how can i do this ?
嗨,我想通过在 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);
}
}
can any body help me to solve problem ? i read some tutorials but not under stand . i have read some post of Stack ,facing some problems.
任何机构都可以帮助我解决问题吗?我读了一些教程,但不明白。我已经阅读了 Stack 的一些帖子,面临一些问题。
采纳答案by Zankhna
create "fonts" folder under "assets" folder. After that put your font file in "fonts" folder and write below code.
在“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);
回答by Shylendra Madda
Use this:
用这个:
Typeface tf = Typeface.createFromAsset(getAssets(),"RECOGNITION.ttf"); Paint paint = new Paint(); paint.setTypeface(tf); canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);
回答by JagrDev
Use the next:
使用下一个:
Paint paint = new Paint();
paint.setTypeface(tf);
paint.setTextSize(yourTextSize);
canvas.drawText("Lorem ipsum", 0, 0, paint);