Libgdx 从 Gdx.files.external() 加载纹理显示为黑色矩形
Libgdx load texture from Gdx.files.external() showing as black rectangles
我已经添加了外部读写权限,并向用户请求了权限。我已经使用 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);
}
}
}
您正在尝试在非ui 线程上加载纹理。如果您想异步加载纹理,请使用 assetmanager。
要对此进行测试,请尝试将 Thread.start 更改为 thread.run(运行 立即在您现在所在的线程中,因此禁用该线程)图像应该可以正常加载。
然后获取异步加载实现资产管理器:
我已经添加了外部读写权限,并向用户请求了权限。我已经使用 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);
}
}
}
您正在尝试在非ui 线程上加载纹理。如果您想异步加载纹理,请使用 assetmanager。
要对此进行测试,请尝试将 Thread.start 更改为 thread.run(运行 立即在您现在所在的线程中,因此禁用该线程)图像应该可以正常加载。
然后获取异步加载实现资产管理器: