Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在主屏幕菜单(此处)中添加背景图像?_Android_Eclipse_Background_Libgdx - Fatal编程技术网

Android 如何在主屏幕菜单(此处)中添加背景图像?

Android 如何在主屏幕菜单(此处)中添加背景图像?,android,eclipse,background,libgdx,Android,Eclipse,Background,Libgdx,如何从我的资源中创建、绘制和设置背景图像 public class MainScreen extends AbstractScreen { private SpriteBatch batch; private Button exit, play; public MainScreen(Main main) { super(main); } public void show(){ batch= main.getBatch

如何从我的资源中创建、绘制和设置背景图像

public class MainScreen extends AbstractScreen {

    private SpriteBatch batch;
    private Button exit, play;

    public MainScreen(Main main) {
        super(main);

    }

    public void show(){
        batch= main.getBatch();
        Texture texture = new Texture(Gdx.files.internal("BotonExit.png"));
        int centroY = Gdx.graphics.getHeight() / 2 - texture.getHeight() / 2;
        int centroX = Gdx.graphics.getWidth() / 2 - texture.getWidth() / 2;
        exit = new ButtonExit(centroX, centroY - 50);
        play = new ButtonPlay(centroX, centroY + 50);


    }

    public void render(float delta){
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       
        exit.update(); 
        play.update();
        batch.begin();
        exit.draw(batch); 
        play.draw(batch); 
        batch.end();
    }
}

绘制背景非常接近,您应该将摄影机设置为填充所有背景。这可能会帮助您:

private SpriteBatch batch;
private Button exit, play;
private final OrthographicCamera camera;

public MainScreen(Main main) {
    super(main);
}

public void show(){

   batch= main.getBatch();

   Texture background = new Texture(Gdx.files.internal("background.png"));
   background.setSize(800,480);

   Texture texture = new Texture(Gdx.files.internal("BotonExit.png"));
   int centroY = Gdx.graphics.getHeight() / 2 - texture.getHeight() / 2;
   int centroX = Gdx.graphics.getWidth() / 2 - texture.getWidth() / 2;
   exit = new ButtonExit(centroX, centroY - 50);
   play = new ButtonPlay(centroX, centroY + 50);

   camera = new OrthographicCamera();
   camera.setToOrtho(false, 800, 480);


}

public void render(float delta){
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);       
    exit.update(); 
    play.update();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    background.draw(batch);
    exit.draw(batch); 
    play.draw(batch); 
    batch.end();
}

谢谢,但仍然不工作,当我把私人决赛,可以使用主屏幕,我不能使用方法设置大小,我不能画背景我不知道为什么,可能是因为我有另一个背景在另一个类或我不知道。有什么想法吗?尝试实现Screen而不是扩展AbstractScreen它将覆盖渲染方法