Reputation: 3274
This is the main scenario: KillThemAll Game
In constructor
of my CustomView
which extends SurfaceView
class, I set the background
as :
this.setBackgroundDrawable(getResources().getDrawable(R.drawable.moon_light));
if I set background in one of the methods of SurfaceHolder.Callback()
, the game and all the animations become freezed...
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) {
}
});
why?
Upvotes: 2
Views: 1082
Reputation: 2088
Looks to me you just want to draw a background. Since you have overridden onDraw()
in your SurfaceView
implementation to do animation, you should not rely on View
methods to do drawing because they will conflict with your custom drawing.
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(background, 0, 0, null); // background replaces canvas.drawColor(Color.BLACK);
// draw your sprites here
}
You just have to make sure background
is a Bitmap
that is bigger than the canvas size. You could also use drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)
to scale it to the right size but it won't respect the aspect ratio.
Upvotes: 1
Reputation: 2404
I think you're making a miskate on your src and dest rects.Try to print the values of both on the log and see whether they are correct
Upvotes: 0
Reputation: 2005
Replace this line
this.setBackgroundDrawable(getResources().getDrawable(R.drawable.moon_light));
with
bm = BitmapFactory.decodeResource(getResources(), R.drawable.moon_light);
Upvotes: 0