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

发布于 2024-11-08 04:13:21 字数 89 浏览 6 评论 0原文

我想在油漆上绘制文字。如何使用自定义字体(ex Helvetica)和粗体来绘制它?我更愿意使用系统字体而不是从资产创建它。谢谢。

I want to draw a text to a paint. How to draw it with a custom font (ex Helvetica ) and bold also? I would preffer to use a system font and not create it from assets. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

も星光 2024-11-15 04:13:24

如果您已经使用了某种字体并且想要使用该字体的粗体版本,则可以执行此操作。

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

我使用了上面的答案,但这种修改对我来说是必要的 - 所以我想提一下

If you already have a font in use and want to use a bold version of that you can do this.

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

I used the answer above, but this modification was necessary for me - so just thought I'd mention it

妞丶爷亲个 2024-11-15 04:13:24

如果您想使用资源中的字体(Kotlin):

val textPaint = TextPaint()
textPaint.typeface = resources.getFont(R.font.font_name)

这可能与问题无关,但这就是我正在寻找的 - 也许有人也需要它。

If you want to use a font from resources (Kotlin):

val textPaint = TextPaint()
textPaint.typeface = resources.getFont(R.font.font_name)

This might not be related to the question, but this is what I was looking for - maybe somebody would need it too.

等你爱我 2024-11-15 04:13:24

自定义字体必须放置在 asset 文件夹中。

也许下面的代码可以帮助你

Paint p = new Paint();
//Set font
Typeface plain = Typeface.createFromAsset(context.getAssets(), "custom_font.ttf");
p.setTypeface(plain);

The custom font must be placed in the assets folder.

Maybe the following code can help you

Paint p = new Paint();
//Set font
Typeface plain = Typeface.createFromAsset(context.getAssets(), "custom_font.ttf");
p.setTypeface(plain);
杀手六號 2024-11-15 04:13:24

使用 FontUtils kotlin 对象

object FontUtils {

    private const val FONT_PATH_LATO_REGULAR = "lato_regular.ttf"

    fun getDefaultTypeface(context: Context): Typeface {
        return Typeface.createFromAsset(context.assets, FONT_PATH_LATO_REGULAR)
    }
}

,您可以将其用作:

paint.typeface = FontUtils.getDefaultTypeface(context)

with FontUtils kotlin object

object FontUtils {

    private const val FONT_PATH_LATO_REGULAR = "lato_regular.ttf"

    fun getDefaultTypeface(context: Context): Typeface {
        return Typeface.createFromAsset(context.assets, FONT_PATH_LATO_REGULAR)
    }
}

then you can use it as:

paint.typeface = FontUtils.getDefaultTypeface(context)
蘑菇王子 2024-11-15 04:13:23

如果您在 XML 中使用 Android 的新字体作为您的字体,那么要获取用于绘制的字体,您可以使用:

val customTypeface = ResourcesCompat.getFont(context, R.font.myfont)

或者如果您的最小 Android API >= 26

val customTypeface = resources.getFont(R.font.myfont)

那么将其应用到您的绘制对象:

mTextPaint.typeface = customTypeface

有关更多信息,请查看 < a href="https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code" rel="noreferrer">https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code

If you are using Android's new Fonts in XML for your fonts, then to get the typeface used for paint you can use:

val customTypeface = ResourcesCompat.getFont(context, R.font.myfont)

or if your min Android API >= 26

val customTypeface = resources.getFont(R.font.myfont)

Then to apply it to your paint object:

mTextPaint.typeface = customTypeface

For more info check out https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code

柠北森屋 2024-11-15 04:13:23

将其用于油漆类:

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

Use this for paint class:

 Paint paint = new Paint();
   paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));
紫罗兰の梦幻 2024-11-15 04:13:22

如果“自定义字体”是指作为资源提供的字体,则以下代码应该有效:

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

If by "custom font" you mean a font that you are supplying as an asset, the following code should work:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文