android上有拉伸屏幕的麻烦

时间:2014-09-09 07:33:54

标签: java android libgdx

如何解决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]

1 个答案:

答案 0 :(得分:1)

您可能希望使用某种Viewport策略,可能是FitViewportExtendViewport

用法在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);
}