Android Canvas Off Screen Performance

I am developing an Android game using the Canvas element. I have many graphic elements (sprites) drawn on a large map of games. These elements are drawn with standard graphics functions such as drawLine , drawPath , drawArc , etc.

It is not difficult to check whether they are on the screen or not. That way, if they are off screen, I can completely skip their drawing procedures. But even this has a processor cost. I wonder if Android Graphics Library can do this faster than I can?

In short, should I try to draw everything, even if they are completely out of screen coordinates, considering that the Android Graphics Library will take care of them and do not spend a lot of processor on drawing them, or should I check their rectangle of the drawing area and if they are completely off screen, skip drawing procedures? What is the right way? Which one should be faster?

ps: I am targeting Android v2.1 and higher.

+4
source share
1 answer

From an incompletely scientific test, I drew raster images broken into a larger area than on the screen, I found that checking in advance if the bitmap was on the screen does not seem to be much different.

In one test, I set Rect to the screen size and set another Rect to the position of the bitmap and checked Rect.intersects () before drawing. In another test, I just drew a bitmap. After 300 draws there was no visible trend - some went one way, others went the other. I tried the 300 draw test every frame, and the change from frame to frame was much larger than the difference between the marked and unverified pattern.

From this, I find it safe to say that Android checks borders in its own code , or you expect a significant difference. I would share the code for my test, but I think it makes sense for you to make your own test in the context of your situation. Possible points behave differently than bitmaps, or some other feature of your paint or canvas changes the situation.

I hope that you will help you (or another, stumble upon this thread, as well as the same question).

+4
source

Source: https://habr.com/ru/post/1412541/


All Articles