Question

I'm using AndEngine to create a Live Wallpaper and I have everything set up the right way for the Settings menu to come up. I'm just not sure how to actually switch out the current images that load by default with images that users can choose in the Settings menu.

Example: By default, the program loads the background image2 and the top layer image 1. If a user chooses to use background image 3 via the Settings menu, I would like image3 to replace image2.

Here is my main code:

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


    public static final String SHARED_PREFS_NAME = "preferences";
    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;

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

    private Camera mCamera;


    private BitmapTextureAtlas mAutoParallaxBackgroundTexture;

    private TextureRegion mParallaxLayerBack;
    private TextureRegion mParallaxLayerFront;

    private SharedPreferences prefs;




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

    @Override
    public void onLoadResources() {
        prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
        prefs.registerOnSharedPreferenceChangeListener(this);


            BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");


            this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
            this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image1.png", 0, 800);
            this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image2.jpg", 0, 0);

            this.mEngine.getTextureManager().loadTextures(this.mAutoParallaxBackgroundTexture);
    }

    @Override
    public Scene onLoadScene() {
            this.mEngine.registerUpdateHandler(new FPSLogger());

            final Scene scene = new Scene();
            final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
            autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(-10.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerBack.getHeight(), this.mParallaxLayerBack)));
            autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0, CAMERA_HEIGHT - this.mParallaxLayerFront.getHeight(), this.mParallaxLayerFront)));
            scene.setBackground(autoParallaxBackground);

            return scene;
    }

....
....

    @Override
    public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
    {

    }


}    

I'm not sure what has to be added after:

prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
        prefs.registerOnSharedPreferenceChangeListener(this);

Here is my preference.xml file which has the actual Settings menu:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
                android:title="Main Settings">
                 <ListPreference
           android:title="List Preference"
           android:summary="This preference allows to select an item in an array"
           android:key="listPref"
           android:defaultValue="1"
           android:entries="@array/background"
           android:entryValues="@array/background_values" /> 
        </PreferenceCategory>
</PreferenceScreen>

I guess my main question is how do I go about loading the new image the user has chosen and then applying it to the actual wallpaper?

Was it helpful?

Solution

Found the soulution:

Created this:

    @Override
    public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
    {           
            settingsChanged = true;
    }

Added:

    @Override
    public void onResume(){
        super.onResume();
        if(settingsChanged)
        {
                BuildScene();
                settingsChanged = false;
        }
    }

(BuildScene()) is a call inside my onLoadScene() method.)

Then I just custom coded If, Else statements inside the BuildScene() method to figure out which option the user is currently using, and then applied the new image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top