Related knowledge points of surface view SurfaceView in Android

1. Introduction to SurfaceView

Android's drawing mechanism is that the UI thread draws on the screen. Under normal circumstances, other threads are not allowed to directly perform drawing operations. This mechanism is not a problem when dealing with simple pages, because ordinary pages do not draw frequently and in large areas, but this mechanism will cause problems when dealing with complex and changeable pages, such as constantly changing game interfaces, continuous photos or videos. The changed preview interface will cause the UI thread resources to be blocked, that is, the interface is stuck.

Surface View SurfaceView is a special view used by Android to solve sub-thread drawing. It has an independent drawing surface, that is, it does not share the same drawing surface with its host page. Due to the independent drawing surface, the interface of the surface view can be drawn in a separate thread, and this sub-thread is the rendering thread. Because the rendering thread does not occupy the resources of the main thread, on the one hand, it can achieve complex and efficient UI refresh, and on the other hand, it can respond to user input events immediately. Because the surface view has the above characteristics, it can be used as a preview interface for taking photos and videos, and it can also be used as a real-time interface for games.

2. Introduction to SurfaceHolder and related API

Because the surface view is not drawing on the main UI thread, neither the onDraw method nor the dispatchDraw method performs drawing operations, so the surface view must be drawn through other means. This way is the internal class surface holder SurfaceHolder externally calling the getHolder method of the SurfaceVIew object Get the SurfaceHolder object, and then perform related drawing operations on the preview interface.

The commonly used APIs are as follows:

  • lockCanvas: Lock and get the canvas on the drawing surface.
  • unlockCanvasAndPost: unlock and refresh the canvas on the drawing surface.
  • addCallback: Add the callback interface SurfaceHolder.Callback of the drawing surface. The callback interface has the following three methods.
surfaceCreated Triggered after the drawing surface is created, the camera can be opened here.
surfaceChanged Triggered after the drawing surface is changed.
surfaceDestroyed Triggered after the drawing surface is destroyed.
  • removeCallback: Remove the callback interface of the drawing surface.
  • isCreating: Determine whether the drawing surface is valid. If you operate SurfaceView elsewhere, you must determine whether the current drawing surface is valid.
  • getSurface: Get the object of the drawing surface, that is, the preview interface.
  • setFixedSize: Set the size of the preview interface.
  • setFormat: Set the format of the drawing surface. The values ​​of the drawing format are as follows

The drawing format type of the PixelFormat class

Description
TRANSPAREN Transparent
TRANSLUCENT translucent
OPAQUE opaque

3. The difference between surface view and normal view

  • The surface view allows multiple threads to be opened for simultaneous drawing operations, while the normal view has only one UI thread that can draw.
  • The surface view will not automatically clear the last drawing result, that is, the drawing operation is performed incrementally, while the normal view will clear the last drawing result before each drawing.

 

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/115080091