2010-10-06 17 views
1

基本上我想要做的是當首選項從默認切換到加載一組新的視差背景時。現在的代碼可能過於複雜並且可能完全關閉當我切換我的首選項時,沒有任何更改。你絕對可以告訴我是一個新手,但是,你的幫助將非常值得讚賞,我很樂意在應用程序發佈信息時發佈它。另外如果你需要更多的代碼,只需要問。首選項和Android動態壁紙幫助

package quotesandothers.livewallpaper.quotesandothers; 

import org.anddev.andengine.engine.camera.Camera; 
import org.anddev.andengine.engine.options.EngineOptions; 
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation; 
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy; 
import org.anddev.andengine.entity.scene.Scene; 
import org.anddev.andengine.entity.scene.background.AutoParallaxBackground; 
import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity; 
import org.anddev.andengine.entity.sprite.Sprite; 
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService; 
import org.anddev.andengine.opengl.texture.Texture; 
import org.anddev.andengine.opengl.texture.TextureOptions; 
import org.anddev.andengine.opengl.texture.region.TextureRegion; 
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory; 
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion; 

import android.content.SharedPreferences; 

public class livewallpaper extends BaseLiveWallpaperService implements 
    SharedPreferences.OnSharedPreferenceChangeListener { 
// =========================================================== 
// Constants 
// =========================================================== 




public static final String SHARED_PREFS_NAME ="Settings"; 
private String mTheme= "inspiration"; 

// Camera Constants 
private static final int CAMERA_WIDTH = 480; 
private static final int CAMERA_HEIGHT = 854; 


// =========================================================== 
// Fields 
// =========================================================== 

// Shared Preferences 

private Texture mTexture; 



private Texture mAutoParallaxInspirationalTexture; 

private TextureRegion mParallaxLayerMid1; 

private TextureRegion mParallaxLayerTop; 

private TextureRegion mParallaxLayerTop2; 

private TextureRegion mParallaxLayerMid2; 

private TextureRegion mParallaxLayerLow1; 

private TextureRegion mParallaxLayerLow2; 
private TextureRegion mParallaxLayerInspired; 


private Texture mAutoParallaxBackgroundTexture; 



// =========================================================== 
// Constructors 
// =========================================================== 

// =========================================================== 
// Getter & Setter 
// =========================================================== 

// =========================================================== 
// Methods for/from SuperClass/Interfaces 
// =========================================================== 

@Override 
public org.anddev.andengine.engine.Engine onLoadEngine() { 
    Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
    return new org.anddev.andengine.engine.Engine(
      new EngineOptions(true, ScreenOrientation.PORTRAIT, 
        new FillResolutionPolicy(), mCamera)); 
} 

@Override 
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 
    mTheme = prefs.getString("cube2_shape", "inspiration"); 
    readTheme(); 

} 
private void readTheme(){ 

    final Scene scene = new Scene(1); 
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(
      0, 0, 0, 5); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(2.7f, 
      new Sprite(0, 0, this.mParallaxLayerInspired))); 
    scene.setBackground(autoParallaxBackground); 

} 



@Override 
public void onLoadResources() { 
    this.mTexture = new Texture(2048, 2048, TextureOptions.DEFAULT); 


    this.mAutoParallaxBackgroundTexture = new Texture(2048, 2048, 
      TextureOptions.DEFAULT); 
    this.mParallaxLayerMid1 = TextureRegionFactory.createFromAsset(
      this.mAutoParallaxBackgroundTexture, this,  "gfx/middle1.png",0, 320); 
    this.mParallaxLayerTop = TextureRegionFactory.createFromAsset(
      this.mAutoParallaxBackgroundTexture, this, "gfx/top.png", 0, 0); 
    this.mParallaxLayerTop2 = TextureRegionFactory.createFromAsset(
      this.mAutoParallaxBackgroundTexture, this, "gfx/top2.png", 0, 173); 
    this.mParallaxLayerMid2 = TextureRegionFactory.createFromAsset(
      this.mAutoParallaxBackgroundTexture, this, "gfx/middle2.png",0, 450); 
    this.mParallaxLayerLow1 = TextureRegionFactory.createFromAsset(
      this.mAutoParallaxBackgroundTexture, this, "gfx/lower1.png", 200,574); 
    this.mParallaxLayerLow2 = TextureRegionFactory.createFromAsset(
      this.mAutoParallaxBackgroundTexture, this, "gfx/lower2.png", 0,740); 
    this.mEngine.getTextureManager().loadTextures(this.mTexture, 
      this.mAutoParallaxBackgroundTexture); 

    this.mAutoParallaxInspirationalTexture = new Texture(2048, 2048, 
       TextureOptions.DEFAULT); 

    this.mParallaxLayerInspired = TextureRegionFactory.createFromAsset(
       this.mAutoParallaxInspirationalTexture, this, "gfx/topinspired.png", 0, 0); 
    this.mEngine.getTextureManager().loadTextures(this.mTexture, 
       this.mAutoParallaxInspirationalTexture); 




} 

@Override 
public Scene onLoadScene() { 

    final Scene scene = new Scene(1); 
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(
      0, 0, 0, 5); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(2.7f, 
      new Sprite(0, 0, this.mParallaxLayerTop))); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(2.9f, 
      new Sprite(0, 173, this.mParallaxLayerTop2))); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(2.4f, 
      new Sprite(0, 320, this.mParallaxLayerMid1))); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(-1.4f, 
      new Sprite(0, 450, this.mParallaxLayerMid2))); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(2.3f, 
      new Sprite(200, 574, this.mParallaxLayerLow1))); 
    autoParallaxBackground.addParallaxEntity(new ParallaxEntity(-4.1f, 
      new Sprite(0, 740, this.mParallaxLayerLow2))); 



    scene.setBackground(autoParallaxBackground); 




    return scene; 


    } 





private float setHeight(int i) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public void onLoadComplete() { 

} 

@Override 
protected void onTap(final int pX, final int pY) { 

} 

} 

回答

2

您是否已更新設置類以鏈接到您的動態壁紙並使用相同的共享首選項名稱?

例如;

public class Settings extends PreferenceActivity 
implements SharedPreferences.OnSharedPreferenceChangeListener { 
@Override 
    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     getPreferenceManager().setSharedPreferencesName(
       livewallpaper.SHARED_PREFS_NAME); 

如果您不這樣做,那麼您將不會得到任何設置更改。我假設您正在使用可從andengine網站獲得的Livewallpaper模板。