帮助首选项和Android动态壁纸

时间:2010-10-06 23:41:00

标签: java android android-preferences sharedpreferences

基本上我想要做的是当首选项从默认值切换到加载一组新的视差背景时。现在代码可能过于复杂而且可能完全关闭当我切换我的首选项时没有任何变化。你绝对可以告诉我我是一个新手但是,你的帮助将非常感激,我很乐意在发布时在应用程序中为你提供信用。此外,如果您需要更多代码,请询问。

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

}

 }

1 个答案:

答案 0 :(得分:2)

您是否更新了设置类以链接到livewallpaper并使用相同的共享首选项名称?

例如;

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

如果不这样做,则不会进行任何设置更改。我假设您使用的是Livewallpaper模板,您可以从andengine网站获取。