如何使用共享首选项正确设置一个新的形象?

我使用AndEngine创建一个动态壁纸和我的一切设置了设置的正确方法菜单上来。 我只是不知道如何真正转出,默认情况下使用,用户可以在设置菜单中选择图像加载当前图像。

例如:在默认情况下,该方案如果用户选择使用通过设置菜单背景图片加载3背景图像2和顶层图像1,我想的Image3更换IMAGE2。

这是我主要的代码:

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

我不知道有什么后加入:

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

这里是一个具有实际的设置菜单我preference.xml文件:

<?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>

我想我的主要问题是我怎么去加载用户选择新的图像,然后将其应用到实际的壁纸?

--------------解决方案-------------

发现soulution:

创造了这个:

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

添加:

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

(建立场景())是我onLoadScene()方法中调用。)

然后,我只是自定义编码如果在建立场景()方法中else语句找出用户当前使用的选项,然后应用新形象。

分类:安卓 时间:2015-03-15 人气:1
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.361 (s). 10 q(s)