旋转屏幕时,Android自定义视图(位图)丢失数据

时间:2016-08-14 09:29:52

标签: android screen-orientation

我对此有类似的问题one.现在我的问题是我不知道该放什么public class FormattingUtils { /** * Return the ordinal of a cardinal number (positive integer) (as per common usage rather than set theory). * {@link http://stackoverflow.com/questions/6810336/is-there-a-library-or-utility-in-java-to-convert-an-integer-to-its-ordinal} * * @param i * @return * @throws {@code IllegalArgumentException} */ public static String ordinal(int i) { if (i < 0) { throw new IllegalArgumentException("Only +ve integers (cardinals) have an ordinal but " + i + " was supplied"); } String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; switch (i % 100) { case 11: case 12: case 13: return i + "th"; default: return i + sufixes[i % 10]; } } } import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; public class WhenWeCallFormattingUtils_Ordinal { @Test public void theEdgeCasesAreCovered() { int[] edgeCases = { 0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 100, 101, 102, 103, 104, 111, 112, 113, 114 }; String[] expectedResults = { "0th", "1st", "2nd", "3rd", "4th", "5th", "10th", "11th", "12th", "13th", "14th", "20th", "21st", "22nd", "23rd", "24th", "100th", "101st", "102nd", "103rd", "104th", "111th", "112th", "113th", "114th" }; for (int i = 0; i < edgeCases.length; i++) { assertThat(FormattingUtils.ordinal(edgeCases[i])).isEqualTo(expectedResults[i]); } } @Test(expected = IllegalArgumentException.class) public void supplyingANegativeNumberCausesAnIllegalArgumentException() { FormattingUtils.ordinal(-1); } } 第一个答案是给我这个错误  private int stuff;

'SavedState()' is not public in android.app.Fragment.SavedState'. Cannot be accessed from outside package.

我尝试的事情:

how can I save a bitmap with onRetainNonConfigurationInstance() for screen orientation?

^这个人给了我 //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); }

This view's id is id/view. Make sure other views do not use the same id.

http://it-ride.blogspot.co.nz/2010/08/save-image-in-instance-state-android.html

1 个答案:

答案 0 :(得分: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];
        }

    };
}