2012-06-03 134 views
0

我已將設置添加到我的動態壁紙,但它們在更改時未應用。如果有人能告訴我爲什麼我的設置在更改時未被應用,我將不勝感激。這裏是我的代碼:不適用的動態壁紙設置

的settings.xml

 <PreferenceCategory android:title="@string/more"> 
     <PreferenceScreen android:title="@string/more"> 
      <intent android:action="android.intent.action.VIEW" 
      android:data="market://search?q=pub:PSP Demo Center" /> 
     </PreferenceScreen> 
     <ListPreference 
      android:persistent="true" 
      android:enabled="true" 
      android:entries="@array/settings_light_number_options" 
      android:title="@string/settings_light_number" 
      android:key="light_power" 
      android:summary="@string/settings_light_number_summary" 
      android:defaultValue="3" 
      android:entryValues="@array/settings_light_number_optionvalues" /> 
     <ListPreference 
      android:persistent="true" 
      android:enabled="false" 
      android:entries="@array/settings_speed_number_options" 
      android:title="@string/settings_speed_number" 
      android:key="speed" 
      android:summary="@string/settings_speed_number_summary" 
      android:defaultValue="10" 
      android:entryValues="@array/settings_speed_number_optionvalues" /> 
     <ListPreference 
      android:persistent="true" 
      android:enabled="false" 
      android:entries="@array/settings_rotate_number_options" 
      android:title="@string/settings_rotate_number" 
      android:key="rotate" 
      android:summary="@string/settings_rotate_number_summary" 
      android:defaultValue="8000" 
      android:entryValues="@array/settings_rotate_number_optionvalues" /> 

    </PreferenceCategory> 

</PreferenceScreen> 

Settings.java

public class GraffitiLWPSettings extends PreferenceActivity 
       implements SharedPreferences 
       .OnSharedPreferenceChangeListener { 
    public static final String SHARED_PREFS_NAME = "wallpaper_settings"; 


    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     getPreferenceManager(). 
      setSharedPreferencesName(GraffitiLWP.SHARED_PREFS_NAME); 
     addPreferencesFromResource(R.xml.settings); 
     getPreferenceManager().getSharedPreferences(). 
      registerOnSharedPreferenceChangeListener(this); 
    } 

    protected void onResume() { 
     super.onResume(); 
    } 

    protected void onDestroy() { 
     getPreferenceManager().getSharedPreferences() 
      .unregisterOnSharedPreferenceChangeListener(this); 
     super.onDestroy(); 
    } 

    public void onSharedPreferenceChanged(SharedPreferences 
      sharedPreferences, String key) { 
    } 
} 

wallpaper.java

public class GraffitiLWP extends Wallpaper { 
    private GraffitiLWPRenderer mRenderer; 
    public static final String SHARED_PREFS_NAME = "wallpaper_settings"; 
    public Engine onCreateEngine() { 
     mRenderer = new GraffitiLWPRenderer(this); 
     return new WallpaperEngine(
      this.getSharedPreferences(SHARED_PREFS_NAME, 
     Context.MODE_PRIVATE), getBaseContext(), mRenderer); 
    } 
} 

renderer.java

public class GraffitiLWPRenderer extends RajawaliRenderer { 
    private Animation3D mAnim; 
    private BaseObject3D mCan; 
    private SettingsUpdater settingsUpdater; 
    //private SharedPreferences preferences; 


    public GraffitiLWPRenderer(Context context) { 
     super(context); 
     setFrameRate(20); 
    } 

    public class SettingsUpdater implements SharedPreferences 
      .OnSharedPreferenceChangeListener { 
     private GraffitiLWPRenderer renderer; 

     public SettingsUpdater(GraffitiLWPRenderer renderer) 
     { 
      this.renderer = renderer; 
     } 

     public void onSharedPreferenceChanged(
       SharedPreferences sharedPreferences, String key) { 
      preferences.getInt("wallpaper_settings", 0); 

      renderer.setSharedPreferences(preferences); 
     } 
    } 

