【问题标题】:And Engine background image和引擎背景图片
【发布时间】:2015-02-05 05:25:06
【问题描述】:

最近我开始研究 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();
    }

}

【问题讨论】:

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


    【解决方案1】:

    像下面这样获取设备的宽度和高度并使用它

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

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

    【讨论】:

    • 我仍然无法找出问题所在。我尝试根据手机调整图像的宽度和高度值。
    • @user3624963 请使用我的代码获取相机的宽度和高度
    • @user3624963 你的图片尺寸是多少?
    • width=256 和 height=512
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2022-01-23
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多