Android自定义/复合控制:Parcelable状态是共享的,它不应该?

时间:2011-11-29 21:07:13

标签: android view controls instance state

我在Android应用程序中创建了一个自定义视图/复合控件,用户可以“绘制”他们的签名。

签名包含在Canvas中,链接到Bitmap对象。

我希望在更改方向时保留签名“图像”。因此,我通过存储位图的像素实现了onSaveInstanceState / onRestoreInstanceState:

@Override
public Parcelable onSaveInstanceState()
{
    // Call superclass method, retrieve Parcelable
    Parcelable superState = super.onSaveInstanceState();

    if (pictureBitmap_ != null)
    {
        // Retrieve the current pixels array.
        int totalSize = pictureBitmap_.getWidth()
                * pictureBitmap_.getHeight();

        int pixels[] = new int[totalSize];

        pictureBitmap_.getPixels(pixels,
            0,
            pictureBitmap_.getWidth(),
            0,
            0,
            pictureBitmap_.getWidth(),
            pictureBitmap_.getHeight());

        // Create the saved state object.
        SavedState savedState = new SavedState(superState);
        savedState.pixels = pixels;

        // Return the populated saved state object.
        return savedState;
    }
    else
    {
        // Simply pass original Parcelable along
        return superState;
    }
}

@Override
public void onRestoreInstanceState(Parcelable state)
{
    // Is the Parcelable an instance of our custom SavedState?
    if (!(state instanceof SavedState))
    {
        // No, simply delegate to superclass.
        super.onRestoreInstanceState(state);
        return;
    }

    // Retrieve custom state object.
    SavedState savedState = (SavedState) state;

    // Use superclass to restore original state.
    super.onRestoreInstanceState(savedState.getSuperState());

    // Restore custom state (transfer pixels).
    this.transferPixels_ = savedState.pixels;

    // If the picture bitmap is already initialized...
    if (pictureBitmap_ != null)
    {
        // Transfer the pixels right away.
        transferBitmap();

        // Refresh the canvas
        drawSignature();
    }
}

整数像素数组存储在SavedState对象中,该对象是BaseSavedState的自定义子类:

private static class SavedState
        extends BaseSavedState
{
    // Members
    int[] pixels;

    // Methods
    SavedState(Parcelable superState)
    {
        super(superState);
    }

    private SavedState(Parcel in)
    {
        super(in);
        in.readIntArray(pixels);
    }

    @Override
    public void writeToParcel(Parcel out, int flags)
    {
        super.writeToParcel(out, flags);
        out.writeIntArray(pixels);
    }

    // required field that makes Parcelables from a Parcel
    @SuppressWarnings("unused")
    public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>()
    {
        public SavedState createFromParcel(Parcel in)
        {
            return new SavedState(in);
        }

        public SavedState[] newArray(int size)
        {
            return new SavedState[size];
        }
    };
}

现在,虽然如果我的活动中只有一(1)个自定义视图实例,但这很有效...如果我有两(2)个或更多实例,则会出现问题:当我翻转方向时,所有我的控件最终显示相同的签名,这是最后一个控件的签名。

换句话说,存储在SavedState对象中的最后一组像素似乎成为我的Activity中所有控件实例的“通用”像素集。

如何确保每个控件都有自己独立的保存状态?

0 个答案:

没有答案