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

时间:2012-08-15 08:33:04

标签: android

该应用程序应该保存它在方向更改时绘制的视图,但事实并非如此。 谁能说出问题出在哪里?

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

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

}

4 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

您是否尝试过使用

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

注意到单独“orientation”无法提供它时,添加“screenSize”选项就可以了!

答案 2 :(得分:0)

您的功能不正确:

@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
}

答案 3 :(得分:0)

您将返回super.onSaveInstanceState();而不是返回自己的包(inBox)。因此,您保存的数据没有任何效果。