Android ApiDemos示例解析(83):Graphics->Typefaces
程序员文章站
2022-07-16 08:28:37
...
本例介绍如何在Android中使用自定义字体,Android支持TureType字体,和Windows 支持的TrueType字体格式相同。
可以在Windows\Fonts 目录下 查看字体,比如将 Edwardian 字体拷贝到本例的assest\fonts目录下:
修改一下本例代码:
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); } }
推荐阅读
-
Android ApiDemos示例解析(19):App->Alarm->Alarm Controller
-
Android ApiDemos示例解析(19):App->Alarm->Alarm Controller
-
Android ApiDemos示例解析(20):App->Alarm->Alarm Service
-
Android ApiDemos示例解析(84):Graphics->UnicodeChart
-
Android ApiDemos示例解析(83):Graphics->Typefaces
-
Android ApiDemos示例解析(83):Graphics->Typefaces
-
Android ApiDemos示例解析(85):Graphics->Vertices
-
Android ApiDemos示例解析(21):App->Device Admin
-
Android ApiDemos示例解析(21):App->Device Admin
-
Android ApiDemos示例解析(84):Graphics->UnicodeChart