簡體   English   中英

使用 Paint 對象在 android 中創建字體和文本樣式

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

我正在使用 android Paint 類來創建文本。 我知道如何設置文字大小和顏色。 我想使用 Arial 作為字體大小和粗體。 我怎樣才能使用油漆對象做到這一點。 我查看了 Paint 類中的方法,但找不到任何關於我如何做的信息。

這就是我創建文本樣式的方式。

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

這是我在視圖上繪制文本的方法。

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

如何使用 Paint 類創建 Arial 字體和粗體文本。

使用TextPaint類而不是 Paint。 並且可以實現如下

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

2020 年一月

將您要使用的字體復制到 res/font(例如 opensans_regular.ttf、opensans_italic.ttf、opensans_bolditalic.ttf 等)。注意名稱中的大寫字母“-”!

創建新字體資源文件 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>

在您的 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());
}

然后打電話

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

或者

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