Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系

1、Surface

Surface

extends Object
implements Parcelable
java.lang.Object
   ?Android.view.Surface

Class Overview


Handle onto a raw buffer that is being managed by the screen compositor.简单翻译:Surface是原始图像缓冲区(raw buffer)的一个句柄,而原始图像缓冲区是由屏幕图像合成器(screen compositor)管理的。



1.1、 就如在C语言编程一样,通过一个文件的句柄,就可以操作文件,获取文件的内容。 同样的,通过Surface就可以获取raw buffer其中的内容。原生缓冲区(raw buffer)存储着当前窗口的像素数据。
1.2、事实上,当得到一个Surface对象时,同时会得到一个Canvas(画布)对象。这一点可以通过查看frameworksasecorejavaandroidviewSurface.java文件可知道Surface类定义了一个Canvas成员变量
  1. //@frameworksasecorejavaandroidviewSurface.java   
  2. // The mSurfaceControl will only be present for Surfaces used by the window   
  3. // server or system processes. When this class is parceled we defer to the   
  4. // mSurfaceControl to do the parceling. Otherwise we parcel the   
  5. // mNativeSurface.   
  6. private int mSurfaceControl;  
  7. private int mSaveCount;  
  8. private Canvas mCanvas;  
  9. private int mNativeSurface;  
  10. private int mSurfaceGenerationId;  
  11. private String mName;  
//@frameworksasecorejavaandroidviewSurface.java// The mSurfaceControl will only be present for Surfaces used by the window// server or system processes. When this class is parceled we defer to the// mSurfaceControl to do the parceling. Otherwise we parcel the// mNativeSurface.private int mSurfaceControl;private int mSaveCount;private Canvas mCanvas;private int mNativeSurface;private int mSurfaceGenerationId;private String mName;
1.3、 理解Canvas对象,可以把它当做画布,Canvas的方法大多数是设置画布的大小、形状、画布背景颜色等等,要想在画布上面画画,一般要与Paint对象结合使用,顾名思义,Paint就是画笔的风格,颜料的色彩之类的。
  1. // 创建画笔     
  2. Paint paint = new Paint();    
  3. paint.setColor(Color.RED);// 设置红色     
  4.   
  5. canvas.drawCircle(602010, paint);// 画一个圆    
1.4、Surface本身的作用类似一个句柄,得到了这个句柄就可以得到其中的Canvas、原生缓冲器以及其它方面的内容。
1.5、Surface实现了Parcelable接口,(implements Parcelable),也就是说Surface对象可以把显示内容的数据写入到 Parcel 中,并且能够从Parcel读回数据。

Parcelable

android.os.Parcelable
Known Indirect Subclasses AbsSavedState,AbsoluteSizeSpan,AccessibilityEvent,AccessibilityNodeInfo,AccessibilityServiceInfo,Account,AccountAuthenticatorResponse,ActivityInfo,ActivityManager.MemoryInfo,ActivityManager.ProcessErrorStateInfo,ActivityManager.RecentTaskInfo,ActivityManager.RunningAppProcessInfo,ActivityManager.RunningServiceInfo, ActivityManager.RunningTaskInfo, and 144 others.

Class Overview


Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field calledCREATOR, which is an object implementing theParcelable.Creator interface.简单翻译:
       实现这个接口的对象可以写入数据到Parcel,同时也可以把数据读出来。



2、SurfaceView

SurfaceView

extends View
java.lang.Object
   ?android.view.View
    ?android.view.SurfaceView
Known Direct Subclasses GLSurfaceView,RSSurfaceView,VideoView

Class Overview


Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screenThe surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes. Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by callinggetHolder().The Surface will be created for you while the SurfaceView"s window is visible; you should implementsurfaceCreated(SurfaceHolder) andsurfaceDestroyed(SurfaceHolder) to discover when the Surface is created and destroyed as the window is shown and hidden. One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics:
  • All SurfaceView and SurfaceHolder.Callback methods will be called from the thread running the SurfaceView"s window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread.
  • You must ensure that the drawing thread only touches the underlying Surface while it is valid -- betweenSurfaceHolder.Callback.surfaceCreated() andSurfaceHolder.Callback.surfaceDestroyed().
简单翻译:
         SurfaceView提供了一个专门用于绘制的surface,这个surface内嵌于。你可以控制这个Surface的格式和尺寸。Surfaceview控制这个Surface在屏幕的正确绘制位置。

        surface是Z-ordered的(也就是说在xyz坐标系中,按照Z坐标排序的,Z值大的表面覆盖在Z值小的表面的上方),这表明它总在自己所在窗口的后面。surfaceview在显示窗口处为Surface提供了一个可见区域,通过这个区域,才能看到Surface里面的内容。可以放置一些覆盖图层(overlays)在Surface上面,如Button、Textview之类的。但是,需要注意的是,如果Surface上面有全透明的控件,那么随着Surface的每一次变化,这些全透明的控件就会重新渲染,这样的话,就影响性能与显示的效果。

        你可以通过SurfaceHolder这个接口去访问Surface,而执行getHolder()方法可以得到SurfaceHolder接口。

        当SurfaceView的窗口可见时,Surface就会被创建,当SurfaceView窗口隐藏时,Surface就会被销毁。当然了,你也可以通过复写surfaceCreated(SurfaceHolder)surfaceDestroyed(SurfaceHolder)  这两个方法来验证一下Surface何时被创建与何时被销毁。

        SurfaceView提供了一个运行在渲染线程的surface,若你要更新屏幕,你需要了解以下线程知识。
  • 所有SurfaceView 和 SurfaceHolder.Callback的方法都应该在主线程(UI线程)里面调用,应该要确保渲染进程所访问变量的同步性。
  • 你必须确保只有当Surface有效的时候,(也就是当Surface的生命周期在SurfaceHolder.Callback.surfaceCreated()SurfaceHolder.Callback.surfaceDestroyed()之间)才能让渲染进程访问。


2.1、SurfaceView与Surface的联系

简单来说,SurfaceView与Surface的联系就是,Surface是管理显示内容的数据(implementsParcelable),包括存储于数据的交换。而SurfaceView就是把这些数据显示出来到屏幕上面。两者联系如图所示: