This article is an amendment to the previous Android surfaceview painting overwrite refresh and dirty rectangle refresh method.
Frame Rate Processing
I am reviewing the previous example today.CodeSuddenly found that I made a low-level error, resulting in frame rate not up to expectations, this error is here:
Here, we set a fixed sleep duration of 33 milliseconds after each painting to limit the frame rate to 30 frames per second.
However, the time consumed by plotting and other operations is ignored here. That is to say, unless the total time consumed by all other operations is less than 1/3 milliseconds, we cannot reach the expectation of 30 frames per second.
The correction method is to get the current time value of the system before each frame starts processing, and then get the current time value after processing, and then subtract the old value from the current value, the time consumed by processing is obtained, and then the time consumed is reduced by 33 MS, which is the time value for the next frame to sleep:
Run nowProgram, You can see that it runs smoothly.
I think people who have been engaged in game development can see this error at a glance. I have no experience planting it here >_< ~
Drawing Processing
Note the following when processing a drawing:
After the canvas is locked, it is recommended that you only perform the operations related to the drawing during the unlock period. The fewer other code, the better, as if we open the database link during development.
Because we have to wait for other threads to be used after locking, releasing it as soon as possible will save the waiting time of other threads, and we should try to put irrelevant code beyond this time period for execution.
In this example, the operations on the matrix can be adjusted to the previous execution, and the paint object can also be moved to the previous creation, without the need to create a paint object for each loop, once created:
After adjustment, only the drawing-related code is left in the green highlighted part, which reduces the workload.