在画布上绘制文本

时间:2012-06-03 21:06:22

标签: android animation android-canvas draw

我正在尝试使用画布绘制文字。我到处检查过,但这些例子相当复杂,我可以在画布上画文字,但它不会像这张照片一样显示。

enter image description here

我发现这个代码并且它正常工作,我只需要像上面的图像一样写。

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(30);
        paint.setAntiAlias(true);

        canvas.drawText("There are 137 days, 9 hours 4 minutes and 36 seconds", 150,150, paint);

1 个答案:

答案 0 :(得分:6)

获取您想要的字体并将其添加到您的资源文件夹中。可以说字体文件名是“pretty.otf”。然后在你的代码中,你所要做的就是。

Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(30);
paint.setAntiAlias(true);

Context mContext = getContext();
Typeface myTypeface = Typeface.createFromAssets(mContext.getAssets(), "pretty.otf");

paint.setTypeface(myTypeface);

要像在图片中那样分隔文字,请在字符串中添加\ n字符添加新行,如下所示:

canvas.drawTextOnPath("There are\n137 days, 9 Hour\n4 Minutes and 36 seconds\nuntil Christmas", circle, 0,30,paint);
相关问题