1、Surface
Handle onto a raw buffer that is being managed by the screen compositor.
簡單翻譯:
Surface是原始圖像緩沖區(raw buffer)的一個句柄,而原始圖像緩沖區是由屏幕圖像合成器(screen compositor)管理的。
1.2、事實上,當得到一個Surface對象時,同時會得到一個Canvas(畫布)對象。這一點可以通過查看\frameworks\base\core\java\android\view\Surface.java文件可知道Surface類定義了一個Canvas成員變量
//@\frameworks\base\core\java\android\view\Surface.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.4、Surface本身的作用類似一個句柄,得到了這個句柄就可以得到其中的Canvas、原生緩沖器以及其它方面的內容。
1.5、Surface實現了Parcelable接口,(implements Parcelable),也就是說Surface對象可以把顯示內容的數據寫入到 Parcel 中,並且能夠從Parcel讀回數據。
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.
2、SurfaceView
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 screen
The 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:
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.
SurfaceHolder.Callback.surfaceCreated()
andSurfaceHolder.Callback.surfaceDestroyed()
.簡單翻譯:
surfaceCreated(SurfaceHolder)
和 surfaceDestroyed(SurfaceHolder)
這兩個方法來驗證一下Surface何時被創建與何時被銷毀。SurfaceHolder.Callback
的方法都應該在主線程(UI線程)裡面調用,應該要確保渲染進程所訪問變量的同步性。
SurfaceHolder.Callback.surfaceCreated()
和SurfaceHolder.Callback.surfaceDestroyed()
之間)才能讓渲染進程訪問。2.1、SurfaceView與Surface的聯系
簡單來說,SurfaceView與Surface的聯系就是,Surface是管理顯示內容的數據(implementsParcelable),包括存儲於數據的交換。而SurfaceView就是把這些數據顯示出來到屏幕上面。
兩者聯系如圖所示: