2010-10-06 78 views
1

Fondamentalement, ce que je veux faire est quand la préférence est passée de la valeur par défaut à tout ce qui de charger un nouvel ensemble de milieux de parallaxe. Avec le code maintenant qui est probablement trop compliqué et peut-être complètement éteint Quand je change mes préférences rien ne change. Vous pouvez certainement dire que je suis un novice mais, votre aide serait grandement appréciée et je serais heureux de vous créditer dans l'application lorsque je l'affiche. Aussi, si vous avez besoin de plus du code, il suffit de demander.Aide avec les préférences et Android Live Wallpaper

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) { 

} 

} 

Répondre

2

Avez-vous mis à jour votre classe de paramètres pour créer un lien vers votre livewallpaper et utiliser le même nom de préférence partagée?

Par exemple;

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

Si ce n'est pas le cas, vous n'obtiendrez aucun des changements de paramètres. Je suppose que vous utilisez le modèle Livewallpaper que vous pouvez obtenir à partir du site andengine.