Android--surfaceview use of the detailed

Source: Internet
Author: User

Surfaceview is the inheritance class for view, which has a built-in surface for drawing. You can control the format and size of this surface. Surfaceview controls where this surface is drawn.
Surface is a deep sort (z-ordered), which indicates that it is always behind its own window. The Surfaceview provides a visible area that is visible only on the surface portion of the visible area and not visible outside the visible area. The layout display of surface is affected by the view hierarchy, and its sibling view nodes are displayed at the top. This means that the content of surface is obscured by its sibling view, which can be used to place a cloak (overlays) (for example, text and buttons). Note that if you have transparent controls on your surface, each change will cause the framework to recalculate the transparency of its and top-level controls, which can affect performance.
You can get this interface by accessing the Surface,getholder () method through the Surfaceholder interface.
Surface is created when Surfaceview becomes visible, and surface is destroyed before Surfaceview is hidden. This will save resources. If you want to see when your surface is created and destroyed, you can reload surfacecreated (Surfaceholder) and surfacedestroyed (Surfaceholder).
The core of Surfaceview is the provision of two threads: the UI thread and the render thread. It should be noted here:
1> all methods of Surfaceview and Surfaceholder.callback should be called in the UI thread, which is generally the main thread of the application. The various variables to be accessed by the render thread should be processed synchronously.
2> because surface may be destroyed, it is only valid between SurfaceHolder.Callback.surfaceCreated () and SurfaceHolder.Callback.surfaceDestroyed (), So make sure that the rendering thread is accessing the legitimate, effective surface.

Next, talk about your understanding of it.
1. Definition

Image data can be obtained directly from hardware interfaces such as memory or DMA, which is a very important drawing container.

It is characterized by the ability to draw on the screen in threads other than the main thread. This prevents the main thread from being blocked when the drawing task is heavy, which improves the program's response speed. In the game development more use Surfaceview, the game background, the character, the animation and so on as far as possible to draw in canvas canvases.

2. Realize

First inherit Surfaceview and implement Surfaceholder.callback interface
Reasons for using interfaces: because there is a principle of using Surfaceview, all the drawing work must be done before surface is created (surface-surface, which is often referred to in graphical programming.) Basically we can think of it as a mapping of video memory, to the content of surface
Can be copied directly to the video memory to display it, which makes the display very fast, and must end before surface is destroyed. So the surfacecreated and surfacedestroyed in callback are the boundaries of the drawing processing code.

Methods that need to be overridden

(1) Public void surfacechanged (surfaceholder holder,int format,int width,int height) {}

//Fires when the size of your surface changes

(2) Public void surfacecreated (Surfaceholder holder) {}

//Fires at creation time, typically called drawing threads here.

(3) Public void surfacedestroyed (Surfaceholder holder) {}

//Fires when destroyed, typically where the drawing thread is stopped and released.

The whole process: inherit Surfaceview and implement Surfaceholder.callback interface----> Surfaceview.getholder () Get Surfaceholder object----> Surfaceholder.addcallback (callback) Add callback function---->surfaceholder.lockcanvas () Get the canvas object and lock the canvas----> Canvas painting ---->surfaceholder.unlockcanvasandpost (canvas canvas) ends the lock drawing and submits the change to display the graphic.


3, Surfaceholder
Here's a class surfaceholder that you can use as a controller for your surface to manipulate your surface. Handle it on canvas to draw effects and animations, control surfaces, sizes, pixels, etc.
Several methods to be aware of:
(1), abstract void Addcallback (Surfaceholder.callback Callback);
Give Surfaceview the current holder a callback object.
(2), abstract Canvas Lockcanvas ();
Lock the canvas, usually after it is locked, it can be drawn through canvas objects it returns, drawing on it, and so on.
(3), abstract Canvas Lockcanvas (Rect dirty);
Lock an area of the canvas for paint, etc... Since the drawing is finished, the following unlockcanvasandpost will be called to change the display.
Compared to some of the higher memory requirements of the game, you can not redraw the dirty outside the pixels of other areas, can improve speed.
(4), abstract void unlockcanvasandpost (canvas canvas);

End the lock drawing and commit the change.

4. Example

<span style= "FONT-SIZE:14PX;" >public class Viewtest extends Activity {@Override public void onCreate (Bundle savedinstancestate) {s         Uper.oncreate (savedinstancestate);      Setcontentview (New MyView (this));  }//View internal classes Class MyView extends Surfaceview implements Surfaceholder.callback {private Surfaceholder         Holder          Private MyThread MyThread;             Public MyView (Context context) {super (context);             TODO auto-generated constructor Stub holder = This.getholder ();             Holder.addcallback (this); MyThread = new MyThread (holder);//Create a drawing thread} @Override public void surfacechanged (Surfaceholder                        Holder, int format, int width, int height) {//TODO auto-generated method stub } @Override public void surfacecreated (Surfaceholder holder) {//TODO Auto-gener     Ated Method Stub        Mythread.isrun = true;        Mythread.start ();  } @Override public void surfacedestroyed (Surfaceholder holder) {//TODO auto-generated method        Stub mythread.isrun = false;          }}//thread internal classes class MyThread extends thread {private Surfaceholder holder;          public Boolean Isrun;               Public MyThread (Surfaceholder holder) {This.holder =holder;          Isrun = true;              } @Override public void Run () {int count = 0;                  while (Isrun) {Canvas c = null; try {synchronized (holder) {c = Holder.lockcanvas ();//Lock the canvas, a                    After the lock, you can draw the canvas object from its back, and then paint it on it. C.drawcolor (Color.Black);//Set canvas background color Paint p = new paint ();            Create Brush P.setcolor (color.white);        Rect r = new Rect (100, 50, 300, 250);                    C.drawrect (R, p);                    C.drawtext ("This is the first" + (count++) + "seconds", 310, p); Thread.Sleep (1000);//Sleep time is 1 seconds}} catch (Exception e) {E.print              StackTrace (); } finally {if (c!= null) {HOLDER.UNLOCKCANV                Asandpost (c);//End lock drawing and commit the change. }}}}}</span>


Android--surfaceview use of the detailed

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.