1
/*** Entire 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 a class Surfaceholder can be used as a controller of surface to manipulate surface. Handle it on canvas to draw effects and animations, control surfaces, sizes, pixels, etc. Several methods to note: (1), abstract void Addcallback (Surfaceholder.callback Callback);//give Surfaceview the current holder a callback object. (2), abstract canvas Lockcanvas ();//Lock the canvas, usually after the lock can be used to return the canvas objects, such as painting on top of canvas, and so on. (3), abstract canvas Lockcanvas (Rect dirty);//Lock a region of the canvas for drawing, 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 lock drawing and commit changes. */ Public classBitmapviewextendsSurfaceviewImplementscallback{PrivateSurfaceholder Holder; PrivateMyrunnable runnable =NULL; /**
To perform a drawing operation in a thread
*/ Public classMyrunnableImplementsrunnable{PrivateSurfaceholder Myholder; Public BooleanIsrun; Publicmyrunnable (Surfaceholder myholder) { This. Myholder =Myholder; } Public voidrun () {intCount = 0; while(Isrun) {Canvas C=NULL; Try { synchronized(Myholder) {c= Myholder.lockcanvas ();//Lock the canvas, get the canvas object, paint, and more.C.drawcolor (Color.Black); Paint Paint=NewPaint ();//Create a brushPaint.setcolor (color.red);//Set Brush ColorRect r =NewRect (100, 50, 150, 300); C.drawrect (R, Paint); C.drawtext ("This is the first" + (count++) + "seconds", 100, 310, paint); Thread.Sleep (1000); } } Catch(Exception e) {e.printstacktrace (); }finally { if(c! =NULL) {holder.unlockcanvasandpost (c);//finish drawing, Commit changes } } } }; }; PublicBitmapview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } PublicBitmapview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicBitmapview (Context context) {Super(context); Holder= This. Getholder ();//Get Surfaceholder ObjectHolder.addcallback ( This);//Add a callback function} @Override Public voidSurfacechanged (Surfaceholder holder,intFormatintwidth,intheight) { //TODO fires when the size of your surface changes} @Override Public voidsurfacecreated (Surfaceholder holder) {//TODO fires at the time of creation, and typically calls the drawing thread here. Runnable =Newmyrunnable (holder); Runnable.isrun=true; NewThread (runnable). Start (); } @Override Public voidsurfacedestroyed (Surfaceholder holder) {//TODO fires when destroyed, typically where the drawing thread is stopped and released. Runnable.isrun =false; } }
2
The connection between surface, Surfaceview, Surfaceholder and Surfaceholder.callback of Android graphics system