Libgdx 从图像或可绘制对象中获取纹理

Libgdx get Texture from Image or Drawable

我想从图像中获取纹理数据,因此可以直接渲染它,但我看不到直接的制作方法,google 搜索只显示相反的方法。

我可以获取 Drawable,但是界面没有指定确切的纹理数据,所以我看不到将 Image actor 转换为 Texture 的方法。

我想要实现的是拥有画笔,其中画笔的数据存储在 ImageButton 的图像中。因此,单击 ImageButton 后,用户将能够根据存储在按钮中的图像在屏幕上绘图。

我怎样才能做到这一点?

深入挖掘后,我在 forums 上找到了解决方案:

Your best bet is probably to draw your textures to a FrameBuffer with a 1:1 scale, then you can use

ScreenUtils.getFrameBufferPixmap

To copy a pixmap off the FrameBuffer and resize as you wish.

Something like this:

    fb.begin();

   
    sb.begin();
    sb.draw(texture, 0, 0);
    sb.end();

    Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);

    fb.end();

它不完全是纹理,但它正是我所需要的:从可绘制对象中提取的像素数据。

经过深思熟虑,我决定将纹理数据冗余地存储在 Imagebutton 旁边,并直接查询存储的数据,而不是将 ImageButton 用作一种容器,它不应该是。