Question

I'm developing an painting app at the moment for the first time. I can draw and such things. But everytime the orientation changes, everything is lost. How can i fix this? I have been searching on the internet for solution but dont seem to find one. I know it has to to with the onSizeChanged event from the view class. I have overriden that event and changed a lot to make it work with the orientation change but i cant get it to work. Here is the code from the onSizeChanged event:

private Canvas drawCanvas;
private Bitmap canvasBitmap;

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    //View given size
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

I have already tryed android:configChanges. But it doesnt seem to work.

Was it helpful?

Solution

I have found an solution for this problem. Everytime the screen orientation changed the event was being called. So the bitmap was recreated everytime. I have fixed this by scaling the bitmap before recreation and then setting it as new bitmap. This way it is automatticly scaled and nothing is lost. This is the full code now:

private Canvas drawCanvas;
private Bitmap canvasBitmap;

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    //View given size
    super.onSizeChanged(w, h, oldw, oldh);
    if(canvasBitmap == null){
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    }else{
        Bitmap temporary = Bitmap.createScaledBitmap(canvasBitmap, w, h, true);
        canvasBitmap = temporary;
    }
    drawCanvas = new Canvas(canvasBitmap);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top