使用 SharedPreference 实现首选项,适用于 Android 动态壁纸
好吧,我需要帮助添加设置来选择要显示的纹理 - 您应该能够明白我的意思,因为已经设置了两个。我有用于设置的图形用户界面以及活动,但我只是不知道如何实现它。如果您可以帮助我,我会在应用程序的设置和市场帖子中感谢您。以及您的姓名、网站或视频中我的 YouTube 频道上的任何内容。可以在此处找到:youtube.com/motodroidhelpandinfo。我有超过 1,200 名订阅者。提前致谢。
这是我的首选活动:
package quotesandothers.livewallpaper.quotesandothers;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Settings extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getPreferenceManager().setSharedPreferencesName(
livewallpaper.PREFERENCES);
addPreferencesFromResource(R.xml.wallpaper_settings);
getPreferenceManager().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
getPreferenceManager().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {}
}
这是我在主要活动中实现它的尝试。请注意,我无法将相关内容放入整个活动中。另外,我希望设置在您可以看到的纹理之间进行更改。我还使用 andengine.org 作为骨干。
@Override
public void onLoadResources() {
preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(this);
this.mTexture = new Texture(2048, 2048, TextureOptions.DEFAULT);
this.mParallaxLayerMid1 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/middle1.png",
0, 320);
this.mParallaxLayerTop = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/top.png", 0,
0);
this.mParallaxLayerTop2 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/top2.png", 0,
173);
this.mParallaxLayerMid2 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/middle2.png",
0, 450);
this.mParallaxLayerLow1 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/lower1.png",
200, 574);
this.mParallaxLayerLow2 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/lower2.png",
0, 740);
this.mEngine.getTextureManager().loadTextures(this.mTexture,
this.mAutoParallaxMotivationalTexture);
this.mTexture = new Texture(2048, 2048, TextureOptions.DEFAULT);
this.mParallaxLayerTopInspired = TextureRegionFactory.createFromAsset(
this.mAutoParallaxInspirationalTexture, this,
"gfx/topinspired.png", 0, 0);
}
public void OnSharedPreferenceChanged(SharedPreferences prefs, String key) {}
Ok well, I need help adding settings to choose which texture to show - you should be able to tell what I mean because there are already two set up. I have the gui for the settings set up along with the activity I just can't figure out how to implement it. If you can help me I'll credit you in the app in the settings and the market post. Along with your name, website or, whatever on my youtube channel in a video. Which can be found here, youtube.com/motodroidhelpandinfo. I have over 1,200 subscribers. Thanks in advance.
Here's my preference Activity:
package quotesandothers.livewallpaper.quotesandothers;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Settings extends PreferenceActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getPreferenceManager().setSharedPreferencesName(
livewallpaper.PREFERENCES);
addPreferencesFromResource(R.xml.wallpaper_settings);
getPreferenceManager().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
getPreferenceManager().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {}
}
And here's my attempt at implementing it in the main activity. Note I couldn't fit in the whole Activity just what is relevant. Also I want the setting to change between the to textures that you can see. Also I use andengine.org as the backbone.
@Override
public void onLoadResources() {
preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(this);
this.mTexture = new Texture(2048, 2048, TextureOptions.DEFAULT);
this.mParallaxLayerMid1 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/middle1.png",
0, 320);
this.mParallaxLayerTop = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/top.png", 0,
0);
this.mParallaxLayerTop2 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/top2.png", 0,
173);
this.mParallaxLayerMid2 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/middle2.png",
0, 450);
this.mParallaxLayerLow1 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/lower1.png",
200, 574);
this.mParallaxLayerLow2 = TextureRegionFactory.createFromAsset(
this.mAutoParallaxMotivationalTexture, this, "gfx/lower2.png",
0, 740);
this.mEngine.getTextureManager().loadTextures(this.mTexture,
this.mAutoParallaxMotivationalTexture);
this.mTexture = new Texture(2048, 2048, TextureOptions.DEFAULT);
this.mParallaxLayerTopInspired = TextureRegionFactory.createFromAsset(
this.mAutoParallaxInspirationalTexture, this,
"gfx/topinspired.png", 0, 0);
}
public void OnSharedPreferenceChanged(SharedPreferences prefs, String key) {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那里看起来一切正常。
我的应用程序只有一处不同,它是基于立方体壁纸示例的。
您获得偏好的这条线对我来说看起来不同。
我的:
尝试一下,看看会发生什么。
Things look ok there.
There is only one difference with my app, which I based off the cube wallpaper example.
This line where you get the preferences looks different for me.
Mine:
Give it a try and see what happens.