我正在通过网络下载图像并将其作为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);
Run Code Online (Sandbox Code Playgroud)
鉴于此,我需要一些重新加载纹理数据的方法,因为它在OpenGL上下文丢失时丢失(例如因为屏幕进入休眠状态).
我已经尝试过制作自己的经理课程了
DynamicTextureManager.register(t, pm); // Register texture together with the source pixmap
Run Code Online (Sandbox Code Playgroud)
到上面的片段,在resume()我做:
DynamicTextureManager.reload();
Run Code Online (Sandbox Code Playgroud)
经理班:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但这没有用 - 我仍然最终将纹理卸载并使用白色区域而不是图像.
该怎么做?我无法找到任何证明这一点的代码!
添加我的解决方案作为参考。现在,我向管理器注册了 Image 对象和 Pixmap 对象,在 reload() 上,从 Pixmap 重新创建了纹理,并为旧图像设置了新纹理。对我有用,但欢迎更优雅的解决方案。
import java.util.Map.Entry;
public class DynamicTextureManager {
private static final class MapData {
Pixmap pixmap;
int width;
int height;
}
private static WeakHashMap<Image, MapData> theMap = new WeakHashMap<Image, MapData>();
public static void reload() {
Set<Entry<Image, MapData>> es = theMap.entrySet();
for (Entry<Image, MapData> e : es) {
Image i = e.getKey();
MapData d = e.getValue();
Texture t = new Texture(d.pixmap);
TextureRegion tr;
if(d.width == -1 || d.height == -1) {
tr = new TextureRegion(t);
}
else {
tr = new TextureRegion(t,d.width, d.height);
}
TextureRegionDrawable trd = new TextureRegionDrawable(tr);
i.setDrawable(trd);
}
}
public static void register(Image i, Pixmap p) {
MapData d = new MapData();
d.pixmap = p;
d.width = -1;
d.height = -1;
theMap.put(i, d);
}
public static void register(Image i, Pixmap p, int width, int height) {
MapData d = new MapData();
d.pixmap = p;
d.width = width;
d.height = height;
theMap.put(i, d);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3280 次 |
| 最近记录: |