Correction of Android surfaceview plotting and frame rate processing methods

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.