9

I'm implementing a fairly standard app with the Android sdk that involves drawing using the SurfaceView, SurfaceHolder, Callback setup.

In my main thread (UI thread) I have no drawing or handling of the SurfaceHolder (or the canvas you retrieve with it).

In a separate thread I have the following:

Log.i("GAME.DrawThread", "run()");
        Log.i("GAME.DrawThread", Thread.currentThread().getName());
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            synchronized(holder) {
                Log.i("GAME", "draw():synchronized");
                Paint paint = new Paint();
                paint.setColor(R.color.draw_color);
                canvas.drawColor(R.color.draw_color);
                canvas.drawLine(0, 0, 500, 500, paint);
            }
        } catch (SurfaceHolder.BadSurfaceTypeException e) {
            Log.e("GAME", "onDraw():  BadSurfaceTypeException");
        } finally {
            if (canvas != null) {
                holder.unlockCanvasAndPost(canvas);
            }
        } 

This code is being executed, throws no exceptions, and has no negative side effects that I can find; however, the unlockCanvasAndPost() call never causes onDraw() to be called.

In other words, unlockCanvasAndPost() does not cause a redraw of the SurfaceView.

Any ideas what could cause this symptom? I have plenty of java experience, a fair amount of android experience, and a lot of debugging experience and cannot track this one down.

Thanks in advance.

cblades
  • 416
  • 3
  • 11

4 Answers4

13

So it turns out that when using SurfaceView you draw to a Surface that is underneath a Window. I was setting the background color of the View in xml; it turns out that sets the background color of the Window, not the Surface. In effect, I made the Window opaque so that you couldn't see the Surface underneath.

Lesson Learned.

cblades
  • 416
  • 3
  • 11
  • Wow, thanks for following up! I had the same problem and don't know how long it would've taken to find it. I spent a few hours on it before I found the solution here. Ugh. – spartygw May 03 '13 at 15:40
  • I've been hours digging for an answer why the `unlockCanvasAndPost()`would not refresh the screen although all drawing methods were executed... Finally came across your answer and realized it was my equal issue, I was setting a black background for the view and the surfaceview was drawing beneath it.. I nearly went mad already :P – JorgeGRC Feb 27 '15 at 10:51
0

That's not how SurfaceView works. Calling unlockCanvasAndPost() does not invoke onDraw(), that's the whole point of using a SurfaceView. A SurfaceView's surface lives in a different window.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
0

This is old, but I have a feeling the problem is there is no surfaceholder callback. He was trying to draw on the surface before the surface was created

Chris Stryker
  • 1,058
  • 2
  • 10
  • 18
0

For set SurfaceView to top Z-View index, add to youre CustomerSurfaceView a code below:

init {
   setZOrderOnTop(true)
}
amiron
  • 721
  • 9
  • 11