I am using a SurfaceView to draw to a part of the screen, whereby I update the drawing with a background thread. The surfaceview implements a SurfaceView.callback.
On the onDraw method I am using canvas.drawpont(x,y,paint) and draw to pretty much the entire canvas pixel by pixel, so it can be quite intensive on large screens. Anyway, I get an illegalArgumentException if I rotate the phone quickly between landscape and reverse landscape. As Follows:
06-18 15:03:05.853: E/AndroidRuntime(29969): FATAL EXCEPTION: Thread-45
06-18 15:03:05.853: E/AndroidRuntime(29969): java.lang.IllegalArgumentException
06-18 15:03:05.853: E/AndroidRuntime(29969): at Android.view.Surface.unlockCanvasAndPost(Native Method)
06-18 15:03:05.853: E/AndroidRuntime(29969): at android.view.SurfaceView$3.unlockCanvasAndPost(SurfaceView.java:793)
06-18 15:03:05.853: E/AndroidRuntime(29969): at com.enhancement.SpecGuageUpdate.run(SpecGuageUpdate.java:313)
And on SpecGugaeupdate at 313, I have:
mHolder.unlockCanvasAndPost(canvas);
Where mHolder is my SurfaceHolder. And of course I have also locked the canvas prior to calling onDraw.
This method works flawlessly if I rotate slowly in every direction, however, the moment I quickly rotate 180degs between the two landscape states it triggers this illegalArgumentException. Upon monitoring the width and height in surfaceChanged, I noticed that when I rotate quickly, the width of landscape get paired with the height of portrait. For example, on my phone (Motorola atrix) the dimensions of the SurfaceView for portrait are 540x307 (WidthxHeight), and for landscape are 960x172. This is what happens when I rotate quickly between landscape and reverse landscape:
Rotate to landscape slowly:
960x172
Rotate quickly to reverse landscape, surface changed gets called twice.
960x307 - 1st
960x172 - 2nd
I have tried overriding onMeasure and the width and Height are identical to that of SurfaceChanged. So it seems I am trying to draw on a canvas which is not quite ready, but ready enough to be able to lock it to my Holder. Any idea's would be great, thanks for your time.
Additionally, in my surfacedestroyed I am handling my thread like this:
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
specupdate.join();
retry = false;
} catch (InterruptedException e) {
}
}
Which stops the thread from executing onDraw.