Come posso utilizzare Preferenze condivise per impostare correttamente una nuova immagine?
-
25-10-2019 - |
Domanda
sto usando AndEngine per creare un Live Wallpaper e devo impostare tutto nel modo giusto per il menu Impostazioni a venire. Solo che non sono sicuro di come passare in realtà le immagini attuali che il carico di default con le immagini che gli utenti possono scegliere nel menu Impostazioni.
Esempio: Per impostazione predefinita, il programma carica l'immagine strato superiore 1 image2 sfondo e. Se un utente sceglie un'immagine uso di fondo 3 tramite il menu delle impostazioni per, vorrei image3 image2 sostituire.
Ecco il mio codice principale:
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)
{
}
}
Non sono sicuro di quello che deve essere aggiunto dopo:
prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
prefs.registerOnSharedPreferenceChangeListener(this);
Ecco il mio file di preference.xml che ha il menu Impostazioni attuale:
<?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>
Credo che la mia domanda principale è come posso fare per il caricamento della nuova immagine che l'utente ha scelto e poi applicarlo alla carta da parati reale?
Soluzione
Trovato il soulution:
ha creato questo:
@Override
public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
{
settingsChanged = true;
}
Aggiunto:
@Override
public void onResume(){
super.onResume();
if(settingsChanged)
{
BuildScene();
settingsChanged = false;
}
}
(BuildScene ()) è una chiamata dentro la mia onLoadScene () metodo.)
Poi ho personalizzato codificato Se, Else all'interno del BuildScene () per capire quale opzione l'utente sta utilizzando, e poi applicato la nuova immagine.