Android Development: Surfaceview Basic Usage Summary

Source: Internet
Author: User

This article explains how to use Surfaceview, which is designed to help you quickly get started with surfaceview development. Since the previous article "Android Development: Surfaceview basic Usage summary and development problem sharing " is poorly typeset, a separate introduction to Surfaceview's basic usage summary.

Reprint Please specify author xiong_it and link: http://blog.csdn.net/xiong_it/article/details/45966309, thank you!


Surfaceview Basic Introduction


1, the system provides Surfaceview with a specially drawn surface, embedded in the Surfaceview view layer

2. Surface is where the screen data is stored, it holds a canvas object, that is, surface is where the screen is drawn.

3, Surfaceholder is the manager of surface, can control surface format and size, etc.


In summary: The picture is drawn on surface, and in Surfaceview, you manage and present the data content of your surface by acquiring Surfaceholder objects.


The difference between Surfaceview and normal view:

Normal view needs to update the UI in the UI thread, or it can easily cause ANR

Surfaceview is drawing a view in a worker thread


Surfaceview Basic Usage


Need to use Surfaceview occasions: Video playback, camera preview, game development and so on. In summary, Surfaceview is suitable for situations where the UI needs to be updated frequently.


The basic process for customizing Surfaceview:

1. Inheriting Surfaceview

2, rewrite Surfaceview at least one of the construction method

3, using Getholder () method to get Surfaceholder Reference object

4. Add an interface object that implements Surfaceholder.callback to the Surfaceholder object

5, rewrite the callback three methods: surfacechanged,surfacecreated,surfacedestroyed

6. Use the Surfaceholder object to format your surface

7. Use the Surfaceholder object to set the type of surface (Api 11 is deprecated)

8. Set surface size and height with Surfaceholder objects (optional)


The Surfaceview sample code is as follows:

First step: Inherit Surfaceviewpublic class Videoview extends Surfaceview{private surfacecallback msurfacecallback;private Surfaceholder msurfaceholder;private Canvas mcanvas;private panit mpanit;//Step Two: Override construction method Public Videoview (context context , AttributeSet Attrs) {Super (context, attrs);///Step three: Get Surfaceholder object msurfaceholder = This.getsurfaceholder ();//   Fourth step: Add Surfaceholder.callback Interface Msurfaceholder (). Addcallback (Mcallback);//Sixth step: Set surface format// Parameter: The int value defined in PixelFormat, see Pixelformat.javamsurfaceholder () in detail. SetFormat (pixelformat.rgba_8888); Seventh step: Set Surface type//Parameters: Surface_type_normal: Normal surface with RAM cache native data//Surface_type_hardware: For DMA (Direct memory Access engine and hardware-accelerated surface//SURFACE_TYPE_GPU: Surface//Surface_type_push_buffers for GPU acceleration: Indicates that surface does not contain native data, The data used by surface is provided by other objects,//using that type of surface in the camera Image preview, and the camera is responsible for providing the preview surface data so that the image preview will be smoother. Msurfaceholder (). SetType (surfaceholder.surface_type_push_buffers);} /** * Eighth Step: Set the size of your surface to be wide (optional) * * For externally setting the width of your surface * @param videowidth Set the width of your surface *@param videowidth set the high */public void setvideolayout (int videowidth, int videoheight) {msurfaceholder () for surface. Setfixedsize (Videowidth, videoheight);} /** * Fifth step: Implement Surfaceholder.callback Interface * Three ways to rewrite callback: surfacechanged,surfacecreated,surfacedestroyed */private Surfaceholder.callback mcallback = new Surfaceholder.callback () {/** * is called immediately when the surface format and size change, you can update your surface in this method * @ Param Holder holds the Surfaceholder object of the current surface * @param the new format of the format surface * @param the new width of the width surface * @param height on your surface Degrees */@Overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}/** * is immediately adjusted when surface is first created Use: when gaining focus. This is where the drawing thread is normally opened. * @param holder holds Surfaceholder object for current surface */@Overridepublic void surfacecreated (Surfaceholder holder) {mpanit = new Pan it ();//Draw a right-walking circle new Thread () {public void run () {synchronized (Msurfaceholder) {//Get the Canvas object for surface Mcanvas = Msurfaceholder.lockcanvas (); for (int i; i <; i++) {//Draw to the right of the Circle Mcanvas.drawcircle (at 500, mpanit); );} catch (Exception e) {e.printstacktrace ();}} Release the Canvas object update surfacemsurfaceholder.unlockandpost (Mcanvas);}}. Start ();} /** * Called immediately when surface is destroyed: when the focus is lost. Generally in this will draw the thread to stop destroying * @param holder holds the Surfaceholder object for the current surface */@Overridepublic void surfacedestroyed (Surfaceholder Holder) {//todo};}


First, we customized the Surfaceview according to the standard steps, and then opened a thread to draw a circle from left to right when surface was created. If you put the code to work, you will find that Surfaceview can not be drawn, this is why?

Because when you set the surface type:

Msurfaceholder (). SetType (surfaceholder.surface_type_push_buffers);//indicates that surface does not contain native data, and that the data used by surface is provided by other objects


It is not possible to draw using the canvas obtained by Msurfaceholder.lockcanvas () when using this type of surfaceholder.surface_type_push_buffers, please note!


There are three other types of Settype:


This article only shares the basic usage of surfaceview, does not involve the principle explanation, If you need to know the principle, you can: (Google) Surfaceview principle Detailed or: (Baidu)Surfaceview principle detailed. if the development of the problem, you can first visit the "Android Development: Surfaceview basic Usage summary and development problem sharing " to see if the same as the author of the problem, the author has given a solution to the text, if you have a better way , can comment share out, thank you.

Reprint Please specify the author xiong_it and Link:http://blog.csdn.net/xiong_it/article/details/45966309, thank you for your cooperation!

Android Development: Surfaceview Basic Usage Summary

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.