Android Surfaceview drawing and application
1. The difference between surfaceview and view
Android provides view drawing, which can meet most drawing needs, but sometimes it is too willing but not enough. We know that the view draws the view by refreshing. The Android system uses the vsync signal to draw the screen. The refresh interval is 16 milliseconds. If the drawing operation requesting refresh is completed within 16 milliseconds, there will be no lag in the visual effect. If there are too many logic operations, frequent refreshes will cause interface lag.
For this problem, Android provides surfaceview to solve it. It can be said to be the twin brother of view, but it is still different from view. The difference between him and view is mainly in the following points:
View is mainly used for active updates, while surfaceview is mainly used for passive updates, such as frequent refreshes.
The view mainly refreshes the interface through the main thread, while the surfaceview mainly refreshes the view through the child thread.
The view does not use a double buffering mechanism when drawing, while the surfaceview is under the bottom layer of the surfaceview uses a double buffering mechanism.
2. Use of surfaceview
Although surfaceview is more complicated to use, it has a set of templates, which makes it easier to use. Generally speaking, we will use the following method steps to achieve the creation of surfaceview:
Create a custom surfaceview, inherit from surfaceview. And realize the connection interface and the Runnable interface.
public class MySurfaceView extends SurfaceView implements , Runnable
Look at the following methods, which correspond to the creation, changes and destruction of surfaceview respectively.
@Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { }
Let's take a look at his template:
public class MySurfaceView extends SurfaceView implements , Runnable { private SurfaceHolder mHolder; private Canvas mCanvs; private Boolean mIsDrawing; public MySurfaceView(Context context) { this(context, null); } public MySurfaceView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private void init() { mHolder = getHolder(); (this); setFocusable(true); setFocusableInTouchMode(true); (true); } @Override public void surfaceCreated(SurfaceHolder holder) { mIsDrawing = true; new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { mIsDrawing = false; } @Override public void run() { while (mIsDrawing) { draw(); } } public void draw() { try { mCanvs = (); } catch (Exception e) { } finally { (mCanvs); } } }
The above templates basically meet most of the drawing needs of surfaceviews. The only thing to note is that
(mCanvs);
Put it in finally to ensure that you can submit changes every time.
As long as we continuously draw in the run method, we can realize timely refresh of the view. Of course, we can also sleep in the run method to reduce resource consumption. This value is generally between 50 and 100 milliseconds.
The above is a detailed introduction to the drawing and application of Android Surfaceview. There are many information about rewriting Android View on this site that you can query as needed. Thank you for reading. I hope it can help you. Thank you for your support for this site!