Android SurfaceHolder.unlockCanvasAndPost() does not cause redraw

◇◆丶佛笑我妖孽 提交于 2019-12-04 19:22:26

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/3818284/android-surfaceholder-unlockcanvasandpost-does-not-cause-redraw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!