如何使用共享的首选项正确设置新图像?
-
25-10-2019 - |
题
我正在使用和引擎来创建一个实时墙纸,并且为设置菜单的出现设置了正确的设置。我只是不确定如何实际切换用户可以在设置菜单中选择的图像加载的当前图像。
示例:默认情况下,程序将加载背景图像2和顶层图像1.如果用户选择通过“设置”菜单使用背景图3,我希望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);
这是我的prexperion.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>
我想我的主要问题是如何加载用户选择的新图像,然后将其应用于实际墙纸?
解决方案
发现了灵魂:
创建了这个:
@Override
public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
{
settingsChanged = true;
}
添加:
@Override
public void onResume(){
super.onResume();
if(settingsChanged)
{
BuildScene();
settingsChanged = false;
}
}
(buildScene())是我的OnloadScene()方法中的调用。)。
然后,我只是自定义编码,如果elsecene()方法中的其他语句弄清楚用户当前正在使用的选项,然后应用新图像。
不隶属于 StackOverflow