Я сделал пользовательский вид, который можно нарисовать, и рисунок будет сохранен в массив. Я хочу сохранить эти массивы в onSaveInstanceState и получить их в onRestoreInstanceState. Я попытался использовать Sharedpreferences с Json, и он не работает и даже не распределяется. Я хочу сохранить путь и краски.
Класс ручки:
private static class Pen implements Parcelable {
Path path;
Paint paint;
int color;
float width;
Pen(int colort, float widtht ) {
color = colort;
width = widtht;
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
// De-parcel Pen object
public Pen(Parcel in) {
color = in.readInt();
width = in.readFloat();
path = new Path();
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(width);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}
// Function that writes Dice values to parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(color);
dest.writeFloat(width);
}
// Creator of Parcelable
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Pen createFromParcel(Parcel in) {
return new Pen(in);
}
public Pen[] newArray(int size) {
return new Pen[size];
}
};
// Function to implement Parcelable interface
@Override
public int describeContents() {
return 0;
}
}
OnsaveInstanceState:
@Override
public Parcelable onSaveInstanceState()
{
Bundle bundle = new Bundle();
bundle.putParcelable("superState", super.onSaveInstanceState());
bundle.putParcelableArrayList("pen", mPenList);
return bundle;
}
onRestoreInstanceState:
@Override
public void onRestoreInstanceState(Parcelable state)
{
if (state instanceof Bundle) // implicit null check
{
Bundle bundle = (Bundle) state;
this.mPenList = bundle.getParcelableArrayList("pen");
state = bundle.getParcelable("superState");
}
super.onRestoreInstanceState(state);
}