我不知道为什么但我的表面视图是空黑屏。它应该有红色背景。
我错了什么?
SurfaceView :
public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
private GameThread gameThread;
public GameView(Context context)
{
super(context);
getHolder().addCallback(this);
gameThread = new GameThread(getHolder(), this);
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.RED);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Utils.logI("Surface changed!");
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Utils.logI("Surface created !");
gameThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
Utils.logI("Surface destroyed");
boolean retry = true;
gameThread.stopThread();
while(retry)
{
try
{
gameThread.join();
retry = false;
}
catch (InterruptedException e){}
}
}
}
GameThread :
public class GameThread extends Thread
{
private boolean isQutting = false;
private SurfaceHolder surfaceHolder;
private GameView gameView;
public GameThread(SurfaceHolder surfaceHolder, GameView gameView)
{
this.surfaceHolder = surfaceHolder;
this.gameView = gameView;
setName("Game thread");
}
@Override
public void run()
{
Canvas canvas = null;
while(!isQuitting())
{
try
{
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder)
{
gameView.draw(canvas);
}
}
finally
{
if (canvas != null)
{
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
private boolean isQuitting() { return this.isQutting; }
public synchronized void stopThread()
{
this.isQutting = true;
}
}
ActivityMain :
public class ActivityMain extends BaseFragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));
}
}