Understanding and summary of surface and surfaceview in Android

Source: Internet
Author: User

Http://www.linuxidc.com/Linux/2012-07/64223.htm

What is surface?

Simply put, the surface corresponds to a screen buffer. Each window corresponds to a surface, and any view is painted on the surface. The traditional view shares a screen buffer, all plotting must be performed in the UI thread.

What is surfaceview?

Surfaceview is a view that may not be rigorous enough. However, in definition, public class surfaceview extends view {...} it is displayed that surfaceview is indeed derived from view, but surfaceview has its own surface. Continue to see the source code of surfaceview:

If (mwindow = NULL ){
Mwindow = new mywindow (this );
Mlayout. type = mwindowtype;
Mlayout. Gravity = gravity. Left | gravity. Top;
Msession. addwithoutinputchannel (mwindow, mwindow. mseq, mlayout,
Mvisible? Visible: gone, mcontentinsets );
}
Obviously, each surfaceview is created with a mywindow. This in new mywindow (this) is the surfaceview itself, so surfaceview and window are bound together, as mentioned above, each window corresponds to a surface, so surfaceview is embedded with its own surface. We can think that surfaceview is used to control the position and size of the surface. As we all know, updates of traditional views and Their Derived classes can only be performed on the UI thread. However, the UI thread also processes other interaction logic, which cannot guarantee the view update speed and frame rate, surfaceview can be drawn using independent threads, so it can provide a higher frame rate. For example, surfaceview is more suitable for scenarios such as games and camera scenes.

What is surfaceholder. callback?

Surfaceholder. callback mainly provides callback notifications when the underlying surface is created, destroyed, or changed. Because the painting can only be performed after the surface is created, surfaceholder. surfacecreated and surfacedestroyed in callback form the boundary of the drawing processing code. Surfaceholder can be used as a surface container and controller to manipulate the surface. It processes effects and animations drawn on its canvas, controls the surface, size, pixels, and so on.

Why can't normal views be refreshed only in the UI thread?

The UI thread is the most important thread. It cannot be blocked or secure. If you know multithreading, there should be no stranger to such words as thread security and synchronization locks. The UI thread is responsible for drawing interface and distributing window events, and the task is very important, generally, to ensure the correctness of resource access during multi-threaded processing, synchronization locks are usually applied to some operations, which will obviously reduce the efficiency and involve thread waiting and thread context switching, to improve efficiency, the UI thread does not use these cumbersome multithreading mechanisms. To ensure the correctness of UI operations, you can only operate the UI in the UI thread. You can use post or runonuithread to refresh the view in a non-UI thread.

Other summary:

Surfaceview is an inherited class of view, which is embedded with a surface dedicated for painting. You can control the format and size of the surface. Surfaceview controls the position of this surface.

Surface is ordered in depth (Z-ordered), which indicates that it is always behind its own window. Surfaceview provides a visible area where only the surface content in the visible area is visible and the area outside the visible area is invisible. The layout display of the surface is affected by the hierarchical view relationship, and its sibling view node is displayed at the top. This means that the content of the surface is blocked by its sibling view, which can be used to place overlays (for example, controls such as text and buttons ). Note: if there is a transparent control on the surface, every change to it will cause the framework to recalculate its transparency and the top-level control, which will affect the performance.

You can access this surface through the surfaceholder interface. The getholder () method can obtain this interface.

When surfaceview becomes visible, the surface is created. Before surfaceview is hidden, the surface is destroyed. This saves resources. If you want to check the time when the surface is created and destroyed, you can reload surfacecreated (surfaceholder) and surfacedestroyed (surfaceholder ).

The core of surfaceview is to provide two threads: the UI thread and the rendering thread. Note:

1. All surfaceview and surfaceholder. Callback methods should be called in the UI thread, which is generally the main thread of the application. Various variables to be accessed by the rendering thread should be processed synchronously.

2. because the surface may be destroyed, it is only in surfaceholder. callback. surfacecreated () and surfaceholder. callback. surfacedestroyed () is valid, so make sure that the rendering thread accesses a valid surface.

The above summary is based on some online materials and personal understanding. please correct me if you have any mistakes ~

This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2012-07/64223.htm

 

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.