Android 旋转屏幕时自定义视图(位图)丢失数据
Android Custom View (Bitmap) losing data when screen is rotated
我有与此类似的问题one.现在我的问题是我不知道在这里放什么private int stuff;
第一个答案是给我这个错误
'SavedState()' is not public in android.app.Fragment.SavedState'. Cannot be accessed from outside package.
.
//no idea what to do here.
private int stuff;
private Bitmap customBitmap;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
customBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
customCanvas = new Canvas(customBitmap);
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
//Draws stuff into the canvas.
canvas.drawBitmap(customBitmap, 0, 0, linePaintOne);
}
@Override
protected Parcelable onSaveInstanceState()
{
Bundle bundle = new Bundle();
bundle.putParcelable("draw", super.onSaveInstanceState());
bundle.putParcelable("bitmap", customBitmap);
bundle.putInt("bitmap", this.stuff);
System.out.println("onSave");
//return super.onSaveInstanceState();
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle)
{
Bundle bundle = (Bundle) state;
customBitmap = bundle.getParcelable("bitmap");
this.stuff = bundle.getInt("stuff");
state = bundle.getParcelable("draw");
System.out.println("onRestore1");
}
System.out.println("onRestore2");
super.onRestoreInstanceState(state);
}
我尝试过的事情:
• how can I save a bitmap with onRetainNonConfigurationInstance() for screen orientation?
^ 这个给我 This view's id is id/view. Make sure other views do not use the same id.
.
@Override
protected Parcelable onSaveInstanceState()
{
super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", customBitmap);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
Bundle bundle = new Bundle();
customBitmap = bundle.getParcelable("bitmap");
}
• http://it-ride.blogspot.co.nz/2010/08/save-image-in-instance-state-android.html
您需要将自己的 SavedState 版本实现为自定义视图的内部 class。它有点冗长,因为它必须实现 Parcelable。它会是这样的:
@Override
protected Parcelable onSaveInstanceState() {
return new SavedState(
super.onSaveInstanceState(),
customBitmap,
stuff
);
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
stuff = savedState.getStuff();
customBitmap = savedState.getBitmap();
}
protected static class SavedState extends BaseSavedState {
private final Bitmap bitmap;
private final int stuff;
public SavedState(Parcelable superState, Bitmap bitmap, int stuff) {
super(superState);
this.bitmap = bitmap;
this.stuff = stuff;
}
public SavedState(Parcel source) {
super(source);
this.bitmap = Bitmap.CREATOR.createFromParcel(source);
this.stuff = source.readInt();
}
public Bitmap getBitmap() {return bitmap;}
public int getStuff() {return stuff;}
@Override
public void writeToParcel(Parcel destination, int flags) {
super.writeToParcel(destination, flags);
bitmap.writeToParcel(destination, 0);
destination.writeInt(stuff);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
我有与此类似的问题one.现在我的问题是我不知道在这里放什么private int stuff;
第一个答案是给我这个错误
'SavedState()' is not public in android.app.Fragment.SavedState'. Cannot be accessed from outside package.
.
//no idea what to do here.
private int stuff;
private Bitmap customBitmap;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
customBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
customCanvas = new Canvas(customBitmap);
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
//Draws stuff into the canvas.
canvas.drawBitmap(customBitmap, 0, 0, linePaintOne);
}
@Override
protected Parcelable onSaveInstanceState()
{
Bundle bundle = new Bundle();
bundle.putParcelable("draw", super.onSaveInstanceState());
bundle.putParcelable("bitmap", customBitmap);
bundle.putInt("bitmap", this.stuff);
System.out.println("onSave");
//return super.onSaveInstanceState();
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle)
{
Bundle bundle = (Bundle) state;
customBitmap = bundle.getParcelable("bitmap");
this.stuff = bundle.getInt("stuff");
state = bundle.getParcelable("draw");
System.out.println("onRestore1");
}
System.out.println("onRestore2");
super.onRestoreInstanceState(state);
}
我尝试过的事情:
• how can I save a bitmap with onRetainNonConfigurationInstance() for screen orientation?
^ 这个给我 This view's id is id/view. Make sure other views do not use the same id.
.
@Override
protected Parcelable onSaveInstanceState()
{
super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", customBitmap);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
Bundle bundle = new Bundle();
customBitmap = bundle.getParcelable("bitmap");
}
• http://it-ride.blogspot.co.nz/2010/08/save-image-in-instance-state-android.html
您需要将自己的 SavedState 版本实现为自定义视图的内部 class。它有点冗长,因为它必须实现 Parcelable。它会是这样的:
@Override
protected Parcelable onSaveInstanceState() {
return new SavedState(
super.onSaveInstanceState(),
customBitmap,
stuff
);
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
stuff = savedState.getStuff();
customBitmap = savedState.getBitmap();
}
protected static class SavedState extends BaseSavedState {
private final Bitmap bitmap;
private final int stuff;
public SavedState(Parcelable superState, Bitmap bitmap, int stuff) {
super(superState);
this.bitmap = bitmap;
this.stuff = stuff;
}
public SavedState(Parcel source) {
super(source);
this.bitmap = Bitmap.CREATOR.createFromParcel(source);
this.stuff = source.readInt();
}
public Bitmap getBitmap() {return bitmap;}
public int getStuff() {return stuff;}
@Override
public void writeToParcel(Parcel destination, int flags) {
super.writeToParcel(destination, flags);
bitmap.writeToParcel(destination, 0);
destination.writeInt(stuff);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}