    public void initScene() { 
     System.gc(); 
     ALight light = new DirectionalLight(); 
     light.setPower(this.preferences.getLong("light_power", 3)); 
     light.setPosition(0, 0, -10); 
     mCamera.setPosition(0, -1, -7); 
     mCamera.setLookAt(0, 2, 0); 
     mCamera.setFarPlane(1000); 

     ObjParser parser = new ObjParser(mContext 
        .getResources(), mTextureManager, R.raw.spraycan_obj); 
     parser.parse(); 
     mCan = parser.getParsedObject(); 
     mCan.addLight(light); 
     mCan.setScale(1.2f); 
     addChild(mCan); 

     Number3D axis = new Number3D(0, 
      this.preferences.getLong("speed", 10), 0); 
     axis.normalize(); 
     mAnim = new RotateAnimation3D(axis, 360); 
     mAnim.setDuration(this.preferences.getLong("rotate", 8000)); 
     mAnim.setRepeatCount(Animation3D.INFINITE); 
     mAnim.setInterpolator(new AccelerateDecelerateInterpolator()); 
     mAnim.setTransformable3D(mCan); 


     setSkybox(R.drawable.posz, R.drawable.posx, 
      R.drawable.negz, R.drawable.negx, R.drawable.posy, 
      R.drawable.negy); 
    } 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
     settingsUpdater = new SettingsUpdater(this); 
     this.preferences.registerOnSharedPreferenceChangeListener(
      settingsUpdater); 
     settingsUpdater.onSharedPreferenceChanged(preferences, null); 
     super.onSurfaceCreated(gl, config); 
     mAnim.start();   
    } 

    public void onDrawFrame(GL10 glUnused) { 
     super.onDrawFrame(glUnused); 
     mSkybox.setRotY(mSkybox.getRotY() + .5f);   
    } 
} 

我知道代碼很長,但我會非常感謝有人可以給我的任何幫助。謝謝。

回答

1

您將PreferenceScreen嵌套在PreferenceCategory中。

- 編輯 - 仔細檢查,標籤都混在一起。 (您要關閉PreferenceScreen兩次)

我相信基本結構是:

<PreferenceScreen android:title="@string/more"> 
    <PreferenceCategory android:title="@string/more"> 
     <ListPreference android:title="@string/settings_something"/> 
    </PreferenceCategory> 
    </PreferenceScreen> 

此外,你必須android:enabled="false"添加到每個偏好。 我可以想象這將禁用輸入...

0

您需要將您的wallpaper.java更改爲:

public class GraffitiLWP extends Wallpaper 
     implements OnSharedPreferenceChangeListener{ 
    private GraffitiLWPRenderer mRenderer; 
    public static final String SHARED_PREFS_NAME = "wallpaper_settings"; 
    private SharedPreferences prefs; 

public Engine onCreateEngine() { 
    mRenderer = new GraffitiLWPRenderer(this); 
    prefs = getSharedPreferences(SHARED_PREFS_NAME, 0); 
    prefs.registerOnSharedPreferenceChangeListener(this); 

    return new WallpaperEngine(
     this.getSharedPreferences(SHARED_PREFS_NAME, 
    Context.MODE_PRIVATE), getBaseContext(), mRenderer); 
    } 
} 

基本上我只是增加了implements OnSharedPreferenceChangeListener

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

+0

u能請給一些更多的澄清,對壁紙應用程序應用設置 –

0

我曾與拉賈瓦利引擎相同的問題;或者至少我雖然我有。 我可以從預覽中進入偏好設置,但是我無法選擇何時應用動態壁紙。 我所做的:我改變了清單這樣(在這裏你聲明的偏好活動):

<activity 
    android:label="@string/settings" 
    android:name="com.onebutton.livewallpaper.budastatuefree.BudaStatueFreeSettings" 
    android:theme="@android:style/Theme.Light.WallpaperSettings" 
    android:exported="true"> 
</activity> 

當然,你必須改變活動名稱

希望這對你的作品...

0

如果你不需要,不要過於複雜的事情。所有你需要的是你的渲染器中的一個函數:

private void setOnPreferenceChange(){ 
    mListener = new OnSharedPreferenceChangeListener(){ 
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 
      if ("cool_pref".equals(key)) 
      { 
       camSpeed = Integer.parseInt(sharedPreferences.getString(key, "default")); 
      } 
      preferences = sharedPreferences; 
     } 
    };  
} 

在initScene中調用函數,你就可以開始了。

我也這樣做是爲了搶在每個initScene喜好太:

private void setPrefsToLocal(){ 
    localPrefVar= Integer.parseInt(preferences.getString("cool_pref", "default")); 
}