Android Surfaceview Detailed

Source: Internet
Author: User

Surfaceview inherited the view, but we don't need to implement its draw method to draw ourselves, why? Because it has a big difference from view, view is in the UI thread to update itself, while Surfaceview in a sub-thread to update itself, which also shows its advantage, when the production of games and so on need to constantly refresh the view, because it is in the child thread, to avoid blocking the UI thread.

Surfaceview, which has a separate drawing surface, that it does not share the same drawing surface as its host window. Because of the independent drawing surface, the Surfaceview UI can be drawn in a separate thread. And because it does not occupy the main thread resources, Surfaceview can implement a complex and efficient UI on the one hand without causing the user input to be unresponsive in a timely manner.

Ordinary Android controls, such as TextView, buttons, and checkboxes, all draw their own UI over the drawing surface of the host window, which means that their UI is drawn in the main thread of the application. Because the main thread of the application needs to respond to user input in a timely manner, in addition to drawing the UI, the system will assume that the application is unresponsive, so a anr dialog box pops up. For some game screens, or for camera previews and video playback, their UIs are complex and require efficient drawing, so their UI is not suitable for drawing in the main thread of the application. This is when you have to generate a separate drawing surface for views that require a complex and efficient UI, and a separate thread to draw the UI for those views.

As long as you inherit the Surfaceview class and implement the Surfaceholder.callback interface, you can implement a custom Surfaceview.

Surfaceview There is a Getholder method, we can get a surfaceholder. With Surfaceholder, you can listen to the Surfaceview life cycle and get the canvas object. Canvas is the equivalent of a canvas where you can paint, draw lines, stooped, and other graphics.

Package Com.example.shengchanglu.test;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.util.log;import Android.view.SurfaceHolder ; Import Android.view.surfaceholder.callback;import android.view.surfaceview;/** * Created by Shengchanglu on 15/9/17.    */public class Mysurfaceview extends Surfaceview implements Callback, Runnable {private Thread th;    Private Surfaceholder SFH;    Private Canvas canvas;    private paint paint;    Private Bitmap bmp;    private int bmp_x, bmp_y;     Private Boolean Himi;        Public Mysurfaceview (context context, AttributeSet Attrs) {super (context);        This.setkeepscreenon (TRUE);        BMP = Bitmapfactory.decoderesource (Getresources (), R.drawable.logo);        SFH = This.getholder (); Sfh.addcallback (this);        Note 1 paint = new paint (); PaiNt.setantialias (TRUE);    This.setlongclickable (TRUE); } public void surfacecreated (Surfaceholder holder) {
int screenw = This.getwidth ();
int screenh = This.getheight ();//surfaceview is created before calling Surfacecreated, where it can get a long width. Instead of the above constructor
Himi = true; th = new Thread (This, "himi_thread_one");//Note 2nd.start (); } public void Surfacechanged (surfaceholder holder, int format, int width, int height) {
    } public    void Surfacedestroyed (Surfaceholder holder) {        Himi = false;//Note 3    } public    void Draw () {        try {            canvas = Sfh.lockcanvas ();            if (canvas! = null) {                canvas.drawcolor (color.white);                Canvas.drawbitmap (BMP, Bmp_x, bmp_y, paint);            }        } catch (Exception e) {        } finally {            if (canvas! = null)                sfh.unlockcanvasandpost (canvas);}    }    public void Run () {        while (Himi) {//Note 4            Draw ();            try {                thread.sleep;            } catch (Exception ex) {            }    }}}

/NOTE 1

Surfaceholder.callback Interface:

As long as you inherit the Surfaceview class and implement the Surfaceholder.callback interface, you can implement a custom Surfaceview, Surfaceholder.callback notify View,surfa when the underlying surface state changes. The Ceholder.callback has the following interfaces:

Surfacecreated (Surfaceholder holder): This function is called immediately after surface has been created for the first time. The program can do some initialization work in this function that is related to drawing the interface, in general it is to draw the interface on another thread, so do not draw surface in this function.

Surfacechanged (surfaceholder holder, int format, int width,int height) : This function is called when the state of surface (size and format) changes, and the function is called at least once after the surfacecreated call.

Surfaceholder class:

It's an interface for controlling surface, which gives you control over the size, format, and pixels of your surface, which is how you monitor its changes.

The Getholder () function of Surfaceview can get the Surfaceholder object, and Surface is inside the Surfaceholder object.                                                        While surface holds the pixel data for the current window, it does not work directly with surface during use, and is obtained by the Surfaceholder canvas Lockcanvas () or the canvas Lockcanvas () function. A Canvas object that modifies the data in your surface by drawing content on the canvas. If surface is not editable, or if it has not yet been created, the function returns NULL, and the contents of surface in Unlockcanvas () and Lockcanvas () are not cached, so you need to completely redraw the contents of your surface. In order to increase efficiency, only the changed parts are redrawn, you can call the Lockcanvas (Rect rect) function to specify a rect region so that content outside the zone is cached. After calling the Lockcanvas function to get the canvas, Surfaceview acquires a sync lock on surface until the unlockcanvasandpost (canvas canvas) function is called to release the lock. The synchronization mechanism here ensures that the surface is not changed (destroyed, modified) during the drawing process.

Android Surfaceview Detailed

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.