Differences between Android surfaceview and view

Source: Internet
Author: User
If your game does not eat CPU, it is better to use view. The system determines the time to refresh the surface according to the standard Android operation mode.

But if it's unfortunate, you can't do it.ProgramIf you eat CPU, you have to use surfaceview to forcibly refresh the surface. Otherwise, the system UI process may not be able to compete with the CPU-consuming threads.

Of course, there are more than two methods to refresh the surface. These two methods are common for pure Java applications.

The most essential difference between surfaceview and view is that surfaceview can be re-painted in a new separate thread, and view must be updated in the main thread of the UI.

Updating the screen in the main UI thread may cause problems. For example, if you update the screen too long, your main UI thread will be blocked by the function you are painting. Then, messages such as buttons and touch screens cannot be responded.

When surfaceview is used to update the screen in a new thread, it will not block your main UI thread. But this also brings about another problem, that is, event synchronization. For example, if you touch the screen, you need to process the thread in surfaceview. Generally, you need an event queue design to save the touch event. This is a little more complicated because it involves thread synchronization.

Based on the above, the game features are generally divided into two categories.

1. passively update the image. For example, you can use view for chess. Because image update relies on ontouch for update, you can use invalidate directly. In this case, this touch and the next touch take a longer time and will not be affected.

2. Actively update. For example, a person is always running. This requires a separate thread to repeatedly repaint the state of the person, to avoid blocking the main UI thread. Obviously, the view is not suitable and needs surfaceview for control.

 

 

Generally, surfaceview is sufficient for 2D game development because it is also a canvas for Google experts to expand for 2D game development.

Use a normal game canvas (2d dedicated game canvas in Android) to draw images, and then use glsurfaceview (3D Game dedicated canvas in Android) in the comparison of rendered images, it is found that glsurfaceview is more efficient than surfaceview by 30 times; glsurfaceview is mainly because of GPU acceleration of machine hardware, and now Flash technology also supports GPU acceleration;

The following is a summary:


Generally, it is sufficient for 2D games to use surfaceview, so do not think that glsurfaceview (OpenGL) should be used for everything, and
The disadvantage of glsurfaceview is its poor adaptability because many models do not have GPU acceleration.

Related Article

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.