我正在使用AndEngine创建动态壁纸,我已经为“设置”菜单设置了正确的方法。我只是不确定如何实际切换当前图像,默认情况下加载用户可以在“设置”菜单中选择的图像。
实施例: 默认情况下,程序加载背景图像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);
这是我的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>
我想我的主要问题是如何加载用户选择的新图像然后将其应用到实际的壁纸?
答案 0 :(得分:0)
找到了洗液:
创建了这个:
@Override
public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
{
settingsChanged = true;
}
添加了:
@Override
public void onResume(){
super.onResume();
if(settingsChanged)
{
BuildScene();
settingsChanged = false;
}
}
(BuildScene())是我的onLoadScene()方法内的一个调用。)
然后我只是在BuildScene()方法中自定义编码If,Else语句,找出用户当前使用的选项,然后应用新图像。