Android--Surfaceview drawing

Source: Internet
Author: User

Surfaceview

Surfaceview is a special subclass of view that is designed to provide another thread for drawing operations.

    • Steps

1. Draw with Surfaceview, first create a class, inherit Surfaceview, and this class should implement Surfaceholder.callback interface .

The three callback functions in this interface (surfacechanged (surfaceholder holder, int format, int width, int height), surfacecreated (Surfaceholder Holder), surfacedestroyed (Surfaceholder holder), respectively, corresponds to when surface is changed, created, and destroyed.

2. The operation of the surface object is done through Surfaceholder. So, when your Surfaceview class initializes, you need to call Getholder () to get the Surfaceholder object, and then use the Addcallback () plus the callback interface (Because your class implements the appropriate interface, this is passed here).

3. In your Surfaceview class, you should create a thread class that handles the drawing operation. To do this, pass the Surfaceholder object obtained above to this thread class.

4. Draw: Draw operations in the Run () method of the thread class, get the canvas object through the Lockcanvas () method, then draw with this object, call Unlockcanvasandpost () after drawing is finished, and pass in the canvas object , your surface will be drawn as canvas.

Note: The previous content will be retained each time you use Surfaceholder to get the canvas.

Code
 Public classMysurfaceviewextendsSurfaceviewImplementssurfaceholder.callback{PrivateDrawthread Mthread =NULL;  PublicMysurfaceview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle);    Init (); }     PublicMysurfaceview (Context context, AttributeSet attrs) {Super(context, attrs);    Init (); }     PublicMysurfaceview (Context context) {Super(context);    Init (); }    Private voidinit () {LOG.D (Appconstants.log_tag,"Init"); Surfaceholder Holder=Getholder (); Holder.addcallback ( This); Mthread=NewDrawthread (holder); } @Overrideprotected voidOnsizechanged (intWintHintOLDW,intOLDH) {LOG.D (Appconstants.log_tag,"Onsizechanged"); Super. Onsizechanged (W, H, OLDW, OLDH); } @Override Public voidsurfacecreated (Surfaceholder holder) {LOG.D (Appconstants.log_tag,"Surfacecreated"); Mthread.setrun (true);    Mthread.start (); } @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) {LOG.D (Appconstants.log_tag,"Surfacechanged"); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {LOG.D (Appconstants.log_tag,"Surfacedestroyed"); Mthread.setrun (false); }    /*** Draw Thread class **/     Public classDrawthreadextendsThread {PrivateSurfaceholder Mholder =NULL; Private BooleanIsrun =false;  PublicDrawthread (Surfaceholder holder) {LOG.D (Appconstants.log_tag,"Drawthread Constructor"); Mholder=Holder; }         Public voidSetrun (BooleanIsrun) {LOG.D (Appconstants.log_tag,"Drawthread Setrun:" +Isrun);  This. Isrun =Isrun; } @Override Public voidrun () {LOG.D (Appconstants.log_tag,"Drawthread Run"); intCount = 0;  while(isrun) {Canvas Canvas=NULL; synchronized(mholder) {Try{log.d (Appconstants.log_tag,"Drawing-------------"); Canvas=Mholder.lockcanvas ();                        Canvas.drawcolor (Color.White); Paint P=NewPaint ();                        P.setcolor (Color.Black); Rect R=NewRect (100, 50, 300, 250);                        Canvas.drawrect (R, p); Canvas.drawtext ("This is the first" + (count++) + "seconds", 100, 310, p); Thread.Sleep (1000);//sleep time is 1 seconds                    }                    Catch(Exception e) {log.d (Appconstants.log_tag,"Throw Exception in Run");                    E.printstacktrace (); }                    finally                    {                        if(NULL!=canvas)                        {mholder.unlockcanvasandpost (canvas); }                    }                }            }        }    }}
I'm the dividing line of the king of the land Tiger.

Reference: http://www.cnblogs.com/mengdd/archive/2013/04/09/3009897.html

Android--Surfaceview drawing

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.