2011-11-30 86 views
0

我使用AndEngine創建動態壁紙。然而,在我onLoadResources()方法,我現在裝3個不同質地:我的AndEngine onLoadResources()似乎效率不高

@Override 
public void onLoadResources() { 
    prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0); 
    prefs.registerOnSharedPreferenceChangeListener(this); 
    this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT); 
    this.mAutoParallaxImage1Texture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT); 
    this.mAutoParallaxImage2Texture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT); 

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
    this.mParallaxLayerBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "background.jpg", 0, 0); 
    this.mParallaxLayerImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxImage1Texture, this, "image1.jpg", 0, 0); 
    this.mParallaxLayerImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxImage2Texture, this, "image2.png", 0, 800); 

    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxBackgroundTexture); 
    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxImage1Texture);    
    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxImag2Texture);    

} 

我覺得這是不是這樣做的最有效方法。但是,如果我將所有BitmapTextureAtlasTextureRegionFactorys'圖像'指向一個BitmapTextureAtlas,並且這些'圖像'處於相同座標,則即使只調用其中一個圖像,圖像也會一起加載。

問題的一個例子是在這裏:

@Override 
public void onLoadResources() { 
    prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0); 
    prefs.registerOnSharedPreferenceChangeListener(this); 
    this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT); 

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
    this.mParallaxLayerBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "background.jpg", 0, 0); 
    this.mParallaxLayerImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image1.jpg", 0, 0; 
    this.mParallaxLayerImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image2.png", 0, 800); 

    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxBackgroundTexture);    

} 

^我也喜歡使用這樣的代碼,因爲它似乎更有效,但「圖像1」和「圖像2」上的每個頂部自動加載其他即使我只打1個。

我這樣做是正確的嗎?還是有更高效的方法?

回答

2

您的紋理可以包含多個資源。把它想成一個精靈表。或者像將照片粘貼到一張大白紙上。每個紋理區域都不需要新的紋理。例如: 假設您有一個星形圖像,即250x250像素和100x100像素的太空船圖像。你可以把它們放在一個尺寸爲512x256的紋理上。 您在0星廣告(250x250)創建一個紋理區域,0 然後在251飛船創建第二個紋理區域,0

在這種情況下還有一定的剩餘區域其他圖像吹太空船,如果他們足夠小,以適應。下面的圖片展示瞭如果您可以在內存中看到您的紋理,它將如何顯示。

texture with two resources added

希望這有助於。

+0

謝謝,這有助於很多。雖然我創建的圖片佔據了整個屏幕,所以我認爲它們必須達到這個尺寸。但onLoadResources()僅在第一次創建壁紙時調用..對嗎? – MJ93

+0

如果活動被系統殺死,它也可以在onResume()上調用 - 如果你有巨大的紋理,這很可能。讓你的紋理儘可能的小,你可以擺脫。嘗試製作一個半分辨率的設置,並讓AndEngine中的開放式GL將其解決。 YOu可能會覺得它看起來不錯。 –

0

您的紋理很偉大!你確定你需要他們那麼大嗎?

+0

啊謝謝你我忘了那些。你會推薦1024x1024? – MJ93

+0

是的,1024x1024應該是最大的,以確保與所有設備兼容。 –