在画布上绘制文本

3

我正在尝试使用画布(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);
1个回答

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);

好的,谢谢,它正在运行,你最棒了,但换行符 \n 没有起到换行作用。 - Isuru
这是因为你将它绘制在圆形路径上了。不要使用路径进行绘制,它应该可以正常工作。 - Zaid Daghestani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接