简体   繁体   中英

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

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?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM