在画布上绘制文本

Isu*_*uru 3 animation android draw android-canvas

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

在此输入图像描述

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

        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);
Run Code Online (Sandbox Code Playgroud)

Zai*_*ani 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);
Run Code Online (Sandbox Code Playgroud)

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

canvas.drawTextOnPath("There are\n137 days, 9 Hour\n4 Minutes and 36 seconds\nuntil Christmas", circle, 0,30,paint);
Run Code Online (Sandbox Code Playgroud)