Java Can';在onActivityResult中加载纹理
这是核心项目:Java Can';在onActivityResult中加载纹理,java,android,libgdx,textures,onactivityresult,Java,Android,Libgdx,Textures,Onactivityresult,这是核心项目: public class GameClass extends Game { public CustomScreen customScreen; public CustomScreen currentScreen; public CustomInterface handler; public static int screenWidth; public static int screenHeight; public GameC
public class GameClass extends Game {
public CustomScreen customScreen;
public CustomScreen currentScreen;
public CustomInterface handler;
public static int screenWidth;
public static int screenHeight;
public GameClass(CustomInterface handler) {
this.handler = handler;
}
@Override
public void create () {
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
customScreen = new CustomScreen();
currentScreen = customScreen;
setScreen(currentScreen);
handler.PickFile();
}
public void onPickFile(String filePath) {
currentScreen.onPickFile(filePath);
}
}
public class CustomObject {
public Texture texture;
public CustomObject() {
this.texture = new Texture(Gdx.files.internal("badlogic.jpg"));
}
public void Display(SpriteBatch batcher) {
batcher.draw(texture, 0, 0, 500, 500);
}
}
public interface CustomInterface {
public void PickFile();
}
public class CustomScreen implements Screen {
Texture texture = new Texture(Gdx.files.internal("badlogic.jpg"));
SpriteBatch batcher;
OrthographicCamera cam;
CustomObject obj2;
public CustomScreen() {
cam = new OrthographicCamera();
cam.setToOrtho(true, GameClass.screenWidth, GameClass.screenHeight);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
}
public void onPickFile(String filePath) {
obj2 = new CustomObject();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batcher.begin();
if (obj2 != null)
obj2.Display(batcher);
batcher.end();
}
}
public class AndroidLauncher extends AndroidApplication implements CustomInterface {
GameClass game;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
game = new GameClass(this);
initialize(game, config);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
game.onPickFile("");
}
public void PickFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
}
这就是Android项目:
public class GameClass extends Game {
public CustomScreen customScreen;
public CustomScreen currentScreen;
public CustomInterface handler;
public static int screenWidth;
public static int screenHeight;
public GameClass(CustomInterface handler) {
this.handler = handler;
}
@Override
public void create () {
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
customScreen = new CustomScreen();
currentScreen = customScreen;
setScreen(currentScreen);
handler.PickFile();
}
public void onPickFile(String filePath) {
currentScreen.onPickFile(filePath);
}
}
public class CustomObject {
public Texture texture;
public CustomObject() {
this.texture = new Texture(Gdx.files.internal("badlogic.jpg"));
}
public void Display(SpriteBatch batcher) {
batcher.draw(texture, 0, 0, 500, 500);
}
}
public interface CustomInterface {
public void PickFile();
}
public class CustomScreen implements Screen {
Texture texture = new Texture(Gdx.files.internal("badlogic.jpg"));
SpriteBatch batcher;
OrthographicCamera cam;
CustomObject obj2;
public CustomScreen() {
cam = new OrthographicCamera();
cam.setToOrtho(true, GameClass.screenWidth, GameClass.screenHeight);
batcher = new SpriteBatch();
batcher.setProjectionMatrix(cam.combined);
}
public void onPickFile(String filePath) {
obj2 = new CustomObject();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batcher.begin();
if (obj2 != null)
obj2.Display(batcher);
batcher.end();
}
}
public class AndroidLauncher extends AndroidApplication implements CustomInterface {
GameClass game;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
game = new GameClass(this);
initialize(game, config);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
game.onPickFile("");
}
public void PickFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
}
调用CustomScreen类中的render函数时,在我从Android库中拾取一张图片后,它显示的不是我期望的badlogic.jpg纹理,而是黑色纹理
这是它显示的内容:
这就是它应该显示的内容:
(我知道Android项目中的部分代码是无用的,我基本上可以在核心项目中加载badlogic.jpg纹理,但最终的目的是加载从Android库中拾取的纹理,但正如您所看到的,我甚至还不能在资产文件夹中加载纹理)
我已经试着解决这个问题好几个月了,但我仍然不知道怎么做,请帮帮我 在渲染函数中,确实如此。我会把它显示的屏幕截图。它调用obj2.Display(batcher),它应该显示obj2.texture(aka badlogic.jpg)。一个是我看到的,另一个是我应该看到的代码>像这样
Gdx.app.postRunnable(new Runnable(){@Override public void run(){obj2=new CustomObject();}})代码>只是猜测OpenGL不喜欢在其他线程上加载纹理。(在本例中是Android UI线程而不是OpenGL线程)它可以工作!非常感谢你!