Libgdx从Gdx.files.external()加载纹理,显示为黑色矩形

时间:2017-03-06 15:21:37

标签: java android libgdx filesystems

我已经为用户添加了外部读写权限和所请求的权限。我已经使用libgdx文件系统生成了一个我想加载的图片文件句柄列表。我像这样加载它们:

image = new Texture(fh); //fh is the filehandle

当我渲染我尝试加载的任何图像时,只需渲染一个黑色矩形。我通常使用带有.draw方法的spritebatch来渲染它们。知道他们为什么渲染为黑色矩形吗?提前谢谢/

我的图片模块代码:

package com.ggi.uparty.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.ggi.uparty.screens.ImagePicker;

public class ImagePickerModule {

public ImagePicker p;

public FileHandle fh;

public Texture image = null;

public TextureRegion load,preview;


public float theta = 0;

public Rectangle bounds = new Rectangle();

public ImagePickerModule(ImagePicker p, final FileHandle fh){
    theta = 0;
    bounds.width=.85f*Gdx.graphics.getWidth()/6f;
    bounds.height=bounds.width;

    load = new TextureRegion(p.u.assets.get("UI/Load.png", Texture.class));

    Thread t = new Thread(new Runnable(){

        @Override
        public void run() {
            try{
            //image = new Texture(fh);

            image = new Texture(fh);
            System.out.println(fh.path());
            float sqSize = image.getWidth()<image.getHeight()?image.getWidth():image.getHeight();
            preview = new TextureRegion(image,bounds.x+bounds.width/2-sqSize/2,bounds.y+bounds.height/2-sqSize/2,sqSize,sqSize);
            }catch(Exception e){
                e.printStackTrace();
            }

        }

    });
    t.start();
}

public void draw(SpriteBatch pic,float fade){
    theta++;
    pic.setColor(1, 1, 1, fade);
    if(image == null){
        pic.draw(load, bounds.x+bounds.width / 2 - bounds.height / 4, bounds.y + bounds.height / 4+theta, bounds.height / 4,
                bounds.height / 4, bounds.height / 2, bounds.height / 2, 1, 1, -theta);
    }
    else{
        pic.draw(image,bounds.x,bounds.y+theta,bounds.width,bounds.height);
    }
}

}

1 个答案:

答案 0 :(得分:1)

您正在尝试在非ui线程上加载纹理。如果您想要异步加载纹理,请使用assetmanager。

要测试这一点,请尝试将Thread.start更改为thread.run(在您现在使用的线程中立即运行,因此禁用该线程)图像应该正常加载。

然后获得异步加载实现资产管理器:

https://github.com/libgdx/libgdx/wiki/Managing-your-assets