Android SurfaceView没有显示onDraw

时间:2011-04-12 19:05:08

标签: android surfaceview ondraw

为了简单问题,我在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>

3 个答案:

答案 0 :(得分:4)

我唯一的猜测是绘画没有在表面上进行,因为视图没有失效。您应该调用invalidate()来绘制,然后让框架调用onDraw()。也许这就是tmp增加的原因,但是绘画操作只是第一次到达表面。

可能值得尝试:也许让Canvas c成为StatusView的成员,然后替换

synchronized (surfaceHolder) {
  onDraw(c);
}

synchronized (surfaceHolder) {
  invalidate();
}

这有用吗?

答案 1 :(得分:3)

目前的解决方案是不对的。您正在使用SurfaceView来更新单独线程中的内容,而不使用在系统刷新内容时将运行invalidate()方法的onDraw()方法。问题是您已为StatusView设置了背景,请尝试删除该行

android:background="#000000"
显然,您需要控制该视图中显示的所有信息。

答案 2 :(得分:0)

看起来您正在使用setContentView(R.layout.main)显示main.xml;而不是创建表面并显示它。除非我在某个地方丢失代码,否则我认为不是这样。