【发布时间】:2011-12-04 16:01:19
【问题描述】:
我正在使用 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>
我想我的主要问题是如何加载用户选择的新图像,然后将其应用于实际壁纸?
【问题讨论】:
-
我的猜测是您需要有一个
BroadcastReceiver来检测何时请求更改壁纸,并将其注册到您的应用程序清单中。就在我的脑海中:) -
嗯,我相信这就是
SharedPreferences.OnSharedPreferenceChangeListener和onSharedPreferenceChanged()的用途 -
BroadcastReceiver 允许您的系统接收通用广播,例如“拨打电话”或“更换壁纸”。您可能需要注册它并进行系统调用才能真正更改墙纸。
标签: android andengine live-wallpaper