[英]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.