无法在AndEngine中设置场景的背景图像?

时间:2014-07-05 18:22:28

标签: android andengine live-wallpaper

我正在尝试在壁纸活动中将图像添加到背景中。但我无法这样做,有人可以提出一些方法来做,这是我的代码:

  

menuBackgroundTexture = new BitmapTextureAtlas(getTextureManager(),   2 * CAMERA_WIDTH,2 * CAMERA_HEIGHT,TextureOptions.DEFAULT);

     

menuBgTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.menuBackgroundTexture,   这个," land.png",0,0);

我尝试使用

之后
  

this.menuBackgroundTexture.load();   但它不起作用,我也试过以下代码   SpriteBackground bg = new SpriteBackground(new Sprite(0,0,   menuBgTexture,this.getVertexBufferObjectManager()));

     

mScene.setBackground(BG);

这也行不通,请帮帮我。

2 个答案:

答案 0 :(得分:0)

我终于找到了办法。以下是相同的代码:

  

final BitmapTexture backgroundTexture = new   BitmapTexture(this.getTextureManager(),new IInputStreamOpener(){               @覆盖               public InputStream open()抛出IOException {

            return getAssets().open("gfx/background1.jpg");
        }
    });
    backgroundTexture.load();
    backgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture); final
     

Sprite background = new Sprite(CAMERA_WIDTH / 2f -   backgroundTextureRegion.getWidth()/ 2f,CAMERA_HEIGHT / 2f -   backgroundTextureRegion.getHeight()/ 2f,backgroundTextureRegion,   this.getVertexBufferObjectManager());         mScene.attachChild(背景);

我希望它可以帮助其他人。

答案 1 :(得分:0)

@Rahul,也在我的壁纸服务中的wallaper活动代码下面工作

public class MagicWallpaper extends BaseLiveWallpaperService {

private TextureRegion region;

@Override
public EngineOptions onCreateEngineOptions() {
    calculateWidthAndHeight();
    Camera mCamera = new Camera(0, 0, Helper.GAME_WIDTH, Helper.GAME_HEIGHT);
    EngineOptions engOptions = new EngineOptions(true,
            ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(),
            mCamera);
    return engOptions;
}

@SuppressWarnings("deprecation")
private void calculateWidthAndHeight() {
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    Helper.GAME_WIDTH = display.getWidth();
    Helper.GAME_HEIGHT = display.getHeight();
}

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)
        throws Exception {

    BitmapTextureAtlas atlas = new BitmapTextureAtlas(getTextureManager(),
            600, 1024);
    region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,
            this, "wall.png", 0, 0);
    atlas.load();
    pOnCreateResourcesCallback.onCreateResourcesFinished();
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
        throws Exception {
    mEngine.registerUpdateHandler(new FPSLogger());
    Scene dScene = new Scene();
    dScene.setBackground(new SpriteBackground(new Sprite(0, 0, region,
            getVertexBufferObjectManager())));
    pOnCreateSceneCallback.onCreateSceneFinished(dScene);
}

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

}