Drawing and application of Android surfaceview

Drawing and application of Android surfaceview

1、 The difference between surface view and view

Android provides view to draw the view, which can meet most of the needs of drawing, but sometimes it is more than enough. As we know, view draws the view by refreshing. Android system draws the screen through Vsync signal. The refresh interval is 16 milliseconds. If the painting operation for refreshing is completed within 16 milliseconds, the visual effect will not feel stuck. If there are too many logical operations, frequent refresh will cause the interface to jam.

For this problem, Android provides surface view to solve it. It can be said to be the twin brother of view, but it is still different from view. The main differences between view and view are as follows:

View is mainly used for active updates, while surface view is mainly used for passive updates, such as frequent refreshing of columns.

The view mainly refreshes the interface through the main thread, while the surface view mainly refreshes the view through the sub thread.

View does not use double buffering mechanism when drawing, but the bottom layer of surface view uses double buffering mechanism.

2、 Use of surfaceview

Although the use of surfaceview is complex, it has a set of templates, which makes it easier to use. Generally, we will use the following methods and steps to create surfaceview:

Create a custom surfaceview, which inherits from surfaceview. It also implements two interfaces, surfaceholder.callback and runnable.

The following methods correspond to the creation, change and destruction of the surface view.

Let's take a look at his template:

The above templates basically meet the drawing requirements of most surface views. The only thing to pay attention to is to

Put it in finally to ensure that changes can be submitted every time.

As long as we draw continuously in the run method, we can refresh the view in time. Of course, we can also sleep in the run method to reduce resource consumption. This value is typically between 50 and 100 milliseconds.

The above is a detailed introduction to the drawing and application of Android surfaceview. You can query a lot of information about the rewriting of Android view on this site as needed. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>