0

I'm currently developing some sort of a game. It's a side-scroller, so a lot of the items are moving - which results in being redrawn. I'm drawing it all on a SurfaceView in another (not UI) thread, but as soon as there are a bit more items to be drawn, the performance drops. Are there any "good" mechanics to improve the performance? Can I somehow do this in multiple threads?

I've thought about creating multiple canvas, and draw a part of my ViewObject list on each one (in different threads), but then I don't know how to "merge" these.

The performance problems began, when I started to add rotating images. I'm rotating them like this:

         Matrix matrix = new Matrix();
            matrix.preRotate(mRotation, (float) (mResourceBitmaps[mFrame].getWidth()), (float) (mResourceBitmaps[mFrame].getHeight()) / 2f);

            Bitmap bmp2 = Bitmap.createBitmap(mResourceBitmaps[mFrame], 0, 0,
                    mResourceBitmaps[mFrame].getWidth(), mResourceBitmaps[mFrame].getHeight(), matrix, true);

So I'm rotating each bitmap for each animation frame in the code. It became faster when I started to add a BitmapHolder class with a LruCache, which keeps reference of each rotated bitmap (as well as the "standard" bitmaps). But it's still kind of lagging.

Any advices on how this is done in "more professional" games? I'd really like to develop this by myself and not use any 3rd party library, due to the learning factor.

So basically, my question is: How do I draw "a lot" of bitmaps / resources (as they are stored as PNG in my drawable folder) on a single canvas / surfaceview? Or do I have to fall back to multiple view elements representing the objects instead of a SurfaceView?

4

1 回答 1

0

您不应该在每一帧中创建新的位图对象。那样你永远不会得到好的帧率。

请尝试改用该drawBitmap(Bitmap, Matrix, Paint)版本。这样您就可以让系统处理每个对象的旋转。

或者,您可以使用rotate()简单的基于度数的旋转。不要忘记save()之前和restore()之后的画布。

于 2013-08-06T22:57:20.060 回答