这是主要场景:KillThemAll Game
在扩展 SurfaceView
类的 CustomView
的 constructor
中,我将 background
设置为:
this.setBackgroundDrawable(getResources().getDrawable(R.drawable.moon_light));
如果我在 SurfaceHolder.Callback()
的方法之一中设置背景,游戏和所有动画都会卡住...
getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
createSprites();
gameLoopThread.setRunning(true);
gameLoopThread.start();
setBackgroundDrawable(getResources().getDrawable(R.drawable.moon_light));
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
为什么?
最佳答案
在我看来你只是想画一个背景。由于您已经在 SurfaceView
实现中覆盖了 onDraw()
来制作动画,因此您不应依赖 View
方法来进行绘图,因为它们会发生冲突与您的自定义绘图。
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(background, 0, 0, null); // background replaces canvas.drawColor(Color.BLACK);
// draw your sprites here
}
您只需确保 background
是一个大于 Canvas 大小的 Bitmap
。您还可以使用 drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)
将其缩放到正确的大小,但它不会考虑纵横比。
关于android - 我的 Surface View 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21955025/