Surfaceview Use summary of Android

Source: Internet
Author: User

1. Concept

Surfaceview is a subclass of the view class that can obtain image data directly from hardware interfaces such as memory or DMA, which is a very important drawing view. 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. Implementation methods

1) Implementation steps

A Inherit Surfaceview

b Implementing the Surfaceholder.callback Interface

2) methods that need to be rewritten

(1)publicvoid surfacechanged (surfaceholder holder,int format,int width,int height) {} // fires when the size of your surface changes (2)publicvoid surfacecreated ( Surfaceholder holder) {} // fires at creation time, typically calling paint threads here.  (3)publicvoid surfacedestroyed (Surfaceholder holder) {} //  Fires when destroyed, typically where the drawing thread is stopped and released. 

3) Surfaceholder

The Surfaceholder,surface controller is used to manipulate surface. Handle it on canvas to draw effects and animations, control surfaces, sizes, pixels, etc. Several methods to be aware of:

(1),Abstract voidAddcallback (Surfaceholder.callback Callback);//give Surfaceview the current holder a callback object. (2),AbstractCanvas 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),AbstractCanvas 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 voidunlockcanvasandpost (canvas canvas);//end the lock drawing and commit the change. 

4) Summarize the process

Inherit Surfaceview and implement Surfaceholder.callback interface----> Surfaceview.getholder () Get Surfaceholder object----> Surfaceholder.addcallback (callback) Add a callback function---->surfaceholder.lockcanvas () Get the canvas object and lock the canvas----> Canvas painting---- >surfaceholder.unlockcanvasandpost (canvas canvas) ends the lock drawing and commits the change to display the graphic.

The following is a complete case:

 Public classViewtestextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (NewMyView ( This)); }     //view Inner class      classMyViewextendsSurfaceviewImplementsSurfaceholder.callback {PrivateSurfaceholder Holder; PrivateMyThread MyThread;  PublicMyView (Context context) {Super(context); //TODO auto-generated Constructor stubHolder = This. Getholder (); Holder.addcallback ( This); MyThread=NewMyThread (holder);//create a drawing thread} @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) {             //TODO auto-generated Method Stub} @Override Public voidsurfacecreated (Surfaceholder holder) {//TODO auto-generated Method StubMythread.isrun =true;        Mythread.start (); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {//TODO auto-generated Method StubMythread.isrun =false; }      }      //thread Inner class      classMyThreadextendsThread {PrivateSurfaceholder Holder;  Public BooleanIsrun;  PublicMyThread (Surfaceholder holder) { This. Holder =Holder; Isrun=true; } @Override Public voidrun () {intCount = 0;  while(Isrun) {Canvas C=NULL; Try                  {                      synchronized(Holder) {C= Holder.lockcanvas ();//Lock the canvas, usually after it is locked, it can be drawn through canvas objects it returns, drawing on it, and so on. C.drawcolor (Color.Black);//set the canvas background colorPaint p =NewPaint ();//Create a brushP.setcolor (Color.White); Rect R=NewRect (100, 50, 300, 250);                    C.drawrect (R, p); C.drawtext ("This is the first" + (count++) + "seconds", 100, 310, p); Thread.Sleep (1000);//sleep time is 1 seconds}}Catch(Exception e) {e.printstacktrace (); }finally{if(c!=NULL) {holder.unlockcanvasandpost (c);//end the lock drawing and commit the change. } }}}}

Surfaceview Use summary of Android

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.