2

I have developed Android LWP using RenderScript. Now I want to add Setting Page and I use Preferences to save the option values.

EG. Initial objects quantity is 10. Then user can change the objects quantity to 20.

I cannot update/refresh/reset/recall the RenderScript to regenerate with the new Setting. How to do it?

I have managed up to onSharedPreferenceChanged, but how to call it in order to regenerate the LWP with the new Setting just after user click?

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//How to force the renderscript to update here
}    

In fact, what I need to know is "How to refresh/clear any drawn object on renderscript?" Because when setting has been changed, I need to redraw the objects again....

The best things I have managed to go so far is to set the quantity of the objects on the listener like this

private class SPListener implements SharedPreferences.OnSharedPreferenceChangeListener {

@Override
public void onSharedPreferenceChanged(
        SharedPreferences sharedPreferences, String key) {
    // TODO Auto-generated method stub

    OBJ_COUNT = Integer.parseInt(Utility.getNoOfObjects(mContext));

    mScript.set_gObjectsCount(OBJ_COUNT);           
    mScript.invoke_updateObjects();


}

}

But I got "double" drawn objects. I need to clear existing objects first before I set with new quantity of objects.

EG. Initial objects quantity is 10. Then user can change the objects quantity to 5.

What I got is still 10 objects....with 5 objects are drawn twice/duplicated on top of it. So 5 objects have darker color due this overlay drawn.

stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63

2 Answers2

0

I don't use RenderScript, but in my live wallpapers, I keep a class of static variables for all my settings, and an OnPreferenceChangedListener that transfers my settings to that class. In my draw method of the live wallpaper, I just pull the current values of those static variables. Also, when your live wallpaper first starts, you need to tell the settings class to update all of its variables to current values.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • OnPreferenceChangedListener is on another class called MyPreferenceActivity.java. So this is successfully called when the Setting was changed. But how to make/call CarView class to refresh its content? About the static values, ok so we can accessed these values but how to trigger/call the draw method again from onPreferenceChangedListener? We cannot just call Car View mView = new CarView(this); setContentView(mView); from inside onPreferenceChangedListener. – stuckedunderflow Aug 25 '13 at 14:56
  • In your Car activity, you can also register another onPreferenceChangedListener in onResume() and unregister it in onPause(). I don't have any experience with RenderScript, but I figure your listener can use the Allocation class to send your new values to the native code. I don't know how your animation is structured, but it is probably cleaner to set up your draw method to read settings from static variables on every frame and adjust accordingly with if statements, rather than rebuild the view. So your animation doesn't need to be told that there are new values. – Tenfour04 Aug 25 '13 at 17:43
  • Yes, actually I have no problem with onPreferenceChangedListener. Now, I just don't know how to regenerate/redraw/refresh the renderscript draw method...I use .rs file anyway. – stuckedunderflow Sep 01 '13 at 01:24
0

Finally managed to fix it...Nothing wrong on the technique. The location to update is also correct on onSharedPreferenceChanged. But I manage to redraw everything perfectly now.

stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63