Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 视图未保存在屏幕方向开关上_Android - Fatal编程技术网

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

Android 视图未保存在屏幕方向开关上,android,Android,应用程序应该保存它在方向改变时绘制的视图,但它没有。 谁能说出问题出在哪里 我没有收到任何错误,但当我更改设备方向时,视图将重新启动,而不保存我绘制的内容 public DrawingView(Context context, AttributeSet attrs) { super(context, attrs); conAdd(); } public DrawingView(Context context) { super(context); conAdd()

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

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

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);
    }

}

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

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

<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()而不是返回您自己的捆绑包(
收件箱
)。因此,对保存的数据不做任何处理。

为了保持数据状态,您不需要覆盖方向更改功能。在简单的非专业应用程序上,处理被销毁和重新创建的活动的生命周期可能会很费劲,但这并不意味着您应该禁用它。手动加载屏幕大小/方向的备用资源只是许多例子中的一个,如果没有这种机制,它将成为一场噩梦;应该在其他人之前code@QuintinBalsdon是正确的,还有,您的
onCreate()
在哪里?创建视图的函数?您可能正在覆盖还原的视图..这不是解决方案。您必须保存实例状态!