Question

I use rajawali ripple for my live wallpaper and am successful in adding it. I also added water sound to onTouch event of live wallpaper. What I need is an option to enable and disable sound using checkbox in preference activity of that live wallpaper and add a boolean code in onToch event where I am stuck. So I need help to achieve it. Here is the code:

public class RipplesRenderer extends RajawaliRenderer {
    private final int NUM_CUBES_H = 4;
    private final int NUM_CUBES_V = 4;
    private final int NUM_CUBES = NUM_CUBES_H * NUM_CUBES_V;
    //private Animation3D[] mAnims;
    private TouchRippleFilter mFilter;
    private long frameCount;
    private final int QUAD_SEGMENTS = 40;
    int mScreenHeight,
    mScreenWeight;
    Gallery_Activity mm;
    boolean flag_check = false;
    int pos = 0;
    int viewBackgroundImageName;
    Bitmap texture;
    SimpleMaterial planeMat;
    int randPosition=1;

    int change_value;
    int Ripple_number,speed1;
    Preferences preferences;

    private MediaPlayer myplayer;
    private boolean sound;

    public RipplesRenderer(Context context) {
        super(context);
        setFrameRate(50);
        this.mContext=context;

        randPosition = BitmapUpdate.randomGenerator;
        texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gthree_one);
        //sp=mContext.getSharedPreferences("wallpapersettings",0);
        preferences=new Preferences(mContext);
    }

    protected void initScene() {

        if(randPosition!=1) {
            texture = BitmapUpdate.bit;
        }

        mCamera.setPosition(0, 0, -9);

        DirectionalLight light = new DirectionalLight(0, 0, 1);
        light.setPower(1f);

        BaseObject3D group = new BaseObject3D();
        DiffuseMaterial material = new DiffuseMaterial();
        material.setUseColor(true);
        mScreenHeight = GNWallpaper.hieght;

        mScreenWeight = GNWallpaper.weight;
        Random rnd = new Random();        

        planeMat = new SimpleMaterial();
        Plane plane=new Plane(9,7,1,1);

        //Plane plane = new Plane(4, 4, 1, 1);
        plane.setRotZ(-90);
        plane.setScale(1.0f);
        plane.setMaterial(planeMat);
        addChild(plane);

        mFilter = new TouchRippleFilter();

        mPostProcessingRenderer.setQuadSegments(QUAD_SEGMENTS);
        mPostProcessingRenderer.setQuality(PostProcessingQuality.MEDIUM);
        addPostProcessingFilter(mFilter);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        if (pos != com.themebowlapp.galaxynote3livewallpaper.Gallery_Activity.wallpaper_position || randPosition != BitmapUpdate.randomGenerator) {
            texture = BitmapUpdate.bit;
            pos = com.themebowlapp.galaxynote3livewallpaper.Gallery_Activity.wallpaper_position;
            randPosition = BitmapUpdate.randomGenerator;
        }

        Ripple_number=preferences.getSpeed_controler();

        speed1=Ripple_number*100;
        super.onSurfaceCreated(gl, config);
    }

    public void onDrawFrame(GL10 glUnused) {
        super.onDrawFrame(glUnused);

        mFilter.setTime((float) frameCount++ *.05f);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        super.onSurfaceChanged(gl, width, height);
        mFilter.setScreenSize(width, height);
        mFilter.setRippleSize((40+speed1));
        planeMat.addTexture(mTextureManager.addTexture(texture));
    }

    public void setTouch(float x, float y) {
        mFilter.addTouch(x, y, frameCount *.05f);
    }

    @Override
    public void  onTouchEvent(MotionEvent event) {
        final int action = event.getAction();

        if(event.getAction() == MotionEvent.ACTION_DOWN) {            
            //sound
            myplayer = MediaPlayer.create(getContext(), R.raw.water_drop);
            myplayer.setVolume(100, 100);
            myplayer.start();

            myplayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer myplayer) {
                    myplayer.release();
                } 
            });

            setTouch(event.getX() / mScreenWeight, 1.0f - (event.getY() / mScreenHeight));
        }

        super.onTouchEvent(event);
    }
}

Settings.java

public class Settings extends Activity  {
    public TextView SettingTextObj, BackgroundTextObj;
    private RelativeLayout chose_background;
    public Preferences preferences;
    Context cont = this;
    private CheckBox soundcheckbox;
    private String PREFRENCES_NAME;
    SharedPreferences settings;
    //    private Button choosebackground;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        chose_background = (RelativeLayout) findViewById(R.id.BackgroundLayoutId);
        BackgroundTextObj = (TextView) findViewById(R.id.backgroundTxtViewId);

        soundcheckbox = (CheckBox)findViewById(R.id.checkBox1);
        settings = getSharedPreferences(PREFRENCES_NAME, 0);
        Boolean isChecked = settings.getBoolean("cbx1_ischecked", false);
        soundcheckbox.setChecked(isChecked);

        soundcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            private MediaPlayer myplayer;
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                Editor editor = getSharedPreferences(PREFRENCES_NAME, 0).edit();
                editor.putBoolean("cbx1_ischecked", isChecked);
                editor.commit();
                Toast.makeText(getApplicationContext(), "Check", Toast.LENGTH_SHORT).show();
                myplayer = MediaPlayer.create(getBaseContext(), R.raw.water_drop);
                myplayer.setVolume(100, 100);
                myplayer.start();
            }
        });


        preferences = new Preferences(cont);

        chose_background.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = { "Phone Gallery", "Choose Background" };

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        Settings.this);
                builder.setTitle("Pick a Background");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (items[item].equals(items[0])) {
                            startActivity(new Intent(Settings.this, PhoneGallery_Activity.class));
                        } else {
                            startActivity(new Intent(Settings.this, Gallery_Activity.class));
                        }
                    }
                });

                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }
}
Was it helpful?

Solution

Looks like You already have checkbox in the Settings. I would suggest to do the following to check its value:

@Override
public void  onTouchEvent(MotionEvent event) {
    final int action = event.getAction();

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        if (mContext.getSharedPreferences(PREFERENCES_NAME, Context.MODE_WORLD_READABLE).getBoolean("cbx1_ischecked", false)) {
            //sound
            myplayer = MediaPlayer.create(getContext(), R.raw.water_drop);
            myplayer.setVolume(100, 100);
            myplayer.start();

            myplayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer myplayer) {
                    myplayer.release();
                } 
            });
        }

        setTouch(event.getX() / mScreenWeight, 1.0f - (event.getY() / mScreenHeight));
    }

    super.onTouchEvent(event);
}

Also, You might need to make the preferences WORLD_READABLE in initial getSharedPreferences() call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top