需要旋转游戏中的大像素图(来自图库)。
我为此使用以下代码。
例如:
public class MyGdxGame extends ApplicationAdapter {
private int w = 720, h = 1280;
private Stage stage;
private Image bigImg, button, rotationIndicator;
private Pixmap pixmap0;
private Pixmap pixmap90;
private Texture textureBigImg;
private String buttonPath = "badlogic.jpg", bigImgPath = "DSC_0006.JPG"; //DSC_0006.JPG: 5504x3096
private Thread thread;
@Override
public void create () {
stage = new Stage(new FillViewport(w, h));
button = new Image(new Texture(buttonPath));
textureBigImg = new Texture(bigImgPath);
bigImg = new Image(textureBigImg);
rotationIndicator = new Image(new Texture(buttonPath));
rotationIndicator.setPosition(720-256,0);
button.setPosition(0,0);
bigImg.setBounds( 0,256, w, w*3096/5504f);
pixmap0 = new Pixmap(Gdx.files.internal(bigImgPath));
thread = new Thread(new Runnable() {
@Override
public void run() {
rotation();
}
});
button.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
super.clicked(event, x, y);
stage.addActor(rotationIndicator);
rotation(); //rotationIndicator draw only after the turn process.
//thread.start(); //I get a black screen.
}
});
stage.addActor(button);
stage.addActor(bigImg);
Gdx.input.setInputProcessor(stage);
}
private void rotation() {
pixmap90 = new Pixmap(pixmap0.getHeight(), pixmap0.getWidth(), pixmap0.getFormat());
for (int y = 0, a = 0; y<pixmap0.getWidth(); ++y, ++a) {
for (int x = 0, b = pixmap0.getHeight(); x<pixmap0.getHeight(); ++x, --b) {
pixmap90.drawPixel(x, y, pixmap0.getPixel(a, b));
}
}
textureBigImg.dispose();
textureBigImg = new Texture(pixmap90);
textureBigImg.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
bigImg.setDrawable(new TextureRegionDrawable(new TextureRegion(textureBigImg)));
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
@Override
public void dispose () {
pixmap0.dispose();
pixmap90.dispose();
textureBigImg.dispose();
}
}
问题在于大局太长了。从这个冻结程序。
我尝试在单独的线程中执行此操作,但是出现黑屏。
我试图显示转弯指示器,但仅在转弯过程之后才绘制。
现在,我正在尝试减少像素图,但是在较弱的设备上仍然冻结。
最佳答案
您不能创建新的Texture对象或在单独的线程中对其进行操作,因为它们是OpenGL对象。创建完旋转的像素图后,您的rotate()
方法应将可运行对象发送回GL线程以加载纹理。
因此,在rotate()
中的for循环之后获取所有代码,并将其包装在Runnable中。呼叫Gdx.app.postRunnable()
。