2016-08-14 134 views
0

我有這個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

回答

0

您需要實現自己的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]; 
     } 

    }; 
} 
相關問題