Android Canvas SurfaceView黑屏

Jur*_*sky 0 android canvas surfaceview

我有以下课程:

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

public static final int WIDTH = 860;
public static final int HEIGHT = 480;

int x = 0;

GameLoop gameLoop;

public GameView(Context context) {
    super(context);
    getHolder().addCallback(this);
    setFocusable(true);
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
    setFocusable(true);
}

public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    getHolder().addCallback(this);
    setFocusable(true);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    //setWillNotDraw(false);

    gameLoop = new GameLoop(this);

    gameLoop.start();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(canvas != null) {
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawRect(x, 0, 200, 200, paint);
        canvas.drawRect(200, 200, 400, 400, paint);
        Log.v("TEST", "Drawing into canvas");
        Log.v("TEST", "x := " + x);
    }
}

public void update() {
    x += 5;
}
Run Code Online (Sandbox Code Playgroud)

和一个看起来像这样的游戏循环:

public class GameLoop extends Thread {
public static final int REFRESH_TIME = 2;
private static final int LOG_FPS_AFTER_FRAMES = 30;

public static Canvas canvas;

private double averageFPS;

private GameView gameView;

private boolean running;

public GameLoop(GameView gamePanel) {
    this.gameView = gamePanel;
}

@Override
public void run() {
    super.run();

    running = true;

    long startTime;
    long timeMillis;
    long waitTime;
    long totalTime = 0;
    int frameCount = 0;
    long targetTime = 1000*REFRESH_TIME;

    while(running) {
        startTime = System.nanoTime();
        canvas = null;

        try {
            canvas = gameView.getHolder().lockCanvas();

            synchronized (gameView.getHolder()) {
                gameView.update();
                gameView.draw(canvas);
            }


        } catch(Exception e) {}
        finally {
            if(canvas != null) {
                try {
                    gameView.getHolder().unlockCanvasAndPost(canvas);
                } catch(Exception e) {}
            }
        }


        timeMillis = (System.nanoTime() - startTime) / 1000000;
        waitTime = targetTime - timeMillis;

        try {
            this.sleep(waitTime);
        } catch(Exception e) {}

        totalTime += System.nanoTime() - startTime;
        frameCount++;

        if(frameCount == LOG_FPS_AFTER_FRAMES) {
            averageFPS = 1000/((totalTime/frameCount)/1000000);
            frameCount = 0;
            totalTime = 0;
            Log.v("FPS", Double.toString(averageFPS));
        }

    }

}

@Override
public void interrupt() {
    super.interrupt();

    running = false;
}

public boolean isRunning() {
    return running;
}

public void setRunning(boolean value) {
    this.running = value;
}
Run Code Online (Sandbox Code Playgroud)

}

每两秒调用一次OnDraw方法,但屏幕仍为黑色.任何想法可能是什么问题?(顺便说一句,如果我取消注释行setWillNotDraw(false),矩形被绘制一次但不移动)...

fad*_*den 5

你有一个常见的问题:你在覆盖onDraw()视图部分.所以你有一个非常昂贵的自定义视图,它无法正常工作.如果你让View绘制,它会画一次,但是你没有打电话invalidate()所以它再也不会发生了.我的猜测是你在布局中设置了背景颜色,使得视图不透明,这就是为什么你无法看到在Surface上绘制的内容.

对于Canvas渲染,使用自定义View而不是SurfaceView 通常会更好.我会推荐这种方法.

如果您真的想使用SurfaceView,请更改onDraw()为其他内容(例如doDraw()),并从渲染线程中调用它.因为你将绘制Surface部件,而不是View部件,所以你不需要继承SurfaceView(并且不应该,因为使它成为一个独立的类将避免常见的陷阱,例如你陷入其中的陷阱).确保您了解SurfaceView和Activity如何交互,因为有各种方法可以解决问题.

您还应该阅读构建游戏循环的推荐方法.