视图未保存在屏幕方向开关上



该应用程序应该保存它在方向更改时绘制的视图,但它没有。谁能说出问题出在哪里?

我没有收到任何错误,但是当我更改设备方向时,视图会重新启动而不保存我绘制的内容。

public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    conAdd();
}
public DrawingView(Context context) {
    super(context);
    conAdd();
}
private void conAdd() {
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.GREEN);
    paint.setStyle(Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(width);
    path = new Path();
    setOnTouchListener(this);
    setBackgroundColor(Color.BLACK);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
    Canvas canvasNew = new Canvas();
    canvasNew.setBitmap(bitmap);
    canvas = canvasNew;
    super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path, paint);
    canvas.drawBitmap(bitmap, 0, 0, paint);
}
public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        path.reset();
        path.moveTo(x, y);
        invalidate();
    }
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        if (Math.abs(x - lastX) > 4 || Math.abs(y - lastY) > 4) {
            path.quadTo(lastX, lastY, (lastX + x) / 2, (lastY + y) / 2);
            invalidate();
        }
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        path.lineTo(x, y);
        canvas.drawPath(path, paint);
    }
    lastX = x;
    lastY = y;
    return true;
}

 @Override
    protected Parcelable onSaveInstanceState() {
        Bundle inBox = new Bundle();
        inBox.putParcelable("draw", super.onSaveInstanceState());
        inBox.putParcelable("bitmap", bitmap);
        return super.onSaveInstanceState();
    }

 @Override
    protected void onRestoreInstanceState(Parcelable state) 
    {
         if (state instanceof Bundle) {
              Bundle outBox = (Bundle) state;
              this.bitmap = outBox.getParcelable("bitmap");
              super.onRestoreInstanceState(outBox.getParcelable("draw"));
              return;
         }
         super.onRestoreInstanceState(state);
    }

}

您需要在运行时管理配置更改 - 我也花了很长时间才找出它在哪里:

http://developer.android.com/guide/topics/resources/runtime-changes.html

引用:
以下清单代码声明处理屏幕方向更改和键盘可用性更改的活动:

<activity android:name=".MyActivity"
    android:configChanges="orientation|keyboardHidden"
    android:label="@string/app_name">

你试过使用

<activity android:name=".YourActivity" android:configChanges="orientation|screenSize">

请注意,虽然仅"方向"可能无法实现它,但添加"屏幕大小"选项就可以了!

您的函数不正确:

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    // Your code here
    // Call at the end
    super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
    // Call at the start
    super.onRestoreInstanceState(savedInstanceState);
    // Your code here
}

您返回的是super.onSaveInstanceState();而不是返回您自己的捆绑包(inBox)。因此,不会对您保存的数据执行任何操作。

最新更新