通过进度条更改后保存图片

时间:2014-05-14 01:47:09

标签: android image progress-bar

我正在尝试根据进度条进度更改图片。像这样:

@Override
                    public void onProgressChanged(SeekBar arg0,
                            int progress, boolean arg2) {
                        audioManager.setStreamVolume(
                                AudioManager.STREAM_RING, progress, 0);

                        if (volumeSeekbar1.getProgress() == 0) {
                            im112.setBackgroundResource(R.drawable.vibrate);

                        }
                        if (volumeSeekbar1.getProgress() == 1) {
                            im112.setBackgroundResource(R.drawable.media1);

                        }
                        if (volumeSeekbar1.getProgress() == 2) {
                            im112.setBackgroundResource(R.drawable.media1);

                        }
                        if (volumeSeekbar1.getProgress() == 3) {
                            im112.setBackgroundResource(R.drawable.media1);

                        }
                        if (volumeSeekbar1.getProgress() == 4) {
                            im112.setBackgroundResource(R.drawable.media1);

                        }
                        if (volumeSeekbar1.getProgress() == 5) {
                            im112.setBackgroundResource(R.drawable.sound);

                        }
                        if (volumeSeekbar1.getProgress() == 6) {
                            im112.setBackgroundResource(R.drawable.sound);

                        }
                        if (volumeSeekbar1.getProgress() == 7) {
                            im112.setBackgroundResource(R.drawable.sound);

                        }
                        if (volumeSeekbar1.getProgress() == 8) {
                            im112.setBackgroundResource(R.drawable.sound);

                        }
                    }
                });

正如您所看到的,当我移动进度条时图片确实发生了变化,问题是我退出图片的时间默认为之前的状态。如何保存 如果我退出应用程序,它就不会更改回默认图标。

1 个答案:

答案 0 :(得分:0)

您可以使用SharedPreferences来存储和恢复资源ID。例如: 存储ID:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("final_image", R.drawable.sound);
editor.commit();

在您的应用恢复时阅读ID:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = default_image_id;
int image_id = sharedPref.getInt("final_image", defaultValue);
im112.setBackgroundResource(image_id);