The difference between Android SurfaceView and View

The difference between Android SurfaceView and View

SurfaceView and View in Android are two commonly used UI components, and they have some differences in rendering and drawing:

Rendering method:

  • View: View uses the UI thread for rendering, that is, the drawing operation is performed in the main thread. When the View needs to be redrawn, it will trigger the drawing process of the UI thread.
  • SurfaceView: SurfaceView uses a double-buffering mechanism, which has its own independent drawing thread (created in the SurfaceHolder.Callback.surfaceCreated() method in SurfaceHolder.Callback), called the rendering thread. The rendering thread is independent of the UI thread, and can perform drawing operations in the background, thereby avoiding the stuck problem caused by performing complex drawing tasks in the UI thread.

Draw method:

  • View: The drawing of View is performed in the UI thread, that is, the drawing operation is performed in the View's onDraw() method. It uses a Canvas object for drawing and triggers a redraw when needed by calling the invalidate() method.
  • SurfaceView: SurfaceView is drawn through the SurfaceHolder object. We can get the Canvas object through SurfaceHolder in the rendering thread, and use Canvas for drawing operations.

Occupying resources:

  • View: Since View is drawn directly in the UI thread, it occupies relatively little system resources.
  • SurfaceView: SurfaceView uses an independent rendering thread, so it will take up more system resources. This is because SurfaceView needs to maintain an independent drawing surface (Surface) in the background, and needs to synchronize the rendering thread with the UI thread.

Applicable scene:

  • View: View is suitable for simple static UI elements and simple animation effects, suitable for general interface layout and interaction.
  • SurfaceView: SurfaceView is suitable for scenes that require complex drawing, frequent updates, or real-time rendering, such as game engines, video players, camera previews, etc. Because it uses an independent rendering thread, it can perform efficient drawing operations in the background, providing a smoother user experience.

To sum up, SurfaceView and View differ in rendering and drawing. SurfaceView is suitable for scenes that require high performance and real-time rendering, while View is suitable for general UI layout and simple animation effects. When choosing to use, you need to decide which UI component to use based on specific needs and performance requirements.

code example

Here are some simple code samples showing how to use SurfaceView and View in Android:

  1. Use View to draw:
public class CustomView extends View {
    // 构造函数
    public CustomView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 在View上进行绘制操作
        canvas.drawColor(Color.RED);
        // 绘制其他图形、文本等
    }
}

In this example, we create a custom View class and override the onDraw() method. In the onDraw() method, we use the Canvas object for drawing operations, such as drawing a red background through the drawColor() method. You can perform more drawing operations in this method, such as drawing graphics, text, etc.

  1. Use SurfaceView to draw:
public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder surfaceHolder;

    // 构造函数
    public CustomSurfaceView(Context context) {
        super(context);
        // 获取SurfaceHolder对象
        surfaceHolder = getHolder();
        // 设置SurfaceHolder回调
        surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 在Surface创建时开始绘制线程
        new DrawThread().start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Surface发生变化时的处理
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // 在Surface销毁时停止绘制线程
        boolean retry = true;
        while (retry) {
            try {
                // 停止线程
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class DrawThread extends Thread {
        @Override
        public void run() {
            Canvas canvas = null;
            try {
                // 获取Canvas对象
                canvas = surfaceHolder.lockCanvas();
                if (canvas != null) {
                    // 在Canvas上进行绘制操作
                    canvas.drawColor(Color.BLUE);
                    // 绘制其他图形、文本等
                }
            } finally {
                if (canvas != null) {
                    // 解锁并提交Canvas
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

In this example, we create a custom SurfaceView class and implement the SurfaceHolder.Callback interface. In the constructor, we get the SurfaceHolder object and set the callback. In the callback method, we start the drawing thread in the surfaceCreated() method and stop the drawing thread in the surfaceDestroyed() method. In the drawing thread, we use the Canvas object for drawing operations, such as drawing a blue background through the drawColor() method.

These examples show the basic usage of drawing with View and SurfaceView in Android. By rewriting the onDraw() method or creating a drawing thread, we can perform custom drawing operations on View or SurfaceView. According to actual needs, more drawing codes can be added in these methods to achieve the desired drawing effect.

Guess you like

Origin blog.csdn.net/QYgujingjing/article/details/131668803