2015-02-05 65 views
0

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

} 

回答

0

獲取設備的寬度和高度像下面,並用它

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

其他的事情,你的背景圖片可能會更小(小於身高)比你屏幕。 因此,無論是縮放圖片還是重複。在最後一種情況下,你可以設置紋理圖集到TextureOptions.REPEATING_BILINEAR,然後讓你精靈變大。

+0

我仍然無法找出問題所在。我嘗試根據手機的寬度和高度調整圖像的寬度和高度。 – user3624963

+0

請更新以上代碼@ user3624963 –

+0

更新我的代碼 – user3624963