2011-11-30 74 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应该是最大的,以确保与所有设备兼容。 –