0

我使用 rajawali 波纹作为我的动态壁纸,并成功添加了它。我还在动态壁纸的 onTouch 事件中添加了水声。我需要的是一个选项,可以使用该动态壁纸的首选项活动中的复选框启用和禁用声音,并在我卡住的 onToch 事件中添加一个布尔代码。所以我需要帮助来实现它。这是代码:

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);
    }
}

设置.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;
    }
}
4

1 回答 1

0

看起来您已经在“设置”中设置了复选框。我建议执行以下操作来检查其值:

@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);
}

此外,您可能需要在初始调用中设置首选项WORLD_READABLE 。getSharedPreferences()

于 2013-10-25T06:03:24.163 回答