Android SurfaceView不显示onDraw

5
为了简化问题,我在SurfaceView上绘制一个整数,每次绘制时该整数增加1。 实际上,这个整数是在不断增加的,因为我可以在System.out上看到。 但是屏幕上的文本仍然是“0”。 谁能告诉我我做错了什么?
SurfaceViewTest.java
package com.niek.surfaceviewtest;

import android.app.Activity;
import android.os.Bundle;

public class SurfaceViewTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

StatusView.java

package com.niek.surfaceviewtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class StatusView extends SurfaceView implements SurfaceHolder.Callback {

    private int tmp;
    private DrawThread drawThread;

    public StatusView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
        setFocusable(true);
        drawThread = new DrawThread(getHolder());
    }

    @Override
    public void onDraw(Canvas c) {
            c.drawColor(Color.BLACK);
        Paint p = new Paint();
        p.setColor(Color.RED);
        c.drawText(tmp + "", 10, 10, p);
        tmp++;

        System.out.println(tmp);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        drawThread.setRunning(true);
        drawThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        drawThread.setRunning(false);
        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

    protected class DrawThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private boolean isRunning;

        public DrawThread(SurfaceHolder surfaceHolder) {
            this.surfaceHolder = surfaceHolder;
            isRunning = false;
        }

        public void setRunning(boolean run) {
            isRunning = run;
        }

        public void run() {
            Canvas c;
            while (isRunning) {
                c = null;
                try {
                    c = surfaceHolder.lockCanvas(null);
                    synchronized (surfaceHolder) {
                        onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">

    <com.niek.surfaceviewtest.StatusView
        android:id="@+id/statusview1"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:background="#000000"
 />
</LinearLayout>

1
[thread-necro] 这不起作用,因为它混淆了SurfaceView的Surface和View部分。 onDraw() 用于在 View 部分绘制,但是对于 SurfaceView,View 应该是一个透明的孔(由布局代码需要),使您可以看到独立的 Surface 层后面。第一次调用 onDraw() 将视图清除为 不透明 的黑色,因此在 Surface 上渲染的任何内容都不可见。上面的实现,结合下面的接受答案,会导致具有不必要开销的自定义 View。为 View 指定背景也是一个坏主意。 - fadden
3个回答

4
我的唯一猜测是,因为视图没有被无效化,所以绘画没有在表面上进行。你应该调用invalidate()来进行绘制,然后让框架调用onDraw()。也许这就是为什么tmp会增加,但绘画操作只在第一次到达表面的原因。
也许值得尝试:把Canvas c变成StatusView的成员,然后替换掉。
synchronized (surfaceHolder) {
  onDraw(c);
}

使用

synchronized (surfaceHolder) {
  invalidate();
}

这个可以吗?


那确实起作用了。但是我不得不调用postInvalidate()而不是invalidate(),否则就会抛出CalledFromWrongThreadException异常。 仍然觉得很奇怪,因为我基本上将90%的代码从这里复制粘贴过来。 - nhaarman
当然。我在链接的文档中看到了这个说明!“这必须从UI线程调用。要从非UI线程调用,请调用postInvalidate()。”很高兴它起作用了。 - Steve Blackwell
1
“invalidate”调用真的需要同步吗?它不仅仅是告诉表面从自己的线程绘制吗? - noelicus
@noelicus 是的,我认为你是对的。由于实际调用是 postInvalidate(),所有与 SurfaceHolder 的交互都将在 UI 线程上进行。DrawThread 是否需要引用 SurfaceHolder 呢?很好的发现。 - Steve Blackwell

3
当前的解决方案不正确。你正在使用SurfaceView来从一个独立的线程更新内容,并没有使用invalidate()方法,这个方法会在系统刷新内容时运行onDraw()方法。问题在于你为StatusView设置了一个背景,尝试删除那一行。
android:background="#000000"

显然,你需要控制在该视图中显示的所有信息。


0
看起来你正在使用setContentView(R.layout.main); 显示你的main.xml,而不是创建并显示表面。除非我在某个地方漏掉了代码,否则我认为这不是问题所在。

R.layout.main 指的是 XML 文件(main.xml),该文件指定了 com.niek.surfaceviewtest.StatusView。我认为它应该可以工作。 - Steve Blackwell
1
糟糕,我的错...看起来你是在main.xml中调用它。我也是新手,不知道你可以通过xml这样做。哈哈 - noobS41bot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接