0

Well I've createad a SurfaceView that displays a bitmap, without problems. But I want to display a Text in the bottom of the screen, I think it can be called the canvas.

I tried to draw the text the same way than i did with the bitmap, but without success. I get de force close error.

I've got something like this:

        public void run() {

        while (isRunning) {
            if (!ourHolder.getSurface().isValid())
                continue;

            Canvas canvas = ourHolder.lockCanvas();

            canvas.drawColor(Color.WHITE);

            canvas.drawText(score, 200, 100, null);

            canvas.drawBitmap(enemy1, enemy1X, enemy1Y, null); // DRAW FIRST
                                                                // ENEMY

            ourHolder.unlockCanvasAndPost(canvas);

I already tried to set up a new Paint too, instead of using the "null", but it didn't worked as well :/

Can you please tell how it must be done, or why it isn't working. Im new to Android Programming...

Thanks ;)

4

1 回答 1

2

奇怪的。Сode 看起来很清晰。图片不覆盖文字?

然而,分配一个文本油漆并设置颜色。试试这个进行测试:

Canvas canvas = null;
try {
    canvas = ourHolder.lockCanvas();
    synchronized (ourHolder) {
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(enemy1, enemy1X, enemy1Y, null);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawText("XXXX", 200, 100, paint);
    }
} catch (Exception e) {
    Log.e(TAG, "run() lockCanvas()", e);
} finally {
    if (canvas != null) {
        ourHolder.unlockCanvasAndPost(canvas);
    }
}

添加

SurfaceView 尺寸大于 200х100?尝试canvas.drawText("XXXX", 20, 20, paint);

于 2012-03-28T22:07:08.957 回答