【发布时间】:2013-02-10 23:17:34
【问题描述】:
我正在通过网络下载图像并将它们作为 Image Actor 添加到我的 libgdx UI 中:
Pixmap pm = new Pixmap(data, 0, data.length);
Texture t = new Texture(pm);
TextureRegion tr = new TextureRegion(t,200,300);
TextureRegionDrawable trd = new TextureRegionDrawable(tr);
Image icon = new Image();
icon.setDrawable(trd);
鉴于此,我需要某种方式重新加载纹理数据,因为当 OpenGL 上下文丢失时(例如,因为屏幕进入睡眠状态),纹理数据也会丢失。
我尝试制作自己的经理类,添加
DynamicTextureManager.register(t, pm); // Register texture together with the source pixmap
到上面的sn-p,在resume()我做:
DynamicTextureManager.reload();
经理类:
public class DynamicTextureManager {
private static LinkedHashMap<Texture, Pixmap> theMap = new
LinkedHashMap<Texture,Pixmap>();
public static void reload() {
Set<Entry<Texture,Pixmap>> es = theMap.entrySet();
for(Entry<Texture,Pixmap> e : es) {
Texture t = e.getKey();
Pixmap p = e.getValue();
t.draw(p, 0, 0);
}
}
public static void register(Texture t, Pixmap p) {
theMap.put(t, p);
}
}
但这无济于事 - 我最终仍然会卸载纹理和白色区域而不是图像。
这应该怎么做?我找不到任何代码来证明这一点!
【问题讨论】:
-
我的猜测是
Texture不会神奇地重新创建底层纹理句柄。尝试重新创建Texture本身,或者切换到所谓的托管 纹理数据(检查TextureData的子类;PixmapTextureData不支持这个概念。 -
我有一个与此非常相似的任务。我还没有完成,但如果我让它工作(我认为这将涉及从二进制数据重建 Pixmap)我会用答案更新它。
-
@Jyro117 我添加了我当前的解决方案作为答案,请随时改进!
-
啊,太好了,感谢您添加解决方案。我一定会在接下来的几天里更详细地查看它!