LibGDX:在 android 设备上缓慢加载纹理(继续调用垃圾收集器)

LibGDX: Slow loading Texture on android device (keep calling the garbage collector)

我正在构建一个需要在内存中包含 27 个动画的游戏,因为我需要在用户点击屏幕后立即显示它们。

当 运行 应用程序的桌面版本工作正常但 android 设备上的加载时间太长(超过 3 分钟)。

我可以看到在加载动画时垃圾收集器被调用了很多(但它们在完成后仍在工作。)

这是我用来加载动画的代码:

private static void loadLetterAnimation(String letter){
    Gdx.app.log("AssetLoader", "Loading letter animation for " + letter);
    Array currentLetterTextures = new Array();
    Array currentLetterTexturesRegions = new Array();
    FileHandle dirHandle = Gdx.files.internal("keys/animations/" + letter + "/");
    for (int j = 0; j < 30; j++) {
        Texture texture = new Texture(Gdx.files.internal("keys/animations/" + letter + "/" + j + ".png"));
        texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        TextureRegion textureRegion = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
        textureRegion.flip(false, true);
        currentLetterTextures.add(texture);
        currentLetterTexturesRegions.add(textureRegion);
    }
    Animation animation = new Animation(0.04f, currentLetterTexturesRegions);
    animation.setPlayMode(Animation.PlayMode.NORMAL);
    letterAnimationsTexturesMap.insert(letterAnimationsTexturesMap.size, letter, currentLetterTextures);
    letterAnimationsMap.insert(letterAnimationsMap.size, letter, animation);
}

日志是:

...
02-09 22:08:24.383  25272-25305/com.testing.android I/AssetLoader﹕ Loading letter animation for a
02-09 22:08:25.843  25272-25276/com.testing.android D/dalvikvm﹕ GC_CONCURRENT freed 373K, 16% free 9547K/11271K, paused 12ms+2ms, total 27ms
02-09 22:08:29.368  25272-25276/com.testing.android D/dalvikvm﹕ GC_CONCURRENT freed 405K, 16% free 9549K/11271K, paused 2ms+1ms, total 24ms
02-09 22:08:32.813  25272-25276/com.testing.android D/dalvikvm﹕ GC_CONCURRENT freed 400K, 16% free 9555K/11271K, paused 12ms+2ms, total 39ms
02-09 22:08:33.263  25272-25305/com.testing.android I/AssetLoader﹕ Loading letter animation for b
02-09 22:08:36.498  25272-25276/com.testing.android D/dalvikvm﹕ GC_CONCURRENT freed 397K, 16% free 9559K/11271K, paused 2ms+3ms, total 29ms
02-09 22:08:40.488  25272-25276/com.testing.android D/dalvikvm﹕ GC_CONCURRENT freed 454K, 16% free 9565K/11271K, paused 12ms+2ms, total 40ms
02-09 22:08:42.073  25272-25305/com.testing.android I/AssetLoader﹕ Loading    letter animation for c
02-09 22:08:45.323  25272-25276/com.testing.android D/dalvikvm﹕ GC_CONCURRENT freed 451K, 16% free 9567K/11271K, paused 12ms+2ms, total 39ms
...

有人可以告诉我我做错了什么吗?甚至可以做到吗?

将纹理打包成一个图集(或一组图集)。这将缩短加载时间,因为您将查找更少的文件、打开更少的文件、一次将更多数据上传到 GPU,并缩短 运行 时间(通过加载更少的纹理,导致更少的纹理切换).参见 https://github.com/libgdx/libgdx/wiki/Texture-packer

flip 操作也很昂贵。您应该能够提前将纹理设置为 "flipped"。

在这样做之前,可能值得使用一种堆分析工具来弄清楚您的内存和时间的实际去向。参见 http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html