和引擎背景图像

时间:2015-02-05 05:25:06

标签: android opengl-es opengl-es-2.0 andengine

最近我开始研究AndEngine。

我正在尝试设置场景的背景图片。

但由于某种原因,背景图像没有完全填满屏幕。

有任何建议和帮助吗?

这是我的源代码:

public class MainActivity extends BaseGameActivity {

Scene sc;

protected final static int cam_wid = 320;

protected final static int cam_heigh = 456;

BitmapTextureAtlas texAtlas;

    ITextureRegion texRegion;

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub
        Camera ca = new Camera(0, 0, cam_wid, cam_heigh);
        EngineOptions en = new EngineOptions(true,
                ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                        cam_wid, cam_heigh), ca);
        return en;
    }

    @Override
    public void onCreateResources(
            OnCreateResourcesCallback pOnCreateResourcesCallback)
            throws Exception {
        // TODO Auto-generated method stub
        load();
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    private void load() {
        // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        texAtlas = new BitmapTextureAtlas(getTextureManager(), 256, 512);
        texRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
                texAtlas, this, "imagery.png", 0, 0);
        texAtlas.load();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
        // TODO Auto-generated method stub
        this.sc = new Scene();
        Sprite s = new Sprite(0, 0, cam_wid, cam_heigh, texRegion,
                this.mEngine.getVertexBufferObjectManager());
        SpriteBackground sb = new SpriteBackground(s);
        sc.setBackground(sb);
        pOnCreateSceneCallback.onCreateSceneFinished(sc);
    }

    @Override
    public void onPopulateScene(Scene pScene,
            OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
        // TODO Auto-generated method stub
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }

}

1 个答案:

答案 0 :(得分:0)

获取设备宽度和高度,如下所示

DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
cam_wid =dm.widthPixels;
cam_heigh =dm.heightPixels;

另一件事您的背景图片可能比屏幕更小(高度更低)。 因此,无论是缩放图片还是重复制作。在最后一种情况下,您可以将纹理图集设置为TextureOptions.REPEATING_BILINEAR,然后使精灵更大。