更改设置后更新动态壁纸

时间:2013-11-28 07:14:59

标签: android andengine live-wallpaper

我正在制作带有发动机的活壁纸。我的项目有两个班级

public class LiveWallpaperMain extends BaseLiveWallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener
{

//Other methods

    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
        // TODO Auto-generated method stub
        this.mEngine.registerUpdateHandler(new FPSLogger());
        scene = new Scene();


        if(snowDensity == 5)
            snowDensityValue = 50;
        else if(snowDensity == 10)
            snowDensityValue = 200;
        else
            snowDensityValue = 500;
        particleSystem = new BatchedPseudoSpriteParticleSystem(
                new RectangleParticleEmitter(CAMERA_WIDTH / 2, 0, CAMERA_WIDTH, CAMERA_HEIGHT),
                2, 5, snowDensityValue, mSnowParticleRegion, vbom);
        particleSystem.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE);
        particleSystem.addParticleInitializer(new VelocityParticleInitializer<Entity>(-3, 3, 20, 40));
        particleSystem.addParticleInitializer(new AccelerationParticleInitializer<Entity>(-3, 3, 3, 5));
        particleSystem.addParticleInitializer(new RotationParticleInitializer<Entity>(0.0f, 360.0f));
        particleSystem.addParticleInitializer(new org.andengine.entity.particle.modifier.ExpireParticleInitializer<Entity>(15f));
        particleSystem.addParticleInitializer(new ScaleParticleInitializer<Entity>(0.5f, 0.9f));
        particleSystem.addParticleInitializer(new RegisterXSwingEntityModifierInitializer<Entity>(15f, 0f, (float) Math.PI * 8, 3f, 25f, true));

        particleSystem.addParticleModifier(new AlphaParticleModifier<Entity>(10f, 15f, 1.0f, 0.0f));
    Log.e("LiveWallpaperSettings", "Density " + snowDensityValue);



    pOnCreateSceneCallback.onCreateSceneFinished(scene);
}

}

这是设置类

public class LiveWallpaperSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
{   
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        SharedPreferences customSharedPreference = getSharedPreferences(key, MODE_PRIVATE);
        SharedPreferences.Editor editor = customSharedPreference.edit();
        editor.putString("Speed",listPreference.getValue());
        editor.commit();
        Log.e("LiveWallpaperSettings", "currvalue " + listPreference.getValue());
        currValue = listPreference.getValue();
    }
}

我想在设置中更改雪密度时更改它。当我更改设置时,它会在共享首选项中更改,但如何刷新粒子系统以使更改立即反映在那里?

1 个答案:

答案 0 :(得分:2)

首先,您不需要在LiveWallpaperSettings类中实现OnSharedPreferenceChangeListener。如果在xml文件中正确定义了首选项键,则将自动存储值。你的课应该是这样的:

public class LiveWallpaperSettings extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getPreferenceManager().setSharedPreferencesName("shared-preference-file-name");
        addPreferencesFromResource(R.xml.settings); // this is xml with PreferenceScreen element
    }
}

在LiveWallpaperMain类中实现OnSharedPreferenceChangeListener。实施应如下所示:

   public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if ("snow-density-key-name-from-xml".equals(key)) {
            // here call the method to update snow density
        }
   }