安卓SurfaceHolder.unlockCanvasAndPost()不会导致重绘SurfaceHolder、unlockCanvasAndPost

2023-09-06 00:24:16 作者:不爱几个人渣怎么货比三家

我实施了Android SDK,涉及使用SurfaceView,SurfaceHolder,回拨设置绘制一个相当标准的应用程序。

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

在我的主线程(UI线程)我有SurfaceHolder(或您检索与之画布)是没有绘图或处理。

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);
            }
        } 

在执行此code,没有抛出异常,而且没有负面影响,我可以找到;然而,unlockCanvasAndPost()调用永远不会导致的OnDraw()被调用。

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.

在换句话说,unlockCanvasAndPost()不会导致SurfaceView的重绘。

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

任何想法可能会导致这种症状?我有足够的经验的Java,Android的体验相当数量,和大量的调试经验,无法追踪这一轮下来。

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.

在此先感谢。

推荐答案

所以,事实证明,使用SurfaceView当你画到一个表面,一个窗口的下方。我设置了查看XML中的背景色;事实证明,把窗口的背景色,而不是曲面。事实上,我做了窗口不透明,让你看不到的表面之下。

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.

教训。