William Hoskins
William Hoskins

Reputation: 305

Android draw Bitmap canvas taking too long

I am attempting to use canvas to make an android game, which I know is bad because opengl is better, but I didn't really think about it then and now I am almost done with it and I just want to get it done and convert it to opengl later.

So, at the beginning of my onDraw, I draw a bitmap with the following code:

Log.d("Start Time", System.nanoTime()/1000000 + "");
        canvas.drawBitmap(bitmaps.Background(), null, bg, paint);
        Log.d("After drawColor", System.nanoTime()/1000000 + "");

So, as you can see I draw the bitmap based on a rectangle, so it is stretched and drawn. Using the logs to see how long it took to do this, I saw that it is taking about 15 to 20 milliseconds just to draw this one bitmap!

Does anyone know why this is happening? Also, if anyone knows an easy way to switch from canvas to opengl that would be awesome :p

William

Upvotes: 0

Views: 1257

Answers (2)

Luke
Luke

Reputation: 5564

I notice that bitmaps.Background() is a method; is it by any chance repeatedly unpacking the Bitmap from the resource file? If you're drawing a Bitmap every frame you should be keeping the Bitmap object in memory. (Just drawing a preexisting Bitmap object to the screen will not take that long.)

Upvotes: 0

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28697

Is bg a Rect? If so, the bitmap may have to be scaled before being drawn, which would consume time. See if you can pre-scale the bitmap.

Secondly, do you really need the paint in the drawBitmap call? You can pass null instead, if you're not doing transformations.

Also why not use System.currentTimeMillis() instead of dividing nanoseconds to get millis?

Upvotes: 1

Related Questions