Android Rookie's growth note (--surfaceview) use

Source: Internet
Author: User

The previous drawing is in the custom view, but the view's drawing mechanism has the following drawbacks:

1, view lacks double buffering mechanism.

2. When the program needs to update the image on the view, the program must redraw the entire picture displayed on the view.

3. The new thread cannot update the view component directly.

Because the view has the above flaw, in the game development generally uses the Surfaceview to draw, Surfaceview generally will use with the Surfaceholder, Surfaceholder is used to draw on the surfaceview associated with it , call Surfaceview's Getholder () method to get the Surfaceholder associated Surfaceview.

Surfaceholder provides the following ways to get a canvas object:

1. Canvas Lockcanvas (): Lock the entire Surfaceview object to get the canvas on that surface.

2. Canvas Lockcanvas (rect dirty): Lock the area of the rect on the Surfaceview to get the canvas on that surface.

Two methods return the same canvas, but the second method only refreshes the circled area, and after the canvas drawing is finished, the canvas is released and the changes are submitted by the Unlockcanvasandpost (canvas) method. When the Surfaceholder Unlockcanvasandpost method is called, the drawing before the method is still buffered, and the next time the Lockcanvas () method locks the area may "obscure" it.

Package Com.example.erweimatest;import;import;import;import;import;import;import;import Android.os.bundle;import android.view.MotionEvent; Import Android.view.surfaceholder;import Android.view.surfaceholder.callback;import Android.view.SurfaceView; Import Android.view.view;import Android.view.view.ontouchlistener;public class Surfaceviewtest extends Activity { Private Surfaceholder holder;private paint paint; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (Savedinstancestate), Setcontentview (r.layout.main);p aint = new paint (); Surfaceview surface = (Surfaceview) Findviewbyid (;//Initialize Surfaceholder object holder = Surface.getholder (); Holder.addcallback (New Callback () {@Overridepublic void surfacedestroyed (Surfaceholder holder) {} @Overridepublic void Surfacecreated (Surfaceholder holder) {//Lock entire SURFACEviewcanvas canvas = Holder.lockcanvas ();//Draw background Bitmap back = Bitmapfactory.decoderesource ( SurfaceViewTest.this.getResources (),;//Draw Background canvas.drawbitmap (back, 0, 0, NULL);//Draw complete, release canvas, Commit to modify Holder.unlockcanvasandpost (canvas),//re-lock, "persist" last draw//This time Lockcanvas will obscure the last Lockcanvasholder.lockcanvas (new Rect (0, 0, 0, 0)); holder.unlockcanvasandpost (canvas);}  @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {//TODO auto-generated method {{Surface.setontouchlistener}}); {new Ontouchlistener () {@Overridepublic Boolean onTouch (View V, motionevent event) { if (event.getaction () = = Motionevent.action_down) {int cx = (int) event.getx (); int cy = (int) event.gety ();// Lock Surfaceview layout area, update only local content canvas canvas = Holder.lockcanvas (New Rect (CX-50, CY-50, CX + 50, CY +));//Save Canvas Current state CA ();//Rotate Canvas canvas.rotate ((), CX, CY);p Aint.setcolor (;//Draw Red squares canvas.drawrect (cx-40, cy-40, CX, CY, paint);//Restore the canvas before the Save state Canvas.restore ();p aint.seTColor (;//Draw Green squares canvas.drawrect (CX, CY, CX + MAX, CY + +, paint);//Draw complete, release canvas, Submit modification Holder.unlockcanvasandpost (canvas);} return false;}});}}

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><surfaceview  android:id= "@+id/show" android:layout_width= "fill_parent" android:layout_height= "Fill_ Parent "/></linearlayout>
Operating effect:

As you can see, the first drawing is obscured by the second area, and the third drawing may obscure the area that was drawn the second time, but it will not obscure the first area. If the second drawn area is obscured by the third area, the first drawing may be exposed.

An oscilloscope based on Surfaceview development:

Package Com.example.erweimatest;import Java.util.timer;import Java.util.timertask;import; Import;import;import;import;import Android.os.bundle;import Android.view.surfaceholder;import Android.view.surfaceholder.callback;import Android.view.surfaceview;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Showvawe extends Activity{private Surfaceholder holder;private Paint paint;final int HEIGHT = 320;final int WIDTH = 320;final int x_offset = 5;private int C x = x_offset;//The actual y-axis position int centery = HEIGHT/2; Timer timer = new timer (); TimerTask task = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (R.layout.activity_main); final Surfaceview surface = (Surfaceview) Findviewbyid (;// Initialize the Surfaceholder object holder = Surface.getholder ();p aint = new paint ();p aint.SetColor (;p aint.setstrokewidth (3); Button sin = (button) Findviewbyid (; Button cos = (button) Findviewbyid (, Onclicklistener listener = (new Onclicklistener () {@Overridepublic void Oncl Ick (final View source) {drawback (holder); CX = x_offset;if (Task! = null) {Task.cancel ();} task = new TimerTask () {@Overridepublic void run () {int cy = Source.getid () = = CenterY-(int) (* Math.sin ( CX-5) * 2 * math.pi/150): centery-(int) (+ * Math.Cos ((cx-5) * 2 * math.pi/150)); Canvas canvas = Holder.lockcanvas (New Rect (CX, Cy-2, cx+2, CY + 2)), Canvas.drawpoint (CX, CY, paint), CX ++;if (cx > WID TH) {task.cancel (); task = null;} Holder.unlockcanvasandpost (canvas);}}; Timer.schedule (Task, 0, 30);}); Sin.setonclicklistener (listener); Cos.setonclicklistener (listener); Holder.addcallback (new Callback () {@ overridepublic void surfacedestroyed (Surfaceholder holder) {//TODO auto-generated method stub} @Overridepublic void Surfacecreated (Surfaceholder holder) {//TODO auto-generated method stub} @Overridepublic void Surfacechanged (surfaceholder holder, int format, int width,int height) {/ /TODO auto-generated Method stub}}); private void drawback (Surfaceholder holder) {Canvas canvas = Holder.lockcanvas ();//Draw white background canvas.drawcolor (color.white ); Paint p = new paint ();p. SetColor (Color.Black);p. Setstrokewidth (2);//Draw Axis Canvas.drawline (X_offset, CenterY, WIDTH, CenterY, p); Canvas.drawline (X_offset, Max, X_offset, HEIGHT, p); holder.unlockcanvasandpost (canvas); Holder.lockcanvas (New Rect (0, 0, 0, 0)); holder.unlockcanvasandpost (canvas);}}

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><linearlayout android:orientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= " Wrap_content "android:gravity=" center "><button android:id=" @+id/sin "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "positive spin curve"/><button android:id= "@+id/cos" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Yu Yen curve"/></linearlayout>< Surfaceview android:id= "@+id/show" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android: gravity= "Center"/></linearlayout>
Operation Result:

When the program draws the current point on the positive and Yu Yen waves, the program does not need to redraw the entire screen, surfaceholder as long as the current drawing point to lock the small range, the system updates the screen as long as the range can be updated.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.