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:
[Java] view plaincopy
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: www.2cto.com
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.
Author: zjmdp