在android studio

时间:2016-05-27 14:42:39

标签: android

我的应用程序有一个图像视图和一个按钮,当我触摸按钮时,图像视图设置例如不可见或在另一个中更改。问题是当我关闭应用程序并再次打开图像视图的原始状态回来时,我想保存图像视图的新状态但我不知道如何,我需要使用共享首选项或存在另一种方式?感谢

我的代码:

public class MainActivity extends Activity {

    ImageView iv1;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv1=(ImageView)findViewById(R.id.iv1);
        b1=(Button)findViewById(R.id.b1);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void a(View view){
        iv1.setVisibility(View.INVISIBLE);
    }
}

4 个答案:

答案 0 :(得分:0)

关闭应用程序或在收到内存警告时强制关闭操作系统时,所有实例变量都会丢失,重新打开时应用程序会重新启动。

为了做你想做的事,你需要将ImageView状态保持在一致的存储中。 SharedPreferences非常适合这些情况,每次单击按钮时都保存值(您可以为可见/不可见状态保存布尔值或图像引用字符串[url,本地文件路径..])然后,在findViewById之后的onCreate中你应该从首选项中检索已保存的状态,并根据需要呈现ImageView。

答案 1 :(得分:0)

简单的答案使用任何类型的持久性来存储你的状态。如果你想保持状态,即使应用程序被杀死或强制关闭,那么请选择Preference,或试试这个https://developer.android.com/training/basics/activity-lifecycle/recreating.html

答案 2 :(得分:0)

使用SharedPreferences非常简单:

private static final String KEY_VISIBILITY = "image_visibility";

private Button mButton;
private ImageView mImageView;

private SharedPreferences mSharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mSharedPreferences = getPreferences(Context.MODE_PRIVATE);

    mButton = (Button) findViewById(R.id.b1);
    mImageView = (ImageView) findViewById(R.id.iv1);

    mImageView.setVisibility((mSharedPreferences.getBoolean(KEY_VISIBILITY, true)
            ? View.VISIBLE : View.INVISIBLE);
}

public void onImageViewClick(View view) {
    boolean visibile = mImageView.getVisibility == View.VISIBLE;
    mImageView.setVisibility(visible ? View.INVISIBLE : View.VISIBLE);

    mSharedPreferences.edit().putBoolean(KEY_VISIBILITY, !visible).apply();
}

答案 3 :(得分:0)

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
            frameLayout_forBitmap.setDrawingCacheEnabled(true);
            frameLayout_forBitmap.buildDrawingCache();
            Bitmap cache = frameLayout_forBitmap.getDrawingCache();
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(GlobalConfig.FilePath+"/Pic_"+ simpleDateFormat.format(new Date())+".jpg");
                cache.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                Toast.makeText(this, "Image Save", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                // TODO: handle exception
            } finally {
                frameLayout_forBitmap.destroyDrawingCache();
            }