在学院科技文化节的时候做了一个安卓小应用,实现了:加载图片——人脸分析——添加搞怪表情。
问题:
加载了图片或者添加了表情之后,锁屏,解锁,界面回到了原始状态(图片丢失了)。
原因:
当解锁的时候,自动调用了一次 oncreat,变量们都重新 new 了一次,所以就回到原始状态了。
解决:
当解锁屏幕的时候,方法调用如下图
我们需要做的是,重写 onSaveInstanceState 方法。
To save additional state information for your activity, you must implement onSaveInstanceState() and add key-value pairs to the Bundle object——android 官网
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | protected void onSaveInstanceState(Bundle outState) { // 保存状态,防止锁屏后图片丢失 // TODO Auto-generated method stub outState.putParcelable("NewBitmap",NewBitmap); outState.putParcelable("img", img); outState.putParcelable("bitmap", bitmap); outState.putInt("ifLoaded", ifLoaded); outState.putFloat("eye_left_x", eye_left_x); outState.putFloat("eye_left_y", eye_left_y); outState.putFloat("eye_right_x", eye_right_x); outState.putFloat("eye_right_y", eye_right_y); outState.putFloat("nose_x", nose_x); outState.putFloat("nose_y", nose_y); outState.putFloat("mouth_x", mouth_x); outState.putFloat("mouth_y", mouth_y); System.out.println("onSaveInstanceState"); super.onSaveInstanceState(outState); } |
把需要保存的数据都 put 进这个 Bundle 对象。
我这里保存的是,bitmap,和一些用来画表情的一些坐标(眼耳口鼻)。
重写 onRestoreInstanceState 把刚才保存的值取出来:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | protected void onRestoreInstanceState(Bundle savedInstanceState) { // 恢复状态数据 // TODO Auto-generated method stub ifLoaded=savedInstanceState.getInt("ifLoaded"); NewBitmap=savedInstanceState.getParcelable("NewBitmap"); img=savedInstanceState.getParcelable("img"); bitmap=savedInstanceState.getParcelable("bitmap"); eye_left_x=savedInstanceState.getFloat("eye_left_x"); eye_left_y=savedInstanceState.getFloat("eye_left_y"); eye_right_x=savedInstanceState.getFloat("eye_right_x"); eye_right_y=savedInstanceState.getFloat("eye_right_y"); nose_x=savedInstanceState.getFloat("nose_x"); nose_y=savedInstanceState.getFloat("nose_y"); mouth_x=savedInstanceState.getFloat("mouth_x"); mouth_y=savedInstanceState.getFloat("mouth_y"); if(img!=null){ imageView.setImageBitmap(img); } System.out.println("onRestoreInstanceState"); super.onRestoreInstanceState(savedInstanceState); } |
OK,问题解决了。
以上是方法一,还有一种方法就是这样的:
引用官网的代码,
1 2 3 4 5 6 7 8 9 10 11 12 13 | protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } ... } |
换成我的代码的话,我只需变动 if 里面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ifLoaded=savedInstanceState.getInt("ifLoaded"); NewBitmap=savedInstanceState.getParcelable("NewBitmap"); img=savedInstanceState.getParcelable("img"); bitmap=savedInstanceState.getParcelable("bitmap"); eye_left_x=savedInstanceState.getFloat("eye_left_x"); eye_left_y=savedInstanceState.getFloat("eye_left_y"); eye_right_x=savedInstanceState.getFloat("eye_right_x"); eye_right_y=savedInstanceState.getFloat("eye_right_y"); nose_x=savedInstanceState.getFloat("nose_x"); nose_y=savedInstanceState.getFloat("nose_y"); mouth_x=savedInstanceState.getFloat("mouth_x"); mouth_y=savedInstanceState.getFloat("mouth_y"); if(img!=null){ imageView.setImageBitmap(img); } } |
也是这样就可以把保存的数据取出来了。
我的馥这几天不知道怎么了。~ 又耍脾气不理我了~ 女人心真难懂~ 唉。