[Android Game Development Learning notes]view and Surfaceview

Source: Internet
Author: User
Tags android games

This article is for reading http://blog.csdn.net/xiaominghimi/article/details/6089594 's notes.

The main role in Android games, in addition to the control class is the display class. What's involved in Android is the view class, and other subclasses of the Surfaceview class and surfaceview that inherit from it.

This is the first view and Surfaceview. The direct subclasses of Surfaceview are Glsurfaceview and Videoview, and you can see that GL and video playback and camera cameras generally use surfaceview.

Surfaceview can control the format of the surface, such as size, the location of the display in the screen, the most critical is to provide the Surfaceholder class, using the Getholder method to obtain, related to Lockcanvas (), Removecallback (), Unlockcanvasandpost () controls graphics and drawing.

When using Surfaceview, it is generally necessary to monitor the creation, destruction, and alteration of the surfaceholder.callback. The interface has three callback methods, namely surfacechanged (surfaceholder holder, int format, int width, int height), surfacecreated (Surfaceholder Holder), surfacedestroyed (Surfaceholder holder).

For surface-related purposes, the Android low-level also provides GPU acceleration, so generally real-time applications are mostly built using Surfaceview instead of directly from view, while later doing Android 3D The Glsurfaceview in OpenGL is also implemented from this class.

The essential difference between surfaceview and view is that Surfaceview can draw a picture in a new, separate thread, and Viwe must update the screen in the UI thread.

Updating the screen in the UI can cause an unresponsive problem due to thread blocking, and updating the screen in a non-UI thread can also cause an event synchronization problem. For example, touch the screen, need to surfaceview thread processing, generally need to have an event queue, which involves the synchronization of threads.


Based on the above, according to the game features are divided into two categories:

1, the passive update interface. such as chess games, with view on it. Because the screen update relies on touch events, you can use the invalidate () method directly. Because in this case, the interval between updates is longer.

2, active update. such as the movement of characters in the game. This requires a separate thread to continually redraw the human state and avoid blocking the UI thread, so Surfaceview control is required.


The Surfaceview class in Android is a double-buffering mechanism, with high efficiency and improved functionality.



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.