我们可以在绘图中添加文字吗? [英] Can we add text to a drawable?

查看:51
本文介绍了我们可以在绘图中添加文字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个看起来像这样的可绘制对象.

I want to create a drawable which looks like this.

我知道如何创建图像背景,但是即使经过搜索,我仍然找不到如何为该可绘制文件提供文本.有没有办法使可绘制文件与包含文本的文件完全相同?

I know how to create the image background but even after searching, I am unable to find how to give text to that drawable file. Is there any way that the drawable file is exactly same containing the text as well?

我不能使用textview,因为它是一个选项卡,并且我需要将drawable设置为背景资源(这就是我所知道的,如果我错了,请更正我.).所以我想要一个可以设置为背景的可绘制文件.

I cant use textview because its a tab and I need drawables to set as background resource (thats what I know, please correct me if I am wrong.). So I want a drawable file which I can set as background.

推荐答案

是的,您可以这样做.

查看帖子-

如何将文本放入可绘制对象中?

基本上,您必须扩展类 Drawable 并设置画布以绘制文本到可绘制对象.

Basically, you have to extend the class Drawable and set the canvas to draw the text to a drawable.

当您覆盖 draw 方法时,它将占据画布并在定义的位置绘制文本.

As you override the draw method, it will take the canvas and draw the text on defined locations.

画布有很多可用的方法.

如图形文档中所述.-

您可以使用Canvas类自己的一组绘制方法,如drawBitmap(...),drawRect(...),drawText(...)等.您可能使用的其他类也具有draw()方法.为了例如,您可能想要一些可绘制对象穿上帆布.Drawable有其自己的draw()方法,可将您的画布作为参数.

The Canvas class has its own set of drawing methods that you can use, like drawBitmap(...), drawRect(...), drawText(...), and many more. Other classes that you might use also have draw() methods. For example, you'll probably have some Drawable objects that you want to put on the Canvas. Drawable has its own draw() method that takes your Canvas as an argument.

绘图文字将如下所示-

canvas.drawText("Front Screen Torch", 30, 48, paint);

要直接从资源中获取实际颜色,请使用-

To get the actual color directly from resources use -

paint.setColor(getResources().getColor(R.color.black));

有关更多信息,请参见画布.

See Canvas for more.

这篇关于我们可以在绘图中添加文字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