On the--surface, Surfaceview and Surfaceholder.callback of Android

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47167553
First, Surface

surface is described in the documentation for the SDK: Handle onto a raw buffer that's being managed by the screen Compositor,an Surface in the droid is a place for drawing (graphics) or images (image), and for view and its subclasses, it's on surface, Each surface object is synthesized from Surfaceflinger to framebuffer, each surface is double-buffered, it has a backbuffer and a frontbuffer,surface creates a canvas object, Used to manage surface drawing operations, canvas corresponds to bitmap and stores content on your surface. The process is:

1: Create a Bitmap object.

2: Create a Canvas object associated with the created Bitmap object.

3: Draw on canvas.

4: Lock the canvas canvas.

5: Draw the bitmap content into the backbuffer.

6: Unlock canvas canvas.

Second, Surfaceview

Surfaceview is a subclass of view Class View, implements the Parcelable interface and implements the Parcelable interface, which embeds a surface that is specifically for drawing, Surfaceview can control the format and size of this surface, as well as where surface is drawn. It can be understood that surface is the place where data is managed, and Surfaceview is where the data is presented.

Third, Surfaceholder

Surfaceholder is an interface, similar to a surace listener. Monitor surface creation, destruction, or change with the following three callback methods.

The Getholder method is called in Surfaceview to get the surfaceholder of surface in the current Surfaceview, and theimportant methods in Surfaceholder are:

1:abstract void Addcallback (Surfaceholder.callback Callback); Add a Surfaceholder.callback callback interface for Surfaceholder .

2: abstract Canvas Lockcanvas (); get the Canvas object in your surface and lock it. The resulting Canvas object.

3:abstract void unlockcanvasandpost (canvas canvas); When you are done modifying the data in your surface, release the sync lock, commit the changes, and then show the new data .

Iv. Surfaceholder.callback

Surfaceholder.callback is a static sub-interface inside the Surfaceholder interface, andthree interface methods are defined in Surfaceholder.callback:
1:public void sufacechanged (surfaceholder holder,int format,int width,int height) {The size of}//surface is changed when it is called.
2:public void surfacecreated (Surfaceholder holder) {}//surface is fired when created, typically the thread that invokes the screen here.
3:public void surfacedestroyed (Surfaceholder holder) {}//Fires when destroyed, typically stopping and releasing the screen's threads here.

The essential difference between surfaceview and view is that Surfaceview is able to redraw the screen in a new, separate thread and view must update the screen in the main thread of the UI. Here is an example of Surfaceview:

Import Android.content.context;import Android.graphics.bitmap;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.graphics.drawable.bitmapdrawable;import Android.view.surfaceholder;import Android.view.SurfaceView; Import Android.view.keyevent;import Android.view.motionevent;import Android.view.surfaceholder.callback;public Class Mysurfaceview extends Surfaceview implements Runnable, Callback {private surfaceholder mholder;//For controlling Surfacevi EW private Thread t; Declares a thread private volatile Boolean flag; The identity of the thread run, used to control the thread private Canvas Mcanvas; Declare a canvas private Paint p;    Declare a brush float m_circle_r = 10;        Public Mysurfaceview (Context context) {super (context); Mholder = Getholder (); Get Surfaceholder Object Mholder.addcallback (this); Add status Monitor p = new Paint () for Surfaceview; Create a Brush object P.setcolor (Color.White); Sets the color of the brush to white setfocusable (true); //Set Focus}/** * Call this function when Surfaceview is created */@Override public void surfacecreated (Surfaceholder holder) { t = new Thread (this); Create a Thread object flag = true; Sets the identity of the thread run to true T.start (); Start thread}/** * Call this function when Surfaceview's view is changed */@Override public void surfacechanged (Surfaceholder hol der, int format, int width, int height) {}/** * When Surfaceview is destroyed, call this function */@Override Publ IC void surfacedestroyed (Surfaceholder holder) {flag = false;//Set the thread run identity to false mholder.removecallback (th    IS);    }/** * Called when the screen is touched */@Override public boolean ontouchevent (Motionevent event) {return true; * * * @Override public boolean onKeyDown (int keycode, keyevent event) {if (KeyCode/**) when the user presses the key    = = keyevent.keycode_dpad_up) {} return Super.onkeydown (KeyCode, event);    } @Override public boolean onKeyUp (int keycode, keyevent event) {    Surfacedestroyed (Mholder);    Return Super.onkeydown (KeyCode, event);                    } @Override public void Run () {while (flag) {try {synchronized (Mholder) { Thread.Sleep (100); Let the thread rest 100 milliseconds Draw ();            Call custom Draw Method}} catch (Interruptedexception e) {e.printstacktrace (); } finally {if (Mcanvas! = null) {//Mholder.unlockcanvasandpost (Mcanvas);//End lock drawing and mention                Change of hand. }}}}/** * Customize a method to draw a circle on the canvas */protected void Draw () {Mcanvas = Mholder.lock Canvas ();            Get the Canvas object and start painting on the canvas if (mcanvas! = null) {Paint paint = new paint (Paint.anti_alias_flag);            Paint.setcolor (Color.Blue);            Paint.setstrokewidth (10);            Paint.setstyle (Style.fill);            if (M_circle_r >= (getwidth ()/)) {m_circle_r = 0;   } else {             m_circle_r++;            } Bitmap pic = ((bitmapdrawable) getresources (). getdrawable (R.DRAWABLE.QQ)). Getbitmap ();            Mcanvas.drawbitmap (pic, 0, 0, paint);                            for (int i = 0, i < 5; i++) for (int j = 0; J < 8; j + +) Mcanvas.drawcircle ( (GetWidth ()/5) * i + (getwidth ()/Ten), (GetHeight ()/8) * j + (Getheig            HT ()/+), M_circle_r, paint); Mholder.unlockcanvasandpost (Mcanvas); Finish drawing and display the canvas on the screen}}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

On the--surface, Surfaceview and Surfaceholder.callback of Android

Related Article

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.