简体   繁体   English

使用 Paint 对象在 android 中创建字体和文本样式

[英]Creating font and text styles in android with Paint object

I am using the android Paint class to create text.我正在使用 android Paint 类来创建文本。 I know how to set text size and color.我知道如何设置文字大小和颜色。 I want to use Arial as font size and bold.我想使用 Arial 作为字体大小和粗体。 How can I do it using the paint object.我怎样才能使用油漆对象做到这一点。 I have looked on the methods in the Paint class but couldn't find anything on how I can do it.我查看了 Paint 类中的方法,但找不到任何关于我如何做的信息。

This is how I create my text style.这就是我创建文本样式的方式。

// Defining a paint object
paint = new Paint();
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.LEFT);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);

Here is how I draw the text on a view.这是我在视图上绘制文本的方法。

g.drawString("My Text", 430, 774, paint);

How do I create the Arial font and bold text using the Paint class.如何使用 Paint 类创建 Arial 字体和粗体文本。

Use the TextPaint class instead of Paint.使用TextPaint类而不是 Paint。 And can be implemented as below并且可以实现如下

TextPaint textPaint = new TextPaint();
textPaint.setTextSize(30);
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.setColor(Color.WHITE);
textPaint.setTypeface(Typeface.create("Arial", Typeface.BOLD));

Januari 2020 2020 年一月

Copy the fonts you want to use to res/font (eg opensans_regular.ttf, opensans_italic.ttf, opensans_bolditalic.ttf, etc.) Watch out no '-' of capitals in the name!将您要使用的字体复制到 res/font(例如 opensans_regular.ttf、opensans_italic.ttf、opensans_bolditalic.ttf 等)。注意名称中的大写字母“-”!

Create New Font resource file opensans.xml创建新字体资源文件 opensans.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family
     xmlns:app="http://schemas.android.com/apk/res-auto">

    <font
        app:fontStyle="normal"
        app:fontWeight="400"
        app:font="@font/opensans_regular" />

    <font
        app:fontStyle="italic"
        app:fontWeight="400"
        app:font="@font/opensans_italic" />

    <font
        app:fontStyle="normal"
        app:fontWeight="700"
        app:font="@font/opensans_bold" />

    <font
        app:fontStyle="italic"
        app:fontWeight="700"
        app:font="@font/opensans_bolditalic" />

    <font
        app:fontStyle="normal"
        app:fontWeight="200"
        app:font="@font/opensans_light" />

    <font
        app:fontStyle="italic"
        app:fontWeight="200"
        app:font="@font/opensans_lightitalic" />

    <font
        app:fontStyle="normal"
        app:fontWeight="800"
        app:font="@font/opensans_extrabold" />

    <font
        app:fontStyle="italic"
        app:fontWeight="800"
        app:font="@font/opensans_extrabolditalic" />

</font-family>

In your MainActivity.java you can use the following code在您的 MainActivity.java 中,您可以使用以下代码

    Paint paint = new Paint();
    Typeface typeface;

    if (Build.VERSION.SDK_INT >= 28) {
        // This does only works from SDK 28 and higher
        Typeface typefaceA = ResourcesCompat.getFont(this, R.font.opensans);
        typeface = Typeface.create(typefaceA, 700, false);
    } else {
        // This always works (Whole name without .ttf)
        typeface = ResourcesCompat.getFont(this, R.font.opensans_bolditalic);
    }
    paint.setTypeface(typeface);
private Bitmap getFontBitmap(String text, int color, float fontSizeSP, int typefaceStyle, boolean isCustomFont) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    if (isCustomFont) {
        Typeface typeface = Typeface.create(Typeface.createFromAsset(mContext.getAssets(), "fonts/bangla/bensen_handwriting.ttf"), typefaceStyle);
        paint.setTypeface(typeface);
    } else {
        Typeface typeface = Typeface.create(Typeface.DEFAULT, typefaceStyle);
        paint.setTypeface(typeface);
    }
    int fontSizePX = ConvertDptoPx(mContext, fontSizeSP);
    int pad = (fontSizePX / 9);
    paint.setColor(color);
    paint.setTextSize(fontSizePX);

    int textWidth = (int) (paint.measureText(text) + pad * 2);
    int height = (int) (fontSizePX / 0.75);
    Bitmap bitmap = Bitmap.createBitmap(textWidth, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    float xOriginal = pad;
    canvas.drawText(text, xOriginal, fontSizePX, paint);
    return bitmap;
}

private int ConvertDptoPx(Context context, float dip) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, context.getResources().getDisplayMetrics());
}

And then call然后打电话

Bitmap bitmap = getFontBitmap("Ahamadullah", color, 22, Typeface.BOLD, true);

or或者

Bitmap bitmap = getFontBitmap("Ahamadullah", color, 22, Typeface.NORMAL | Typeface.ITALIC, false);

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

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