在画布上绘图始终显示黑屏

时间:2018-08-26 13:50:56

标签: java android canvas

几个星期前,我想尝试使用Android Studio制作一个小小的愚蠢的游戏,但是两天后我就被困了,因为如果我尝试在画布上画一些东西,我会一直看到黑屏,我不知道为什么! AS没有给出任何错误,我正确地遵循了本教程,并尝试寻找解决方案,但是我什么都没发现…… 这是我的GameLoop,我尝试在白色背景上绘制经典的红色矩形:

package axel.gametest;

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

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

    private MainThread thread;

    public GameView(Context context){
        super(context);
        getHolder().addCallback(this);
        thread = new MainThread(getHolder(), this);
        setFocusable(true);
    }

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

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder){

        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
        boolean retry = true;
        while (retry){
            try{
                thread.setRunning(false);
                thread.join();
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
            retry = false;
        }

    }

    public void update(){
    }

    @Override
    public void draw(Canvas canvas){
        super.draw(canvas);
        if (canvas != null){
            canvas.drawColor(Color.WHITE);
            Paint paint = new Paint();
            paint.setColor(Color.rgb(250,0,0));
            canvas.drawRect(100,100,200,200,paint);
        }
    }
}

我想念什么?有什么建议么?谢谢!

0 个答案:

没有答案