Android Development: summary of basic SurfaceView usage, androidsurfaceview
This article describes how to use SurfaceView to help you quickly get started with SurfaceView development. As the layout of the previous article "Android development: SurfaceView basic usage summary and development issues sharing" is poor, a separate summary of the basic usage of SurfaceView is introduced.
Reprinted by xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45966309. thank you!
Basic Introduction to SurfaceView
1. The system provides a special Surface for SurfaceView, which is embedded in the SurfaceView layer.
2. The Surface is the place where the image data is saved. It holds a Canvas object, that is, the Surface is the place where the image is drawn.
3. SurfaceHolder is the Surface manager and can control the Surface format and size.
To sum up, the screen is drawn in the Surface. In SurfaceView, The SurfaceHolder object is obtained to manage and display the Surface data content.
SurfaceView and General View:
Normal view needs to update the UI in the UI thread; otherwise, ANR may easily occur.
SurfaceView is used to draw a view in a work thread.
Basic SurfaceView usage
Scenarios where SurfaceView is required: video playback, video preview, and game development. In short, SurfaceView is applicable to scenarios where the UI needs to be updated frequently.
The basic process for customizing SurfaceView:
1. inherit SurfaceView
2. Rewrite at least one constructor of SurfaceView.
3. Use the getHolder () method to obtain the reference object of SurfaceHolder.
4. Add an interface object implementing SurfaceHolder. Callback to the SurfaceHolder object.
5. Rewrite the three Callback Methods: surfaceChanged, surfaceCreated, and surfaceDestroyed.
6. Use the SurfaceHolder object to set the Surface format
7. Use the SurfaceHolder object to set the Surface type (the Surface type has been deprecated over Api 11)
8. Use the SurfaceHolder object to set the Surface size, width, and height (optional)
The SurfaceView sample code is as follows:
// Step 1: Inherit SurfaceViewpublic class VideoView extends SurfaceView {private SurfaceCallback mSurfaceCallback; private SurfaceHolder mSurfaceHolder; private Canvas mCanvas; private Panit mPanit; // Step 2: override the constructor public VideoView (Context context, AttributeSet attrs) {super (context, attrs); // Step 3: Obtain the SurfaceHolder object mSurfaceHolder = this. getSurfaceHolder (); // Step 4: Add SurfaceHolder. callback interface mSurfaceHolder (). addCallback (MCallback); // Step 6: Set the Surface format // parameter: the int value defined in PixelFormat. For details, see PixelFormat. javamSurfaceHolder (). setFormat (PixelFormat. RGBA_8888); // Step 7: Set the Surface type // parameter: SURFACE_TYPE_NORMAL: normal Surface cached with RAM // SURFACE_TYPE_HARDWARE: Applicable to DMA (Direct memory access) engine and hardware-accelerated Surface // SURFACE_TYPE_GPU: GPU-accelerated Surface // SURFACE_TYPE_PUSH_BUFFERS: indicates that the Surface does not contain native data, and the Surface uses data provided by other objects, // This type of Surface is used in the Camera image preview, with Cam Era is responsible for previewing the Surface data, so that the image preview will be smoother. MSurfaceHolder (). setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS);}/*** Step 8: Set the Surface size, width, and height (optional) ** used to set the Surface width and height externally * @ param videoWidth set the Surface width * @ param videoWidth set the Surface height */public void setVideoLayout (int videoWidth, int videoHeight) {mSurfaceHolder (). setFixedSize (videoWidth, videoHeight);}/*** Step 5: Implement SurfaceHolder. callback interface * override the three Callback Methods: surfaceChanged, surfaceCreated, surfaceDestroye D */private SurfaceHolder. callback mCallback = new SurfaceHolder. callback () {/*** is called immediately when the Surface format and size change, you can update the Surface ** @ param holder in this method to hold the new format of the SurfaceHolder object of the current Surface * @ param format surface * @ param width new width of the surface * @ param height surface new Height */@ Overridepublic void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}/ *** is called immediately when the Surface is first created: Get focus. Generally, the painting thread is enabled here. * @ Param holder: The SurfaceHolder object of the current Surface */@ Overridepublic void surfaceCreated (SurfaceHolder holder) {mPanit = new Panit (); // draw a circle to the right () {public void run () {synchronized (mSurfaceHolder) {// obtain the canvas object mCanvas = mSurfaceHolder of the Surface. lockCanvas (); for (int I; I <500; I ++) {// draw the right circle mCanvas. drawCircle (15,100, 15, mPanit); try {sleep (500);} catch (Exception e) {e. printStackTrace () ;}// release the canvas Object update SurfacemSurfaceHolder. unlockAndPost (mCanvas) ;}}. start () ;}/ *** call immediately when the Surface is destroyed: when the focus is lost. Generally, the painting thread is stopped and destroyed here * @ param holder holds the SurfaceHolder object of the current Surface */@ Overridepublic void surfaceDestroyed (SurfaceHolder holder) {// TODO };}
First, we customized SurfaceView according to the standard steps, and then started a thread during Surface creation to draw a circle from left to right. If you run the code, you will find that SurfaceView cannot be drawn. Why?
Because when setting the Surface type:
MSurfaceHolder (). setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); // indicates that the Surface does not contain native data, and the data used by the Surface is provided by other objects.
When SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS is used, Canvas obtained by mSurfaceHolder. lockCanvas () cannot be used for plotting. Please note!
There are three other types of setType:
SurfaceHolder. SURFACE_TYPE_NORMAL // use RAM to cache normal Surface SurfaceHolder of native data. SURFACE_TYPE_HARDWARE // applicable to the DMA (Direct memory access) engine and hardware-accelerated Surface SurfaceHolder. SURFACE_TYPE_GPU // applicable to GPU-accelerated Surface
This article only shares the basic usage of SurfaceView and does not cover the Principles. If you need to understand the principles, you can go to: (Google) SurfaceView principles or: (Baidu) SurfaceView principles. If the development encounters problems, you can first visit "Android development: SurfaceView basic usage summary and development issues sharing" to see if you have encountered the same problems as the author. The author has provided a solution in this article, if you have a better method, you can share it with us. Thank you.
For more information, see the author xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45966309. Thank you for your cooperation!