Java android上的屏幕拉伸问题

Java android上的屏幕拉伸问题,java,android,libgdx,Java,Android,Libgdx,如何解决straching的问题,我希望android的所有屏幕上都有相同的图片和位置。在那个时候,我做了这样的事情,但并不是很成功,我该如何改进或解决这个问题呢?我会非常高兴,谢谢你们的帮助 [code] @Override public void resize(int width, int height) { this.width = width; this.height = height; if (height == 480 &am

如何解决straching的问题,我希望android的所有屏幕上都有相同的图片和位置。在那个时候,我做了这样的事情,但并不是很成功,我该如何改进或解决这个问题呢?我会非常高兴,谢谢你们的帮助

[code]  @Override
    public void resize(int width, int height) {
        this.width = width;
        this.height = height;
        if (height == 480 && width == 320) {
            cam = new OrthographicCamera( 466,700);
            this.width = 466;
            this.height = 700;
        } else if (height == 320 && width == 240) {
            cam = new OrthographicCamera( 525,700);
            this.width = 525;
            this.height = 700;
        } else if (height == 400 && width == 240) {
            cam = new OrthographicCamera( 480,800);
            this.width = 480;
            this.height = 800;
        } else if (height == 432 &&width == 240) {
            cam = new OrthographicCamera( 389,700);
            this.width = 389;
            this.height = 700;
        } else if (height == 960 && width == 640) {
            cam = new OrthographicCamera( 533,800);
            this.width = 533;
            this.height = 800;
        } else if (height == 1366 && width == 768) {
            cam = new OrthographicCamera( 720,1280);
            this.width = 720;
            this.height =1280 ;
        } else if (height == 1366 && width == 720) {
            cam = new OrthographicCamera(675,1280);
            this.width = 675;
            this.height = 1280;
        } else if (height== 1536 && width == 1152) {
            cam = new OrthographicCamera( 1024,1366);
            this.width =  1024;
            this.height =1366;
        } else if (height == 1920 && width == 1152) {
            cam = new OrthographicCamera( 854,1366);
            this.width = 854;
            this.height =1366 ;
        } else if (height == 1920 && width == 1200) {
            cam = new OrthographicCamera( 800,1366);
            this.width =  800;
            this.height =1280;
        } else if (height > 1280) {
            cam = new OrthographicCamera( 768,1280);
            this.width =  768;
            this.height =1280;
        } else if (height < 800) {
            cam = new OrthographicCamera( 480,800);
            this.width = 480;
            this.height = 800;
        } else {
            cam = new OrthographicCamera(width, height);
        }
        cam.position.x = 240;
        cam.position.y = 400;
        cam.update();
        // backgroundFX.resize(width, height);
        titleBatch.getProjectionMatrix().set(cam.combined);

        musicOnOff.setPosition(20 - ((this.width - 480) / 2),
                10 - ((this.height - 800) / 2));
        help.setPosition(75 - ((this.width - 480) / 2),
                10 - ((this.height - 800) / 2));
        settings.setPosition(135 - ((this.width - 480) / 2),        8 - ((this.height - 800) / 2));


        p1.setSize(256-((this.width - 480) / 2), 256- ((this.height - 800) / 2));
        p2.setSize(256-((this.width - 480) / 2), 256- ((this.height - 800) / 2));
        p3.setSize(256-((this.width - 480) / 2), 256- ((this.height - 800) / 2));
        back.setPosition(100- ((this.width - 480) / 2) ,600 - ((this.height - 800) / 2));
        next.setPosition(400- ((this.width - 480) / 2) , 600 - ((this.height - 800) / 2));
        collisionMusic.set(
                new Vector3(musicOnOff.getVertices()[0], musicOnOff
                        .getVertices()[1], -10),
                new Vector3(musicOnOff.getVertices()[10], musicOnOff
                        .getVertices()[11], 10));
        collisionHelp.set(new Vector3(help.getVertices()[0],
                help.getVertices()[1], -10), new Vector3(
                help.getVertices()[10], help.getVertices()[11], 10));
        collisionSettings.set(
                new Vector3(settings.getVertices()[0],
                        settings.getVertices()[1], -10),
                new Vector3(settings.getVertices()[10],
                        settings.getVertices()[11], 10));

        collisionBack.set(
                new Vector3(back.getVertices()[0],
                        back.getVertices()[1], -10),
                new Vector3(back.getVertices()[10],
                        back.getVertices()[11], 10));

        collisionNext.set(
                new Vector3(next.getVertices()[0],
                        next.getVertices()[1], -10),
                new Vector3(next.getVertices()[10],
                        next.getVertices()[11], 10));


    }[/code]
[code]@覆盖
公共空心调整大小(整型宽度、整型高度){
这个。宽度=宽度;
高度=高度;
如果(高度==480和宽度==320){
cam=新的正交摄影机(466700);
这个宽度=466;
这个高度=700;
}否则如果(高度==320和宽度==240){
cam=新的正交摄影机(525700);
这个宽度=525;
这个高度=700;
}否则如果(高度==400和宽度==240){
cam=新的正交摄影机(480800);
这个宽度=480;
这个高度=800;
}否则如果(高度==432和宽度==240){
cam=新型正交摄影机(389700);
这个宽度=389;
这个高度=700;
}否则如果(高度==960和宽度==640){
cam=新型正交摄影机(533800);
该宽度=533;
这个高度=800;
}否则如果(高度==1366和宽度==768){
cam=新的正交摄影机(7201280);
这个宽度=720;
这个高度=1280;
}否则如果(高度==1366和宽度==720){
cam=新的正交摄影机(6751280);
该宽度=675;
这个高度=1280;
}否则如果(高度==1536和宽度==1152){
cam=新的正交摄影机(10241366);
这个宽度=1024;
这个高度=1366;
}否则如果(高度==1920和宽度==1152){
cam=新的正交摄影机(8541366);
该宽度=854;
这个高度=1366;
}否则,如果(高度==1920和宽度==1200){
cam=新型正交摄影机(8001366);
这个宽度=800;
这个高度=1280;
}否则,如果(高度>1280){
cam=新的正交摄影机(7681280);
这个宽度=768;
这个高度=1280;
}否则,如果(高度<800){
cam=新的正交摄影机(480800);
这个宽度=480;
这个高度=800;
}否则{
cam=新的正交摄影机(宽度、高度);
}
凸轮位置x=240;
凸轮位置y=400;
cam.update();
//backgroundFX.调整大小(宽度、高度);
titleBatch.getProjectionMatrix().set(cam.combined);
musicOnOff.设置位置(20-((此宽度-480)/2),
10-((此高度-800)/2));
帮助。设置位置(75-((this.width-480)/2),
10-((此高度-800)/2));
设置。设置位置(135-((this.width-480)/2),8-((this.height-800)/2);
p1.设置大小(256-((this.width-480)/2),256-((this.height-800)/2);
p2.设置大小(256-((this.width-480)/2),256-((this.height-800)/2);
p3.设置大小(256-((this.width-480)/2),256-((this.height-800)/2);
back.setPosition(100-((this.width-480)/2),600-((this.height-800)/2);
接下来,设置位置(400-((this.width-480)/2),600-((this.height-800)/2);
碰撞音乐(
新矢量3(musicOnOff.getVertices()[0],musicOnOff
.getVertices()[1],-10),
新矢量3(musicOnOff.getVertices()[10],musicOnOff
.getVertices()[11],10];
collisionHelp.set(新向量3(help.getVertices()[0],
help.getVertices()[1],-10),新矢量3(
help.getVertices()[10],help.getVertices()[11],10];
collisionSettings.set(
新矢量3(settings.getVertices()[0],
settings.getVertices()[1],-10),
新矢量3(settings.getVertices()[10],
settings.getVertices()[11],10]);
冲突返回(
新向量3(back.getVertices()[0],
back.getVertices()[1],-10),
新向量3(back.getVertices()[10],
back.getVertices()[11],10]);
collisionNext.set(
新向量3(next.getVertices()[0],
next.getVertices()[1],-10),
新向量3(next.getVertices()[10],
next.getVertices()[11],10]);
}[/code]

您可能需要使用一些策略,可能是
FitViewport
ExtendViewport

wiki中解释了该用法

private Viewport viewport;
private Camera camera;

public void create() {
    camera = new PerspectiveCamera();
    viewport = new FitViewport(800, 480, camera);
}

public void resize(int width, int height) {
    viewport.update(width, height);
}