本例介紹如何在Android中使用自定義字體,Android支持TureType字體,和Windows 支持的TrueType字體格式相同。
可以在Windows\Fonts 目錄下 查看字體,比如將 Edwardian 字體拷貝到本例的assest\fonts目錄下:
修改一下本例代碼:
[java]
private static class SampleView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Typeface mFace;
private Typeface mFace1;
public SampleView(Context context) {
super(context);
mFace = Typeface.createFromAsset(getContext().getAssets(),
"fonts/samplefont.ttf");
mFace1 = Typeface.createFromAsset(getContext().getAssets(),
"fonts/edwardian.ttf");
mPaint.setTextSize(64);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
mPaint.setTypeface(null);
canvas.drawText("Default", 10, 100, mPaint);
mPaint.setTypeface(mFace);
canvas.drawText("Custom", 10, 200, mPaint);
mPaint.setTypeface(mFace1);
canvas.drawText("Edwardian", 10, 300, mPaint);
}
}
private static class SampleView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Typeface mFace;
private Typeface mFace1;
public SampleView(Context context) {
super(context);
mFace = Typeface.createFromAsset(getContext().getAssets(),
"fonts/samplefont.ttf");
mFace1 = Typeface.createFromAsset(getContext().getAssets(),
"fonts/edwardian.ttf");
mPaint.setTextSize(64);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
mPaint.setTypeface(null);
canvas.drawText("Default", 10, 100, mPaint);
mPaint.setTypeface(mFace);
canvas.drawText("Custom", 10, 200, mPaint);
mPaint.setTypeface(mFace1);www.aiwalls.com
canvas.drawText("Edwardian", 10, 300, mPaint);
}
}
作者:mapdigit