Android的自定义视图(位图)旋转屏幕方向
问题描述:
我有这个one.类似的问题,现在我的问题是我不知道要放什么东西在这里private int stuff;
第一个答案是给我这个错误 'SavedState()' is not public in android.app.Fragment.SavedState'. Cannot be accessed from outside package.
丢失数据。Android的自定义视图(位图)旋转屏幕方向
//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版本作为一个内部类自定义视图。它有点冗长,因为它必须实现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];
}
};
}