使用Icepick保存表面视图状态

时间:2019-05-19 19:48:06

标签: android parceler

我正在尝试构建游戏,我正在使用Surfaceview在UI上绘制组件,以保存和恢复组件的状态,而我正在使用Icepick库,但是它似乎无法正确保存变量的值 以下是我的代码。

MainActivity类

int g_slider_position = 0;
int g_run = 1, g_dontset = 0;
VideoCapture g_cap;

void onTrackbarSlide(int pos, void *) {
    g_cap.set(CAP_PROP_POS_FRAMES, pos);
    if (!g_dontset)
        g_run = 1;
    g_dontset = 0;
}

int main()
{
    namedWindow("MyMovie", WINDOW_AUTOSIZE);
    g_cap.open("Movie.mp4");
    int frames = (int)g_cap.get(CAP_PROP_FRAME_COUNT);
    int tmpw = (int)g_cap.get(CAP_PROP_FRAME_WIDTH);
    int tmph = (int)g_cap.get(CAP_PROP_FRAME_HEIGHT);
    cout << "Video has " << frames << " frames of dimensions( "
        << tmpw << ", " << tmph << " )." << endl;

    createTrackbar("Position", "MyMovie", &g_slider_position,
        frames, onTrackbarSlide);

    Mat frame;

    for (;;) {
        if (g_run != 0) {
            g_cap >> frame; if (frame.empty()) break;
            int current_pos = (int)g_cap.get(CAP_PROP_POS_FRAMES);
            g_dontset = 1;

            setTrackbarPos("Position", "MyMovie", current_pos);
            imshow("MyMovie", frame);

            g_run -= 1;
        }


        char c = (char)waitKey(10);
        if (c == 's') // single step
        {
            g_run = 1; cout << "Single step, run = " << g_run << endl;
        }
        if (c == 'r') // run mode
        {
            g_run = -1; cout << "Run mode, run = " << g_run << endl;
        }
        if (c == 27) break;
    }
    return 0;
}

SurfaceView类

public class MainActivity extends Activity {
MySurfaceView surfaceView;
MySurfaceViewThread surfaceViewThread;

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

        setContentView(R.layout.activity_main);

        surfaceView = (MySurfaceView) findViewById((R.id.surfaceView));
        surfaceViewThread= surfaceView.getThread();
        surfaceView.setActivity(this);

       Icepick.restoreInstanceState(this,savedInstanceState);
       // if(savedInstanceState!=null)
        //    surfaceView.restoreState(savedInstanceState);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
/*
@Override
protected void onResume() {
    surfaceView.onResume();
    super.onResume();
}

@Override
protected void onPause() {
    surfaceView.onPause();
    super.onPause();
}
*/

@Override public void onSaveInstanceState(Bundle state)
{
    super.onSaveInstanceState(state);
    Icepick.saveInstanceState(this, state);

    // surfaceView.saveState(state);
}

存储前变量的值

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
@State int Screen_Width;
@State int Screen_Height;

@Override public Parcelable onSaveInstanceState()
{
     Log.d(TAG,"Inside onSaveInstanceState method");
      return Icepick.saveInstanceState(this,super.onSaveInstanceState());
}

 @Override public void onRestoreInstanceState(Parcelable state)
 {
 super.onRestoreInstanceState(Icepick.restoreInstanceState(this,state));
 }
 }

还原后变量的值

Screen_Height='502'
Screen_Width='897'

我无法理解为什么更改值。我也对清单文件做了confing更改。

Screen_Height='850'
 Screen_Width='540'

<?xml version="1.0" encoding="utf-8"?>

您能帮我修复我的代码吗?预先感谢。

0 个答案:

没有答案