Android Game Slows Down

| | August 10, 2015

I had been developing a Maze Game which is dragging a bitmap down on the maze going on the finish line. on first my game response quickly on my onTouch and dragging and after couples of
adding toast message, generate random mazes, changing character from drawCircle into a bitmap and a background from a solid color into a customize PNG file my game seems to Slows Down.

Can anyone explain why could this happen?

And what should I do to make my game to response more quickly?

Tnx for the help and godbless.

UPDATE: I found out that it all started when i change my solid color background to a bitmap background.

here’s my code:

From this:

protected void onDraw(Canvas canvas) 
canvas.drawRect(0, 0, width, height, background);

To this:

protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.menu_bg);
//Set Rectangle Size and Coordinates
RectF rect = new RectF(0,0,width,height);
//Maze with Background
canvas.drawBitmap(bitmap, null, rect, null);

One Response to “Android Game Slows Down”

  1. Alexandre Desbiens on November 30, -0001 @ 12:00 AM

    On of the major reason your code is slow is because you load your bitmap every time you render your game. Try putting this line in an initialization function:

    ...
    Bitmap bitmap;
    
    protected void init() {
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.menu_bg);
    }
    
    ...
    
    protected void onDraw() {
        //Set Rectangle Size and Coordinates
        RectF rect = new RectF(0,0,width,height);
        //Maze with Background
        canvas.drawBitmap(bitmap, null, rect, null);
    }
    

    If you could post your entire code, I could tell you exactly where to put this line, but your engine should have something like a constructor or an init() function.

Leave a Reply