android - 如何在 OpenGL 上下文丢失后重新加载 libgdx 非托管纹理

标签 android libgdx

我正在通过网络下载图像并将它们作为图像 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

对于上面的代码 fragment ,在 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);
    }
}

但这无济于事 - 我仍然以卸载纹理和白色区域而不是图像结束。

这应该怎么做?我还没有找到任何代码来证明这一点!

最佳答案

添加我的解决方案作为引用。我现在向我的管理器注册 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);
    }

}

关于android - 如何在 OpenGL 上下文丢失后重新加载 libgdx 非托管纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803783/

相关文章:

java - TextureAtlas 的优势以及如何正确使用它

java - 是否可以在不启动相应应用程序的情况下在android中发送电子邮件?

java - 显示由相机API android拍摄的图像

java - 如何简化多个IF语句?

android-studio - LibGDX Gradle任务可以在jar中导出android项目

libgdx - 如何在Libgdx中绘制饼图

android - android中的 Material 动画

android - targetSdkVersion ="17"和 targetSdkVersion ="18"ListView 项目布局不同

java - libjpeg turbo android jpeg 压缩

git - 将Gradle-add项目重新集成到现有的Git中