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

时间:2014-04-16 14:20:38

标签: java android

我使用android Paint类来创建文本。我知道如何设置文字大小和颜色。我想使用Arial作为字体大小和粗体。我怎么能用paint对象来做呢。我已经查看了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字体和粗体文本。

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:3)

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

答案 2 :(得分:0)

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