Android SurfaceView (2)

Source: Internet
Author: User

Let's talk about SurfaceView:

(1) Definition:
Image data can be obtained directly from hardware interfaces such as memory or DMA, which is a very important drawing container.
It features: it can be drawn to the screen outside the main thread. In this way, the main thread blocking can be avoided when the drawing task is heavy, thus improving the response speed of the program. SurfaceView is often used in game development. The background, characters, and animations in the game should be drawn in the canvas whenever possible.
SurfaceView provides direct access to a graphic interface and controls the subview layer at the top of the interface. SurfaceView is provided for applications that need to directly draw pixels instead of using invisible forms. An important concept and clue in the Android system is that the Surface. View and its subclass (TextView, Button, etc.) should be painted on the Surface. Each Surface creates a Canvas object (but its attributes are often changed) to manage the drawing operations of a view on the Surface, such as drawing a point or draw a line.

Top layer.
(2) Implementation
First, we inherit SurfaceView and implement the SurfaceHolder. Callback interface.
Reason for using the interface: Because SurfaceView has a principle, all the drawing work must start after the Surface is created (the Surface can be used as a ing of the display to write the content to the Surface, can be directly copied to the video memory for display, which makes the display speed very fast), and must end before the Surface is destroyed. Therefore, surfaceCreated and surfaceDestroyed in CallBack form the boundary of the drawing proxy code.
Method to be rewritten:
A. public void sufaceChanged (SurfaceHolder holder, int format, int width, int height) {}// called when the Surface size changes
B. public void surfaceCreated (SurfaceHolder holder) {}// triggered when the Surface is created. Generally, the screen thread is called here.
C. public void surfaceDestroyed (SurfaceHolder holder) {}// triggered upon destruction. Generally, the screen thread is stopped and released here.
D. public void addCallback {}; // Add a callback function to SurfaceView.
E. public void lockCanvas {}; // lock the canvas. You must lock the canvas before drawing to obtain the canvas object.
F. public void unlockCanvasAndPost {}; // The canvas is locked when the painting starts. After the painting is complete, the canvas is unlocked.
G. public void removeCallback: removes the callback function from SurfaceView.

The difference between SurfaceView and View is that SurfaceView does not need to update the View through a thread, but before painting, you need to use locakCanvas to lock the canvas, get the canvas, and then draw the desired image on the canvas. After the painting is complete, use the lockCanvasAndPost method to unlock the canvas. So it can be displayed on the screen. The event processing rules are the same as the View rules.
 

The entire implementation process:

Inherit SurfaceView and implement SurfaceHolder. Callback interface ------> SurfaceView. getHolder () Get SurfaceHolder object -----> SurfaceHolder. addCallback (callback) add Callback function ----->
SurfaceHolder. lockCanvas () gets the Canvas object and locks the Canvas ------> Canvas painting -------> SurfaceHolder. unlockCanvasAndPost (canvas Canvas) ends locking the painting, and submits changes to display the image.
(3.) SurfaceHolder:
SurfaceHolder is used as a surface controller to manipulate the surface. It processes effects and animations drawn on its Canvas, controls the surface, size, pixels, and so on.
Several common methods:
A. abstract void addCallback (SurfaceHolder. Callbask callback); // a callback function is provided to the current owner of SurfaceView.
B. abstract Canvas lockCanvas (); // lock the Canvas. After locking the Canvas, you can use the Canvas object it returns to manipulate it.
C. abstract Canvas lockCanvas (Rect dirty); // draw a picture when a certain area of the Canvas is locked. As a result, the following unlockCanvasAndPost () is called to change the display content after the image is drawn. For games with relatively high memory requirements, you do not need to repeat pixels in other regions outside dirty to increase the speed.
D. abstract void unlockCanvasAndPost (Canvas canvas); // end the locked drawing and submit the changes.

Finally, use a SurfaceView to develop an oscilloscope to end the SurfaceView (there are too many code in the next blog). Thank you.

 

 

From Damon_tong's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.