在画布上绘制文本并使其在屏幕上可见

发布于 2024-11-18 02:50:30 字数 767 浏览 4 评论 0原文

这段代码应该将文本转换为图像

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(16);
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.MONOSPACE);

        Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ALPHA_8);
        float x = bm.getWidth();
        float y = bm.getHeight();
        Canvas c = new Canvas(bm);
        c.drawText("Test", x, y, paint);
}

这段代码可以吗?如果是,我怎样才能使这个新的位图在屏幕上可见?我尝试了这段代码,它产生了一个错误

setContentView(c); //<- ERROR!

,我对元素 Canvas 感到困惑,因为 XML 中没有这样的元素,我可以在代码中使用。

This code was supposed to convert text to image

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        paint.setTextSize(16);
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.MONOSPACE);

        Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ALPHA_8);
        float x = bm.getWidth();
        float y = bm.getHeight();
        Canvas c = new Canvas(bm);
        c.drawText("Test", x, y, paint);
}

Is this code ok? If yes, how can I make this new bitmap visible on screen? I tried this code which produced an error

setContentView(c); //<- ERROR!

I am confused with the element Canvas as there is not such element in XML which I can use in the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-11-25 02:50:30

setContentView(View) 需要一个 ViewCanvas 不是 View

我不确定您是否想自己创建一个 Canvas 。不过,有多种方法可以获取从 Android 框架传递给您的 Canvas。实现此目的的一种方法是创建自定义View。为此,您需要创建一个扩展 View 的新类。

重写 View 类时,您将能够重写 onDraw(Canvas) 方法。这可能是您想要在您发布的代码中的 onCreate() 方法中执行的操作。

此链接很好地概述了创建您的自己的自定义视图。

setContentView(View) takes a View and Canvas is not a View.

I am not sure that you want to create a Canvas on your own. There are ways to get a Canvas passed to you from the Android Framework though. One way you can do this is by creating a custom View. To do this, you will need to create a new class that extends View.

When overriding a View class, you will have the ability to override the onDraw(Canvas) method. This is probably where you want to do what you are attempting to do in your onCreate() method in the code you posted.

This link gives a good overview of what is required to create your own custom view.

御弟哥哥 2024-11-25 02:50:30

第一:如果您在指定的 x 和 y 位置绘制文本,则会绘制它
在右下角,从该像素开始。您的画布上不会绘制任何内容。尝试 bm.getWidth()/2,高度与测试绘图相同。您可以稍后对其进行优化。

第二:Canvas不是View(不扩展View类)。您只能通过 set ContentView() 设置视图。我在这里建议编写一个仅包含单个 ImageView 的 XML 布局,并通过 setContentView(R.layout.mylayout) 进行设置。

之后,您可以使用 findViewById() 获取该 ImageView 并使用 ImageView.setImageBitmap(bm) 在其上显示您的位图。
使用位图创建画布后,您无需对画布执行任何操作。从那时起,您在画布内绘制的所有内容都会立即在位图中找到。
因此您不能在 XML 中指定 Canvas。可以这么说,它只是一个编辑图片的“编辑器”,而不是一个实际的 UI 元素。

First: If you draw your text at the x and y position you specified, you draw it
at the lower right corner, starting with exactly that pixel. Nothing will be drawn on your canvas. Try bm.getWidth()/2, for height the same for test drawing. You can optimize that later.

Second: Canvas is not a View (does not extend the View class). You can only set Views via set ContentView(). What I recommend here is writing a XML layout containing only a single ImageView and set that via setContentView(R.layout.mylayout).

After that, you can use findViewById() to grab that ImageView and use ImageView.setImageBitmap(bm) to show your bitmap on it.
You dont have to do anything with the canvas, once you created it with your bitmap. Everything you draw inside the canvas from that point on is found in the Bitmap immediately.
Therefore you can't specify the Canvas in XML. It's just an "Editor" to edit pictures, so to speak and not an actual UI element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文